Swift 学习笔记 - 类(1) 定义一个类

在Swift中,类用关键字class声明,有两种类型,一种是定义一个父类,一种是定义一个子类。




类是事物的共同点抽象。对象是类的具体化。

门就是一个类。所有的门都有宽度,高度,颜色的属性,都有是否打开,是否锁上的属性。

class Door {
    var width : Float = 100
    var height : Float = 300
    var color :String = "clearColor"
    var isOpened : Bool = false
    var isLocked : Bool = false
}

门可以打开,关上,上锁,开锁的动作,这些就是“门”这个类的方法。

//在执行这些动作之前先判断门的状态,是否打开,是否上锁
    func open() -> String {
        if !self.isOpened {
            self.isOpened = true
            return "the door is opened!"
        }
        else {
            return "the door has be opened!"
        }
    }
    func close() -> String {
        if self.isOpened {
            self.isOpened = false
            return "the door is closed!"
        }
        else {
            return "the door has be closed!"
        }
    }
    func locked() -> String {
        if !isLocked {
            self.isLocked = true
            return "the door is locked!"
        }
        else {
            return "the door has be locked!"
        }
    }
    func unlocked() -> String {
        if self.isLocked {
            self.isLocked = false
            return "the door is unlocked!"
        }
        else {
            return "the door has be unlocked"
        }
    }

使用Door()方法,实例化一个对象出来。

var myDoor1 = Door()

这时所实例化的对象的属性,是定义类时给出的初始值。


这个就好比一个门从工厂生产出来,都有一些默认的属性。

如果要定制一个门,我们就需要给工厂提出要求,给出和默认值不同的尺寸。

这个时候就需要对Door这个类的init方法进行重写。这个方法也是写在类体中。

init(width : Float,height : Float,color : String) {
        self.width = width;
        self.height = height;
        self.color = color;
}

再次初始化的时候,我们需要传递3个参数,分别是宽度,高度和颜色,这样就可以自己定制一个“门”,而不是默认的。

和用默认的初始化方法进行对比,宽度,高度和颜色三个属性值已经变成我们给定的值。


假如我们只需要改变Door里的一个属性值,其余的使用默认值,那么我们再使用刚才重写的init方法实例化一个对象就很不方便。

所以我们需要再写一个init方法,只带一个我们需要改变的那个参数,比如Door的颜色color。这样,我们就有两个init方法来实例化一个类Door的对象。

    init(width : Float,height : Float,color : String) {
        self.width = width;
        self.height = height;
        self.color = color;
    }
    
    init(color : String) {
        self.color = color
    }

我们再用新的init方法实例一个对象,看看三个对象的属性的不同。


这里需要注意的是,重写init方法之后,再调用不带参数的Door()进行实例化,系统会报错。


错误:不能调用没有参数的初始化方法。

这时,我们需要将不带参数的init方法添加到类体中。

    init(width : Float,height : Float,color : String) {
        self.width = width;
        self.height = height;
        self.color = color;
    }
    
    init(color : String) {
        self.color = color
    }
    
    init() {
        
    }

下面给出完整的类体内容。

class Door {
    //属性
    var width : Float = 100
    var height : Float = 300
    var color :String = "clearColor"
    var isOpened : Bool = false
    var isLocked : Bool = false
    //初始化方法
    init(width : Float,height : Float,color : String) {
        self.width = width;
        self.height = height;
        self.color = color;
    }
    
    init(color : String) {
        self.color = color
    }
    
    init() {
        
    }
    //方法,动作
    //在执行这些动作之前先判断门的状态,是否打开,是否上锁
    func open() -> String {
        if !self.isOpened {
            self.isOpened = true
            return "the door is opened!"
        }
        else {
            return "the door has be opened!"
        }
    }
    func close() -> String {
        if self.isOpened {
            self.isOpened = false
            return "the door is closed!"
        }
        else {
            return "the door has be closed!"
        }
    }
    func locked() -> String {
        if !isLocked {
            self.isLocked = true
            return "the door is locked!"
        }
        else {
            return "the door has be locked!"
        }
    }
    func unlocked() -> String {
        if self.isLocked {
            self.isLocked = false
            return "the door is unlocked!"
        }
        else {
            return "the door has be unlocked"
        }
    }
}

笔者感觉,Swift的类定义和Java,C++很像,所以总的来说还是很好理解的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值