其他分享
首页 > 其他分享> > Swift学习-Class

Swift学习-Class

作者:互联网

1.声明Class

class A{
    var str = "Hello, Class"
    var number = 1

}
//创建类的实例
var a = A()
print(a.str)
//打印结果:Hello, Class

2.类与结构体不同,类没有默认初始化方法。我们需要在类内部自己定义初始化方法传参。

结构体可以直接写为:

var a = A(str:"test",number:10)

但是用同样方法写类的时候是会报错的,如果不自定义初始化方法,只能使用空初始化:

var a = A()

自定义初始化方法

class B{
    var str = "Hello, Class"
    var number = 1
    init(str: String,number: Int){
        self.str = str
        self.number = number
    }
    //无参初始化,委托初始化方法
//    convenience init(){
//        self.init(str: "Hello, Class",number: 1)
//    }
    //或者这样定义也可以
    init() {
        str = "Declare as many initalisers as you want"
        number = 0
    }
}
var b = B(str: "Hello this is B", number: 2)
print("Default String is \(b.str) and Default number is \(b.number)")
//打印结果:Default String is Hello this is B and Default number is 2

如果自定义了初始化方法,此时申明空初始化会报错;
提示:Missing arguments for parameters 'str', 'number' in call
此时,我们可以在类中声明一个代理依托,声明一个无参初始化。此时就不报错了。

3.类继承:类支持继承,结构体不支持

子类继承父类的特征,然后可以对其进行优化,并添加新的特征

class Animal{
    var isVeg : Bool
    var eats : String
    var numberOfLegs : Int
    init(isVeg: Bool, eats: String, numberOfLegs: Int) {
        self.isVeg = isVeg
        self.eats = eats
        self.numberOfLegs = numberOfLegs
    }
    func printProperties(){
        print("Is Veg? \(isVeg). Eats:\(eats). Number of legs:\(numberOfLegs)")
    }
}

var anim = Animal(isVeg: false, eats: "Can eat you", numberOfLegs: 4)
anim.printProperties()
//打印结果:Is Veg? false. Eats:Can eat you. Number of legs:4

创建子类

class Tiger : Animal{
    //Add class specific implementaion
}
var tiger = Tiger(isVeg: false, eats: "Can eat you", numberOfLegs: 4)
tiger.printProperties()
//打印结果:Is Veg? false. Eats:Can eat you. Number of legs:4

需要重写父类中的函数时,需要用override修饰函数名

class Tiger : Animal{
    override func printProperties() {
        super.printProperties()
        print("This is Subclass Tiger")
		}
}
var tiger = Tiger(isVeg: false, eats: "Can eat you", numberOfLegs: 4)
tiger.printProperties()
//打印结果:
//Is Veg? false. Eats:Can eat you. Number of legs:4
//This is Subclass Tiger

下面例子是在子类中添加新属性

class Tiger : Animal{
    var color : String
    override func printProperties() {
        //调用父类这个方法,会多打印一个结果,所以在重写父类方法时,也可以不加这个
//        super.printProperties()
        print("This is tiger Is Veg \(isVeg). Eats:\(eats). Number of legs:\(numberOfLegs).color \(color)")
    }
    init(isVeg: Bool, eats: String, numberOfLegs: Int,color:String) {
        self.color = color
        //如果想要在子类中引用父类的属性或函数,使用super.调用
        super.init(isVeg: isVeg, eats: eats, numberOfLegs: numberOfLegs)
    }
}
var tiger = Tiger(isVeg: false, eats: "Can eat you", numberOfLegs: 4,color: "yellow")
tiger.printProperties()
//打印结果:This is tiger Is Veg false. Eats:Can eat you. Number of legs:4.color yellow

类与结构体的另一个区别就是:类是引用类型的,结构体是值类型

var tiger = Tiger(isVeg: false, eats: "Can eat you", numberOfLegs: 4, color: "White")
tiger.printProperties()
//打印结果:This is tiger Is Veg false. Eats:Can eat you. Number of legs:4.color White
/ar tiger1 = tiger
tiger1.color = "Yellow"
tiger1.printProperties()//打印结果:This is tiger Is Veg false. Eats:Can eat you. Number of legs:4.color Yellow
tiger.printProperties()//打印结果:This is tiger Is Veg false. Eats:Can eat you. Number of legs:4.color Yellow

tiger.eats = "they can eat everything"
tiger1.printProperties()
tiger.printProperties()
//打印结果:This is tiger Is Veg false. Eats:they can eat everything. Number of legs:4.color Yellow
//打印结果:This is tiger Is Veg false. Eats:they can eat everything. Number of legs:4.color Yellow

4.便利初始化:设置属性值的初始值

class A {
    var eyes: Int
    var legs: Int
    init(eyes: Int, legs: Int) {
        self.eyes = eyes
        self.legs = legs
    }
    
    
    convenience init() {
        self.init(eyes: 2, legs: 4)
    }

}

var a = A()
print(a.eyes) //prints 2
print(a.legs) //prints 4

便利初始化,只有当类中已经声明了init()时,才可以定义

标签:tiger,color,eats,eat,学习,var,legs,Swift,Class
来源: https://www.cnblogs.com/PotatoToEgg/p/14950456.html