python oop 继承_令人困惑的Python OOP继承

我是Python语言的新手。我只有10天的经验。当我开始学习时,没有困难,但是当我达到“面向对象的概念”,尤其是“继承的”时,这会使我放慢速度。

我的一些有关“继承是子类的背景知识可以获取父类的所有特征和行为,换句话说-父类的数据和方法”。好的,我将展示使我与两个程序混淆的概念。他们俩都得到相同的结果,所以人们为什么有所不同。

第一的:

class Parent():

parentdata = 0

def __init__(self):

pass

def getParentData(self):

return Parent.parentdata

def setParentData(self, setdata):

Parent.parentdata = setdata

class Child(Parent):

childdata = 0

def __init__(self):

pass

def getChildData(self):

return Child.childdata

def setChildData(self, setdata):

Child.childdata = setdata

child = Child()

print "Default Child's Data is :" + str(child.getChildData())#getting 0

child.setChildData(3)

print "After Adding Child's Data is :"+ str(child.getChildData()) # getting 3

print "Default Parent's Data is:"+ str(child.getParentData())# getting 0

child.setParentData(1)

print "After Adding Parent's Data is :"+str(child.getParentData())# getting 1

第二:

class Parent():

parentdata = 0

def __init__(self):

pass

def getParentData(self):

return Parent.parentdata

def setParentData(self, setdata):

Parent.parentdata = setdata

class Child(Parent):

childdata = 0

def __init__(self):

#super(Child, self).__init__()

#super(Child, self).__init__(self, self)

Parent.__init__(self)

def getChildData(self):

return Child.childdata

def setChildData(self, setdata):

Child.childdata = setdata

child = Child()

print "Default Child's Data is :" + str(child.getChildData())#getting 0

child.setChildData(3)

print "After Adding Child's Data is :"+ str(child.getChildData()) # getting 3

print "Default Parent's Data is:"+ str(child.getParentData())# getting 0

child.setParentData(1)

print "After Adding Parent's Data is :"+str(child.getParentData())# getting 1

并且也请指导我,如何使用SomebodySuper()实例Parent.__init__(self),其中的Super方法以及某些方法作为我的方法。我不清楚这两个不同之处。

在这两个程序中-我没有__init__用作构造函数。如果我要__init__用作将数据添加到类的数据(childdata,parentdata)中,如何在其中插入参数Parent.__init__(self)以及它们的两个def __init__(self):方法?

解决方案

您的示例似乎表现出预期。您已经使用类级别的数据设置了两个类-也就是说,每个Parent将共享相同的parentData值,每个孩子将共享相同的childData值。因为你的方法名称不共享的,你的“孩子”的实例将独立“父”的行为,除了呼吁setParentData对孩子将推动价值为所有父母的状态-但不是所有的孩子。

更常见的示例如下所示:

class Parent(object):

shared_state = 0

def __init__(self):

self.private_state = 1

def get_shared(self):

return self.shared_state

def get_private(self)

return self.private_state

class Child(Parent):

shared_state = 2

def __init__(self):

Parent.__init__(self)

self.private_state = 3

#in this case calling the Parent constructor is not really needed but it's good practice

p = Parent()

p.get_shared()

> 0

p.get_private()

> 1

new_p = Parent()

new_p.get_shared()

> 0

c = Child()

c.get_shared()

> 2

c.get_private()

> 3

在此示例中,所有父级共享变量“ shared_state”,但是每个父级都具有独立于其他父级的自己的private_state副本。同样,每个孩子共享一个孩子的shared_state的副本-与父对象的共享状态不同。

实质上,当子类中出现相同的名称(对于诸如shared_state之类的类变量)或方法(诸如get_private或get_shared之类)时,它将覆盖原始版本。这就是为什么在Child上调用get_shared()返回2的原因,即Child.shared_state。在您的原始代码中,您从Chold显式调用了Parent.parentData,因此您从Parent获得了值。

继承性的核心思想是使共享功能易于共享,并且仅针对特定需求编写新的类或方法:

class Vehicle(object):

def __init__(self, name, passengers):

self.Passengers = passengers

self.Name = name

def go(self):

print "%n is going with %i passengers" % (self.Name, self.Passengers)

def get_name(self):

print self.Name

class Car(Vehicle):

def go(self):

print "%n is driving along with %i passengers" % (self.Name, self.Passengers)

class Boat(Vehicle):

def go(self):

print "%n is sailing along with %i passengers" % (self.Name, self.Passengers)

class Truck(Car):

def deliver(self):

print "%n is delivering cargo" % self.Name

Here the subclasses can all 'go' but they do it in different ways -- the nice thing is that code using them doesn't need to know what they are, it just calls go on them and they go in their own ways. The Truck 'goes' like a car and has a method for doing deliveries as well which the Boat and Car don't have. All of the classes share the 'get_name' method so it does not have to be rewritten -- it's there for free in all derived classes. So as you see you can share functions and data, replace them with different functions or data that have the same names (so outside code can get at them the same way) or add new functions and data.

You might want to check out Head First Python for a more thorough intro to these ideas. And of course there's a lot here on SO.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值