swift - 类的继承
来源:互联网 发布:mba加油站 知乎 mpacc 编辑:程序博客网 时间:2024/06/06 19:25
不同于结构体和枚举类型,继承是类特有的。
语法:
class MyClass:Superclass{ //MyClass:子类 Superclass:父类
//类的定义
}
EG:
class Animal {
var eyes :Int
var month :Int
var nose :Int
var voice :String
init(){
eyes =2
month =1
nose =1
voice ="voice"
}
func description() ->String {
return"\(eyes) eyes ; up to\(month) month"
}
}
class Cat: Animal {
overrideinit() { //重写init()
super.init()
voice ="miao"
}
}
var cat = Cat()
print(cat.voice) //"miao\n"
2.重写属性
class Animal {
var eyes :Int
var month :Int
var nose :Int
var voice :String
init(){
eyes =0
month =0
nose =0
voice ="voice"
}
func description() ->String {
return"\(eyes) eyes ; up to\(month) month"
}
}
class Cat: Animal {
overrideinit() {
super.init()
voice ="miao"
}
overridevar eyes:Int{
get{
returnsuper.eyes
}
set{
super.eyes =max(newValue,100)
}
}
}
var cat = Cat()
print(cat.voice)
cat.eyes =1000
print(cat.eyes)
若不想被继承重写,可在前面加final.
- Swift 类的继承
- swift - 类的继承
- 13、swift语言中类的继承
- Swift - 类的继承 & 访问权限
- Swift 类的继承 convenience required
- swift之类的继承
- swift的继承
- swift 类继承
- swift类声明、继承
- swift中Class的继承
- swift_023(Swift 的继承)
- swift学习笔记(四)关于类的继承
- 【iOS】Swift类的继承、构造方法、析构器等复习
- Swift中类的初始化器与继承
- 【iOS】Swift类的继承、构造方法、析构器等复习
- swift学习之路(十六)类的继承
- swift 2.0 类中的继承
- [Swift]继承
- D1.将数字转换成汉字
- 二进制
- iOS 苹果官方Demo合集
- java类型转换防止报异常
- linux 下的硬链接与软链接
- swift - 类的继承
- MVC,MVP 和 MVVM
- 创业网友提问:
- [Android异常]listview(含上拉加载,上拉加载)下拉刷新报数组越界异常
- 十六进制
- 单例模式
- Servlet监听器
- 判断一个点是否在三角形内
- 小于65536的数的十六进制数