swift语言点评十二-Subscripts

Posted zzfx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift语言点评十二-Subscripts相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

swift语言点评八-枚举

swift语言点评二

Swift-下标脚本和继承(Subscripts and Inheritance)

swift语言点评四-Closure

swift语言点评三 - Basic Operators

swift语言点评十九-类型转化与检查