为了理解 Genie 中的类继承,我创建了两个类(Kitten 和 Puppy),它们应该从 Pet 类继承属性。目的是让 minou 喵喵叫和 duke 吠叫,但是 _name 似乎超出了子类的范围。如何将此属性传递给子类?
这是代码:
[indent=4]
// Experimenting with classes in Genie
class Pet
_name:string
construct ( name:string? )
_name = name
class Kitten : Pet
def meow()
print self._name + " meowed!"
class Puppy : Pet
def bark()
print self._name + " barked!"
init
var minou = new Kitten("Minou")
var duke = new Puppy("Duke")
minou.meow()
duke.bark()
错误信息是:
Test78.gs:16.15-16.24: error: Access to private member `Pet._name' denied
print self._name + " meowed!"