我试图将我的commercial()
类的Cars
方法包含在由我的finalNegotiation()
类的PriceNegotiation
方法执行的字符串插值中。有可能吗?
我试过使用.commercial()
,正如您在本文中看到的,我也试过使用super.commerical()
class Cars {
var make = ""
var model = ""
var year = 0
init(carMake make:String, carModel model:String, carYear year:Int) {
self.make = make
self.model = model
self.year = year
}
func commercial() {
print("This car is a \(year) \(make) \(model)")
}
}
class PriceNegotation: Cars {
var price:Double = 0
init(desiredBuyerPrice price:Double,carMake make:String, carModel model:String, carYear year:Int ) {
self.price = price
super.init(carMake: make, carModel: make, carYear: year)
}
func finalNegotiation() {
let dealerPrice = price * 1.5
print("Since \(super.commercial()) the asking price is \(dealerPrice)")
}
}
最佳答案
当调用“cc>”时,它打印文本并退出该函数。要实现您的目标,请使commercial()
函数包含一个返回值。这里有一个例子。
class Cars {
var make = ""
var model = ""
var year = 0
init(carMake make:String, carModel model:String, carYear year:Int) {
self.make = make
self.model = model
self.year = year
}
func commercial()->String {
return "This car is a \(year) \(make) \(model)"
}
}
class PriceNegotation: Cars {
var price:Double = 0
init(desiredBuyerPrice price:Double,carMake make:String, carModel model:String, carYear year:Int ) {
self.price = price
super.init(carMake: make, carModel: make, carYear: year)
}
func finalNegotiation() {
let dealerPrice = price * 1.5
let commercialOutput = commercial()
print("Since \(commercialOutput) the asking price is \(dealerPrice)")
}
}
这样做的目的是通过插值将
commercial()
函数acommercial()
的输出放入另一个字符串中。以前,函数没有返回任何内容,因此看起来好像函数不起作用。另一方面,这应该管用。如果没有请告诉我。