假设我有一个名为Car的父类,其中包含一些属性和一个实际上什么都不做的方法。
class Car {
var brandName : String
var yearOfCreation : Int
var color : String
var numberOfDoors : Int
func manipulateWithCar() {
}
init(brandName : String, yearOfCreation : Int, color : String, numberOfDoors : Int) {
self.brandName = brandName
self.yearOfCreation = yearOfCreation
self.color = color
self.numberOfDoors = numberOfDoors
}
}
我有两个名为SportCar和TankerCar的子课程。它们具有独特的属性,而且父类的方法也被重写,现在它可以做一些事情。
class SportCar : Car {
var turbo : Bool
var nitrousOxideEngine : Bool
override func manipulateWithCar() {
print("Putting turbo on your car...")
turbo = true
}
init(brandName: String, yearOfCreation: Int, color: String, numberOfDoors: Int, turbo : Bool, nitrousOxideEngine : Bool) {
self.turbo = turbo
self.nitrousOxideEngine = nitrousOxideEngine
super.init(brandName: brandName, yearOfCreation: yearOfCreation, color: color, numberOfDoors: numberOfDoors)
}
}
此方法还有一个枚举。这并不重要,但你只想把它保留在作业中。
enum TankerForm {
case cilindrical
case quadratic
}
class TankerCar : Car{
var tankerFilledSize : Int
var tankerForm : TankerForm
init(brandName : String, yearOfCreation : Int,
color : String, numberOfDoors : Int, tankerVolum : Int, tankerForm : TankerForm) {
self.tankerForm = tankerForm
self.tankerFilledSize = tankerVolum
super.init(brandName: brandName, yearOfCreation: yearOfCreation, color: color, numberOfDoors: numberOfDoors)
}
override func manipulateWithCar() {
print("Loading 200 gallon of oil")
tankerFilledSize = 200
}
}
创建一些对象......
var kia = Car(brandName: "Rio", yearOfCreation: 2015, color: "Green",
numberOfDoors: 4)
var toyota = Car(brandName: "Corolla", yearOfCreation: 2015, color:
"Red", numberOfDoors: 4)
var lamborgini = SportCar(brandName: "Aventador", yearOfCreation: 2018,
color: "Black", numberOfDoors: 2, turbo: false, nitrousOxideEngine:
false)
var mazeratti = SportCar(brandName: "Ghibli", yearOfCreation: 2017,
color: "Red", numberOfDoors: 4, turbo: true, nitrousOxideEngine: false)
var kamaz = TankerCar(brandName: "Kamaz", yearOfCreation: 2007, color:
"Yellow", numberOfDoors: 2, tankerVolum: 0, tankerForm: .cilindrical)
var belaz = TankerCar(brandName: "Belaz", yearOfCreation: 2003, color:
"White", numberOfDoors: 2, tankerVolum: 0, tankerForm: .quadratic)
使用对象创建数组...
var carrsArray : [Car] = [kia, toyota]
var sportCarsArray : [SportCar] = [lamborgini, mazeratti]
var tankerCarsArray : [TankerCar] = [kamaz, belaz]
使用for循环打印Car类对象的所有属性...
for value in carrsArray {
print("This car is \(value.brandName)")
print("The color is \(value.color)")
print("The car has \(value.numberOfDoors) doors")
print("The year of creation is \(value.yearOfCreation)")
print(" ")
}
使用for循环打印SportCar类对象的所有属性...
for value in sportCarsArray {
print("This car is \(value.brandName)")
print("The color is \(value.color)")
print("The car has \(value.numberOfDoors) doors")
print("The year of creation is \(value.yearOfCreation)")
if value.nitrousOxideEngine == true {
print("This car has Nitrous Oxide Engine")
} else {
print("There is no Nitrous Oxide Engine on this car.")
}
if value.turbo == true {
print("This car has turbo")
} else {
print("There is no turbo in this car.")
}
print(" ")
}
使用for循环打印TankerCar类对象的所有属性...
for value in tankerCarsArray {
print("This car is \(value.brandName)")
print("The color is \(value.color)")
print("The car has \(value.numberOfDoors) doors")
print("The year of creation is \(value.yearOfCreation)")
print("The tanker is filled with \(value.tankerFilledSize) lb of oil.")
if value.tankerForm == .cilindrical {
print("This car has cilindrical tanker form.")
} else {
print("This car has quadratic tanker form.")
}
print(" ")
}
现在的问题。有没有办法打印不同类的所有属性?我试图创建一个包含三个数组的新数组:
var allClassesArrays = [carrsArray, sportCarsArray, tankerCarsArray]
但是在迭代期间,它能够获得父类的所有属性。
答案 0 :(得分:0)
这就是反思。您可以向Car
类添加属性/方法,列出当前类的所有属性以及超类的属性。当此属性/方法动态应用于子类时,它将提供您要查找的内容。
我们需要Mirror
的扩展,它从Martin R's answer获取当前实例的所有属性及其超类的属性:
extension Mirror {
func toDictionary() -> [String: Any] {
var dict = [String: Any]()
// Attributes of this class
for attr in self.children {
if let propertyName = attr.label {
dict[propertyName] = attr.value
}
}
// Attributes of superclass
if let superclass = self.superclassMirror {
for (propertyName, value) in superclass.toDictionary() {
dict[propertyName] = value
}
}
return dict
}
}
class Car: CustomDebugStringConvertible {
// ...
var debugDescription: String {
let dict = Mirror(reflecting: self).toDictionary()
return dict.map { "\($0.key) = \($0.value)" }.joined(separator: "\n")
}
}
用法:
var allClassesArrays = [carrsArray, sportCarsArray, tankerCarsArray]
for car in allClassesArrays.flatMap({ $0 }) {
print(car.debugDescription, terminator: "\n\n")
}
您当然不必将Car
符合CustomDebugStringConvertible
或将财产命名为debugDescription
。我发现它们很方便,因为当您键入debugDescription
时调试器将使用po car
。