我有一个班级:
class WeaponItems {
var name: String
var index: Int
var price: Int
var weaponPower: Int = 0
var attackSpeed: Double = 0
var criticalChance: Double = 0
var criticalDamage: Double = 0
init(name: String, index: Int, price: Int){
self.name = name
self.index = index
self.price = price
}
var weaponBlade = WeaponItems(name: "Weapon Blade", index: 0, price: 300)
weaponBlade.weaponPower = 15
// Error: expected declaration
var swiftShooter = WeaponItems(name: "Swift Shooter", index: 1, price: 300)
swiftShooter.attackSpeed = 0.2
// Error: expected declaration
var minionsFoot = WeaponItems(name: "Minions Foot", index: 3, price: 300)
minionsFoot.criticalChance = 0.1
minionsFoot.criticalDamage = 0.1
// Error: expected declaration
}
我需要为每个项目设置weaponPower
attackSpeed
...,这样做会出错。
我发现答案说你必须使用一个函数来修改实例的值,但我觉得这会使代码变得复杂,我该怎么设置它的值呢?
使用函数的关注点是它将实例创建和值设置过程分开,使代码更难维护。
答案 0 :(得分:3)
你应该为类
中的所有weaponItem添加属性那么你的类实现应该是这样的
class WeaponItems {
var name: String
var index: Int
var price: Int
var weaponPower: Int = 0
var attackSpeed: Double = 0
var criticalChance: Double = 0
var criticalDamage: Double = 0
var weaponBlade:WeaponItems{
get {
let tempWeaponBlade = WeaponItems(name: "Weapon Blade", index: 0, price: 300)
tempWeaponBlade.weaponPower = 15
return tempWeaponBlade
}
}
var swiftShooter:WeaponItems{
get {
let tempSwiftShooter = WeaponItems(name: "Swift Shooter", index: 1, price: 300)
tempSwiftShooter.attackSpeed = 0.2
return tempSwiftShooter
}
}
var minionsFoot:WeaponItems{
get {
let tempMinionsFoot = WeaponItems(name: "Minions Foot", index: 3, price: 300)
tempMinionsFoot.criticalChance = 0.1
tempMinionsFoot.criticalDamage = 0.1
return tempMinionsFoot
}
}
init(name: String, index: Int, price: Int){
self.name = name
self.index = index
self.price = price
}
}
答案 1 :(得分:1)
在您的情况下,如果代码会编译,要使用预先创建的weaponBlade
,您必须创建两个WeaponItems
实例,如下所示:
let balde = WeaponItems(name: "", index: 0, price: 0).weaponBlade
这不是最好的解决方案,即使在你的情况下根本不编译(你可以阅读@ gnasher729的评论来理解原因)。
我建议你创建它们的静态实例
extension WeaponItems {
static var Blade: WeaponItems {
get {
let blade = WeaponItems(name: "The name", index: 1, price: 300)
blade.weaponPower = 15
return blade
}
}
}
现在您可以通过键入WeaponItems.Blade
来使用该刀片,或者如果它类型很容易推断如下:
let blade: WeaponItems = .Blade
答案 2 :(得分:1)
如果您希望拥有默认属性值,您还希望在创建实例时自定义,我建议您在初始化程序中使用default parameter values。这将允许您使用初始化时已知的实际属性值创建WeaponItems
类的新实例,而不是在之后立即更改的某些默认值。
我也非常同意with Luca D'Alberti - 您不需要预先设定的武器'成为实例属性。因为他们不依赖于任何实例状态,或者对于给定实例是唯一的 - 它们应该是static
。
例如:
class Weapon {
static var blade : Weapon {
return Weapon(name: "Blade", index: 0, price: 300, power: 15)
}
static var swiftShooter : Weapon {
return Weapon(name: "Swift Shooter", index: 1, price: 300, attackSpeed: 0.2)
}
static var minionsFoot : Weapon {
return Weapon(name: "Minions Foot", index: 3, price: 300, criticalChance: 0.1, criticalDamage: 0.1)
}
var name: String
var index: Int
var price: Int
var power: Int
var attackSpeed: Double
var criticalChance: Double
var criticalDamage: Double
init(name: String, index: Int, price: Int,
power: Int = 0, attackSpeed: Double = 0,
criticalChance: Double = 0, criticalDamage: Double = 0) {
self.name = name
self.index = index
self.price = price
self.power = power
self.attackSpeed = attackSpeed
self.criticalChance = criticalChance
self.criticalDamage = criticalDamage
}
}
的
let blade = Weapon.blade
let swiftShooter : Weapon = .swiftShooter
答案 3 :(得分:0)
您可以尝试使用以下代码。
lazy var weaponBlade:WeaponItems = {
let _weaponBlade = WeaponItems(name: "Weapon Blade", index: 0, price: 300)
_weaponBlade.weaponPower = 15
return _weaponBlade
}()
请参阅此link以了解有关延迟变量的更多信息。