Swift不支持多重继承,如何实现?
Swift does not support multiple inheritance, How could be achieved?
以下代码:
class City {
var cityId : String?
var cityName : String?
}
class Town {
var townid : String?
var townName : String?
}
class Address : City , Town {
var house : String?
var street: String?
}
生成编译时错误:
Address.swift:38:24: Multiple inheritance from classes 'City' and 'Town'
我怎样才能解决他这样的问题?遵循什么方法?
备注:如果您不确定继承是否适合您的情况,我建议检查。
但是,正如您提到的,Swift 不 支持多重继承。你应该做的是让 Address
class 采用 protocols;您还可以添加 协议扩展 :
You can extend an existing type to adopt and conform to a new
protocol, even if you do not have access to the source code for the
existing type. Extensions can add new properties, methods, and
subscripts to an existing type, and are therefore able to add any
requirements that a protocol may demand.
意味着您可以将默认 values/behaviors 实现到协议 methods/properties。
参考你的例子,它应该类似于:
protocol City { }
extension City {
var cityId: String {
return "Default City ID"
}
var cityName: String {
return "Default City Name"
}
// adding method:
func doSomething() {
print("Do Something!!")
}
}
protocol Town { }
extension Town {
var townid: String {
return "Default Town ID"
}
var townName: String {
return "Default Town Name"
}
}
class Address:City, Town {}
输出:
let address = Address()
print(address.cityId) // Defaut City ID
print(address.cityName) // Defaut City Name
print(address.townid) // Default Town ID
print(address.townName) // Default Town Name
address.doSomething() // Do Something!!
另外,如果你想给一个方法添加一些额外的工作(类似于super.doSomething()
),你可以这样做:
class Address:City, Town {
func doSomething() {
(self as City).doSomething()
print("Addtional Job!!")
}
}
输出:
let address2 = Address()
address2.doSomething()
// Do Something!!
// Addtional Job!!
更进一步:
有关详细信息,请查看 Protocols and Extensions 文档。
此外,您可能还想观看 Protocol-Oriented Programming in Swift Apple Session。
看来你想多了。尝试使用更多组合而不是继承。地址不应继承自 City
。为什么?因为从逻辑上讲,地址不是城市的一种。相反,城市定义是地址的一部分:
class Address {
var city: City?
var town: Town?
var house : String?
var street: String?
}
以下代码:
class City {
var cityId : String?
var cityName : String?
}
class Town {
var townid : String?
var townName : String?
}
class Address : City , Town {
var house : String?
var street: String?
}
生成编译时错误:
Address.swift:38:24: Multiple inheritance from classes 'City' and 'Town'
我怎样才能解决他这样的问题?遵循什么方法?
备注:如果您不确定继承是否适合您的情况,我建议检查
但是,正如您提到的,Swift 不 支持多重继承。你应该做的是让 Address
class 采用 protocols;您还可以添加 协议扩展 :
You can extend an existing type to adopt and conform to a new protocol, even if you do not have access to the source code for the existing type. Extensions can add new properties, methods, and subscripts to an existing type, and are therefore able to add any requirements that a protocol may demand.
意味着您可以将默认 values/behaviors 实现到协议 methods/properties。
参考你的例子,它应该类似于:
protocol City { }
extension City {
var cityId: String {
return "Default City ID"
}
var cityName: String {
return "Default City Name"
}
// adding method:
func doSomething() {
print("Do Something!!")
}
}
protocol Town { }
extension Town {
var townid: String {
return "Default Town ID"
}
var townName: String {
return "Default Town Name"
}
}
class Address:City, Town {}
输出:
let address = Address()
print(address.cityId) // Defaut City ID
print(address.cityName) // Defaut City Name
print(address.townid) // Default Town ID
print(address.townName) // Default Town Name
address.doSomething() // Do Something!!
另外,如果你想给一个方法添加一些额外的工作(类似于super.doSomething()
),你可以这样做:
class Address:City, Town {
func doSomething() {
(self as City).doSomething()
print("Addtional Job!!")
}
}
输出:
let address2 = Address()
address2.doSomething()
// Do Something!!
// Addtional Job!!
更进一步:
有关详细信息,请查看 Protocols and Extensions 文档。
此外,您可能还想观看 Protocol-Oriented Programming in Swift Apple Session。
看来你想多了。尝试使用更多组合而不是继承。地址不应继承自 City
。为什么?因为从逻辑上讲,地址不是城市的一种。相反,城市定义是地址的一部分:
class Address {
var city: City?
var town: Town?
var house : String?
var street: String?
}