当前位置: 首页 > news >正文

wordpress和站点网页制作软件dreamweaver

wordpress和站点,网页制作软件dreamweaver,广州市技师学院,电商平台有哪些平台我是Python语言的新手。我只有10天的经验。当我开始学习时,没有困难,但是当我达到“面向对象的概念”,尤其是“继承的”时,这会使我放慢速度。我的一些有关“继承是子类的背景知识可以获取父类的所有特征和行为,换句话…

我是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.

http://www.lbrq.cn/news/2443357.html

相关文章:

  • 莱西网站建设公司企业网站建设方案
  • 历史价格查询百度seo关键词排名优化教程
  • 珠宝静态网站模板茂名seo顾问服务
  • 成都网上商城网站建设互联网推广招聘
  • 本网站建设广州网页推广公司
  • 网站设计方案及报价单湖南百度推广公司
  • 全球网站排名查询外贸软件
  • 算命网站建设开发沈阳seo按天计费
  • 织梦做的网站打包在dw修改友情链接的四个技巧
  • wordpress主题更改首页贵州seo技术查询
  • 网站标题写什么作用是什么网站推广优化价格
  • 网站建设合同封面网络营销是指
  • 个人网站免费模板seo外链友情链接
  • 学校网站模板html下载女孩子做运营是不是压力很大
  • 昆明定制化网站建设天津seo推广软件
  • 哈尔滨住房和城乡建设委员会网站百度app登录
  • 微信自己怎么弄小程序北京seo供应商
  • 酒泉如何做百度的网站经典软文广告
  • wordpress macos武汉seo网站优化排名
  • 如何建设一个商城网站搜索推广和信息流推广的区别
  • 做悬赏的网站网络营销专业是学什么的
  • 怎么建立一个网站csdn网络营销团队
  • 网站需要什么服务器seo企业培训班
  • 检测网站是用什么代码做的软件百度推广深圳分公司
  • 个人备案域名做企业网站广州网络推广定制
  • Wordpress自动回复评论昆明seo网站建设
  • 网站建设与熊掌号未来的关系百度游戏排行榜风云榜
  • 海原网站建设网站模板价格
  • 常州网站外包bing搜索引擎国内版
  • 信誉好的邯郸网站建设一份完整的营销策划方案
  • 图像处理:第二篇 —— 选择镜头的基础知识及对图像处理的影响
  • Python-初学openCV——图像预处理(三)
  • 【最新版】防伪溯源一体化管理系统+uniapp前端+搭建教程
  • ART配对软件使用
  • Linux 如何统计系统上各个用户登录(或者登出)记录出现的次数?
  • 深度学习(鱼书)day03--神经网络(后两节)