子类是新类型吗?
Is a subclass a new type?
如果我子class一个名为Animal
的class并将其命名为Dog
,那么classDog
是一个新类型吗?
给出这两个定义
class Animal { }
class Dog: Animal { }
是的,Animal
和Dog
有不同的类型,你可以检查一下运行这个代码
type(of: Animal()) == type(of: Dog())
希望对您有所帮助。
这里已经有了很好的答案。但为了记录一些额外的想法:
在Swift中,每个class都是一个named type:
A named type is a type that can be given a particular name when it’s defined. Named types include classes, structures, enumerations, and protocols.
A subclass 是 class,它也是一种类型。所以 Dog
和 Animal
是两种类型。 subclass Dog
是更通用的 Animal
的特化。所以 Dog
可以做 Animal
可以做的一切(可能与其他动物不同)并且可以做更多:
class Animal {
func reactToStranger() {
print("Run away")
}
func sleep() {
print ("zzzz")
}
}
class Dog:Animal {
override func reactToStranger() { // do like an animal, but differently
print("Bark ferocely", terminator:": ")
bark()
}
func bark() { // only dog can do this
print ("Ouaf Ouaf !!")
}
}
let pet1 = Animal() // type is Animal
let pet2 = Dog() // type is Dog which is a special kind of Animal
由于 Dog
是 Animal
的一种特殊类型(subclass),它可以在任何需要 Animal
的地方使用。让我们试试我们的两只宠物:
func Test(_ pet: Animal) {
print("Experiment with \(type(of: pet)) in presence of stranger", terminator:": ")
pet.reactToStranger()
}
Test(pet1) // pet1 is an Animal, so no surprise
Test(pet2) // pet2 is a Dog, but it can be used as an Animal
pet2.bark() // ok: pet2 is a Dog so it can also bark.
//pet1.bark() // not ok: pet1 is an Animal and animals in general do not bark.
pet1.sleep() // all animals can sleep
pet2.sleep() // dog can sleep since it is a special kind of animal (inheritance)
如果我子class一个名为Animal
的class并将其命名为Dog
,那么classDog
是一个新类型吗?
给出这两个定义
class Animal { }
class Dog: Animal { }
是的,Animal
和Dog
有不同的类型,你可以检查一下运行这个代码
type(of: Animal()) == type(of: Dog())
希望对您有所帮助。
这里已经有了很好的答案。但为了记录一些额外的想法:
在Swift中,每个class都是一个named type:
A named type is a type that can be given a particular name when it’s defined. Named types include classes, structures, enumerations, and protocols.
A subclass 是 class,它也是一种类型。所以 Dog
和 Animal
是两种类型。 subclass Dog
是更通用的 Animal
的特化。所以 Dog
可以做 Animal
可以做的一切(可能与其他动物不同)并且可以做更多:
class Animal {
func reactToStranger() {
print("Run away")
}
func sleep() {
print ("zzzz")
}
}
class Dog:Animal {
override func reactToStranger() { // do like an animal, but differently
print("Bark ferocely", terminator:": ")
bark()
}
func bark() { // only dog can do this
print ("Ouaf Ouaf !!")
}
}
let pet1 = Animal() // type is Animal
let pet2 = Dog() // type is Dog which is a special kind of Animal
由于 Dog
是 Animal
的一种特殊类型(subclass),它可以在任何需要 Animal
的地方使用。让我们试试我们的两只宠物:
func Test(_ pet: Animal) {
print("Experiment with \(type(of: pet)) in presence of stranger", terminator:": ")
pet.reactToStranger()
}
Test(pet1) // pet1 is an Animal, so no surprise
Test(pet2) // pet2 is a Dog, but it can be used as an Animal
pet2.bark() // ok: pet2 is a Dog so it can also bark.
//pet1.bark() // not ok: pet1 is an Animal and animals in general do not bark.
pet1.sleep() // all animals can sleep
pet2.sleep() // dog can sleep since it is a special kind of animal (inheritance)