码迷,mamicode.com
首页 > 编程语言 > 详细

swift语言点评十二-Subscripts

时间:2018-04-04 15:16:35      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:with   scripts   ble   store   triangle   print   define   amp   span   

Classes, structures, and enumerations can define subscripts, which are shortcuts for accessing the member elements of a collection, list, or sequence.

 

下标的形式和函数相同,并且set和get合一

subscript(row: Int, column: Int) -> Double

 

比较:

In addition to simple properties that are stored, properties can have a getter and a setter.

  1. class EquilateralTriangle: NamedShape {
  2. var sideLength: Double = 0.0
  3. init(sideLength: Double, name: String) {
  4. self.sideLength = sideLength
  5. super.init(name: name)
  6. numberOfSides = 3
  7. }
  8. var perimeter: Double {
  9. get {
  10. return 3.0 * sideLength
  11. }
  12. set {
  13. sideLength = newValue / 3.0
  14. }
  15. }
  16. override func simpleDescription() -> String {
  17. return "An equilateral triangle with sides of length \(sideLength)."
  18. }
  19. }
  20. var triangle = EquilateralTriangle(sideLength: 3.1, name: "a triangle")
  21. print(triangle.perimeter)
  22. triangle.perimeter = 9.9
  23. print(triangle.sideLength)

swift语言点评十二-Subscripts

标签:with   scripts   ble   store   triangle   print   define   amp   span   

原文地址:https://www.cnblogs.com/feng9exe/p/8716471.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!