2014-01-23 48 views
0

我正在通过LPTHW,并在前42(http://learnpythonthehardway.org/book/ex43.html),我认为我对is-a有一个体面的把握,并且有一个概念,但是在研究演习中他要求我们给这些类添加列表和字典,以使这个更具功能的脚本。我正在考虑为个人或员工添加一个名单,包括狗,猫和字典,以便存储名称的值以及员工的薪水。我不知道如何做到这一点,使其功能,有人可以给我一个简单的例子,我想要做的,我会从那里建立完成剩下的事情。谢谢你,也低于代码...使用OOP创建名称列表

什么,我在得到的快速的例子...

class Animal(object): 
    pass 

## Dog is-a Animal 
class Dog(Animal): 
    def __init__(self, name): 
     ##Dog has-a name 
     self.name = name 
     self.doglist = [] 

Dog.doglist.append("Rover") 

当我做了以上我得到一个错误“AttributeError错误:类型的对象‘狗’没有属性doglist。

全码:

##Animal is-a object (yes, sort of confusing) look at the extra credit 
class Animal(object): 
    pass 

## Dog is-a Animal 
class Dog(Animal): 
    def __init__(self, name): 
     ##Dog has-a name 
     self.name = name 


##Cat is-a animal 
class Cat(Animal): 
    def __init__(self, name): 
     ##Cat has-a name 
     self.name = name 

##Person is-a object 
class Person(object): 
    def __init__(self, name): 
     ##Person has-a name 
     self.name = name 


     ##Person has-a pet 
     self.pet = None 


##Employee is-a Person 
class Employee(Person): 
    def __init__(self, name, salary): 
     ## not sure what this does 
     super(Employee, self).__init__(name) 
     ##Person has-a salary 
     self.salary = salary 

##Fish is a class 
class Fish(object): 
    pass 

##Salmon is-a fish and is a class 
class Salmon(Fish): 
    pass 

##Halibut is-a fish 
class Halibut(Fish): 
    pass 

##rover is-a Dog 
rover = Dog("Rover") 


##satan is-a Cat 
satan = Cat("Satan") 

##mary is a person 
mary = Person("Mary") 

##Mary has-a pet named satan 
mary.pet = satan 

##frank is-a employee that has-a salary of 120,000 
frank = Employee("Frank", 120000) 

##frank has-a pet rover(dog) 
frank.pet = rover 

##flipper is-a instance of Fish 
flipper = Fish() 

##crouse is-a instance of salmon 
crouse = Salmon() 

##harry is-a instance of Halibut 
harry = Halibut() 

print frank.pet.name 
+1

整个继承点是把它的属性放入类中,并在所有子类中使用它。为什么不把一个列表添加到'动物'和一个字典'人'?另外,我不确定你想要什么。这段代码不工作还是表现出意外?有什么是不是在做什么? – mojo

+0

不,它完美地工作。我确实想为'Animal'添加一个列表,并为'Person'添加一个字典,但我不知道如何正确格式化它。例如,我是否会简单地将'self.Animallist = []'放入动物在那里创建一个列表?我该如何追加到该列表中? 'self.Animallist.append ='randomname'',因为我认为我试过了,它没有用... – user3221870

+0

'append()'是一种方法而不是属性。使用它'list.append(thing_to_append)'。所以如果你使用'self.animal_list = []'',它就会工作。如果'cat'是'Cat()'的一个实例,所有你需要做的就是'cat.animal_list.append(cat_thing)' – IanAuld

回答

0
class Animal(object): 
    pass 

## Dog is-a Animal 
class Dog(Animal): 
    def __init__(self, name): 
     ##Dog has-a name 
     self.name = name 
     self.doglist = [] 

Dog.doglist.append("Rover") 

这不工作的原因是因为你还没有初始化的Dog对象。要纠正这一点,

dog = Dog('Fido') 
dog.doglist.append('Rover') 

当您设置__init__方法,你必须先初始化它访问内的财产的属性,否则它不存在。如果你想拥有所有Dog对象共享你可以在同一个列表:

class Dog: 
    dog_list = [] 

    def __init__(self, name): 
     self.name = name 

随后的方法之外声明

>>> Dog.dog_list.append('Fido') 
>>> Dog.dog_list 
['Fido'] 
>>> dog = Dog('Rover') 
>>> dog.dog_list 
['Fido'] 
>>> dog.dog_list.append('Rover') 
>>> dog.dog_list 
['Fido', 'Rover'] 
>>> Dog.dog_list 
['Fido', 'Rover'] 

属性将这个类的所有实例之间共享。

+0

这正是我寻找的解释类型!我会喜欢但我没有声望哈,非常感谢你我真的欣赏它,这真的帮助我把握这个概念 – user3221870

+0

这就是我们在这里!玩得开心学习 – IanAuld

+0

我刚刚添加评论只是为了再次说谢谢,你帮了我很多,真的很感激。 – user3221870