swift 4.2 官方文档学习笔记(十)- Method + Subscript + inheritance

Sellen Wei
1 min readDec 16, 2018

--

  1. Instance methods are functions that belong to instances of a particular class, structure, or enumeration.(定义)
  2. Inside Instance methods, If you don’t explicitly write self, Swift assumes that you are referring to a property or method of the current instance whenever you use a known property or method name within a method.(如果没有写self,含义是什么)
  3. 结构体和枚举是value type。默认情况下,值类型的属性不能在它的实例方法中被修改。但是可以使用mutating来改变,这相当于在运算时,property被替换成了新的instance

同样,mutating也可以修改instance本身,assign an entirely new instance to the implicit self property

Type Methods(static method)

Subscripts

首先来说一下含义,实际上下标可以定义在类、结构体和枚举中,是访问集合、列表或序列中元素的快捷方式。比如array[index], dict[“bird”]中的index和“bird都是下标”

使用下标必须要生成instance,而且下标不能使用static和class关键词,也就是不能成做类下标。

如何在struct中声明下标并且使用:

Inheritance

继承是一个class专属的性能,可以被继承的包括superclass的function,property和subscript。

重写:

如果不想被重写,可以在super class里面加上final 修饰符(比如 final var , final func , final class func , final subscript )

你可以通过在类定义中在 class 关键字前面写 final 修饰符( final class )标记一整个类为终点。Any attempt to subclass a final class is reported as a compile-time error.

--

--