Objects & Class

Objects & Class

1、定义一个类。

  

  上述代码中,numberOfSides是实例变量,simpleDescription也是实例方法。

2、创建实例,使用实例。

  

3、init是构造函数,deinit是析构函数:

  

  上图代码中使用self来引用成员变量是为了与局部变量name以区分。每个成员变量都要被初始化,要么在定义处,要么在init方法中。

4、继承时,使用super来引用父类的方法,使用override来覆盖父类的方法。如果子类有一个和父类签名一样的方法而无override声明的话,compiler会报错。对于有override而父类无相同方法的情况,compiler也会报错。

  

5、属性可以有get&set方法。

  

  

  在set方法中,隐含的传入参数是newValue。也可以set的()中指定参数名。

6、调用Method时,从第2个参数开始,必须显式地指明参数名。另外Method的参数可以有2个名字。

  

7、Optional Value:If the value before the ? is nil, everything after the ? is ignored and the value of the whole expression is nil.

  

  

  

原文地址:https://www.cnblogs.com/tekkaman/p/3784179.html