大佬教程收集整理的这篇文章主要介绍了swift - 类的继承,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
不同于结构体和枚举类型,继承是类特有的。
语法:
class MyClass:Superclass{ //@H_828_16@myClass:子类 Superclass:父类
//类的定义
}
EG:
class Animal {
var eyes :Int
var month :Int
var nose :Int
var voice :String
init(){
eyes =2
@H_506_129@month =1
nose =1
voice ="voice"
}
return"\(eyes) eyes ; up to\(@H_506_129@month) month"
}
}
class Cat: Animal {
overrideinit() { //重写init()
super.init()
voice ="miao"
}
}
var cat = Cat()
print(cat.@H_647_301@voice) //"miao\n"
2.重写属性
class Animal {
var eyes :Int
var month :Int
var nose :Int
var voice :String
init(){
eyes =0
@H_506_129@month =0
nose =0
voice ="voice"
}
return"\(eyes) eyes ; up to\(@H_506_129@month) month"
}
}
class Cat: Animal {
overrideinit() {
super.init()
voice ="miao"
}
overridevar eyes: Int{
get{
returnsuper.eyes
}
set{
super.eyes =@H_939_624@max(newValue,100)
}
}
}
var cat = Cat()
print(cat.voice)
cat.eyes =1000
print(cat.eyes)
若不想被继承,可在前面加final.
以上是大佬教程为你收集整理的swift - 类的继承全部内容,希望文章能够帮你解决swift - 类的继承所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。