前端之家收集整理的这篇文章主要介绍了
swift学习笔记之继承,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
class StepCounter {
var totalSteps:Int=0 {
willSet(newTotalSteps) {
print("新的值:\(newTotalSteps)")
}
didSet(old) {
if (totalSteps > old) {
print("与原来比增减了\(totalSteps - old)个值")
}
}
}
}
class stepCounter:StepCounter {
}
let counter=stepCounter()
counter.totalSteps=10
counter.totalSteps=100
class Sorce {
var english:Int=130
var chinese:Int=50
var math:Int=90
subscript(index:Int)->Int {
get {
switch index {
case 0:
return english
case 1:
return chinese
case 2:
return math
default:
return 0
}
}
}
}
class childSorce: Sorce{
}
let myscore=childSorce()
print(myscore[0])
print(myscore[1])
class NewClass {
var value:Int {
return 100
}
class var classValue:Int {
return 200
}
}
class NewClass1: NewClass {
override var value:Int {
return 333
}
override class var classValue:Int {
return 666
}
}
let cls = NewClass1()
print(cls.value)
print(NewClass1.classValue)
class Car {
var speed:Double=0.00
}
class AutomicCar: Car {
var gear = 1
override var speed:Double {
didSet {
gear = Int(speed)
print(gear)
}
}
}
let automic = AutomicCar()
automic.speed=34.00
automic.speed=6886.00
class childSorce111: Sorce{
var value=0
override subscript(index:Int)->Int {
let count = index-value
return count
}
}
class ClassA {
}
class ClassA1:ClassA {
var movie:String="hello"
}
class ClassA2:ClassA {
var movie:String="hello"
}
class ClassA3:ClassA {
var movie:String="hello"
}
class ClassA4:ClassA {
var movie:String="hello"
}
class ClassB:ClassA {
var movie:String="hello"
}
class ClassC {
}
let b = ClassB()
if b is ClassB {
print("true")
}
else {
print("false")
}
let a = ClassA()
if a is ClassC {
print("true")
}
else {
print("false")
}
let bb=ClassB()
let clsbb = bb as ClassB
print("clsbb.movie==\(clsbb.movie)")
let library1 = [ClassA1(),ClassA2(),ClassA3(),ClassA4()]
for item in library1 {
let cls = item as ClassA
print(cls)
}
for item:AnyObject in library1 {
let cls = item as! ClassA
print(cls)
}
@H_928_
403@
原文链接:https://www.f2er.com/swift/323652.html