- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Python中的面向对象编程详解(下)由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
继承 。
继承描述了基类的属性如何“遗传”给派生类。一个子类可以继承它的基类的任何属性,不管是数据属性还是方法。 创建子类的语法看起来与普通(新式)类没有区别,一个类名,后跟一个或多个需要从其中派生的父类
。
class Child(Parent): # define child class 定义子类 def childMethod(self): print 'calling child method' 。
。
继承 。
不同于Java,python的子类继承父类后,会把父类的所有的方法,包括构造器init()也继承下来. 。
。
def func(self): print "call parent func" 。
class Child(Parent): def __init__(self): print "init Child class instance" 。
child = Child() child.func() 。
。
super 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是如果使用多继承,会涉及到查找顺序(MRO)、重复调用(钻石继承)等种种问题。语法如下 。
。
super继承只能用于新式类,用于经典类时就会报错。 新式类:必须有继承的类,如果没什么想继承的,那就继承object 经典类:没有父类,如果此时调用super就会出现错误:『super() argument 1 must be type, not classobj』 实例 。
。
class Child(Parent): def __init__(self): super(Child, self).__init__() self.data = 100 。
def main(): child = Child() print "phone is: ", child.phone print "address is: ", child.address print "data is: ", child.data 。
if __name__ == '__main__': main() 。
。
子类只要重新定义一个与父类的方法同名的方法,就可以重写覆盖父类的方法. 子类只要把上例父类的func(self)重写就行了. 。
。
child = Child() child.func() 。
。
同 C++一样,Python 允许子类继承多个基类。但一般不推荐用多重继承.语法如下
。
class Mother(): def __init__(self): print "init Mother instance" 。
class Child(Father, Mother): pass 。
。
issubclass() 。
布尔函数判断一个类是另一个类的子类或子孙类。它有如下语法
。
布尔函数在判定一个对象是否是另一个给定类的实例时,非常有用。它有如下语法
。
●hasattr() 它的目的就是为了决定一个对象是否有一个特定的属性,一般用于访问某属性前先作一下检查。 ●getattr()和setattr() ●getattr()和 setattr()函数相应地取得和赋值给对象的属性.
●delattr() 删除特定的属性 。
实例 。
。
child = Child() print "has data attr?", hasattr(child, 'data') 。
print "delete attr" delattr(child, 'data') 。
print "has data attr?", hasattr(child, 'data') 。
print "set data attr to 200" setattr(child, 'data', 200) print "data attr is: ", getattr(child, 'data') 。
。
私有化 。
Python没有像Java那样实现真正的封装,只是用双划线和单划线实现私有化. 。
●双划线 防止外部访问.如在func前加双划线,可以防止包括子类的实例的访问. 。
最后此篇关于Python中的面向对象编程详解(下)的文章就讲到这里了,如果你想了解更多关于Python中的面向对象编程详解(下)的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我的一位教授给了我们一些考试练习题,其中一个问题类似于下面(伪代码): a.setColor(blue); b.setColor(red); a = b; b.setColor(purple); b
我似乎经常使用这个测试 if( object && object !== "null" && object !== "undefined" ){ doSomething(); } 在对象上,我
C# Object/object 是值类型还是引用类型? 我检查过它们可以保留引用,但是这个引用不能用于更改对象。 using System; class MyClass { public s
我在通过 AJAX 发送 json 时遇到问题。 var data = [{"name": "Will", "surname": "Smith", "age": "40"},{"name": "Wil
当我尝试访问我的 View 中的对象 {{result}} 时(我从 Express js 服务器发送该对象),它只显示 [object][object]有谁知道如何获取 JSON 格式的值吗? 这是
我有不同类型的数据(可能是字符串、整数......)。这是一个简单的例子: public static void main(String[] args) { before("one"); }
嗨,我是 json 和 javascript 的新手。 我在这个网站找到了使用json数据作为表格的方法。 我很好奇为什么当我尝试使用 json 数据作为表时,我得到 [Object,Object]
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我听别人说 null == object 比 object == null check 例如: void m1(Object obj ) { if(null == obj) // Is thi
Match 对象 提供了对正则表达式匹配的只读属性的访问。 说明 Match 对象只能通过 RegExp 对象的 Execute 方法来创建,该方法实际上返回了 Match 对象的集合。所有的
Class 对象 使用 Class 语句创建的对象。提供了对类的各种事件的访问。 说明 不允许显式地将一个变量声明为 Class 类型。在 VBScript 的上下文中,“类对象”一词指的是用
Folder 对象 提供对文件夹所有属性的访问。 说明 以下代码举例说明如何获得 Folder 对象并查看它的属性: Function ShowDateCreated(f
File 对象 提供对文件的所有属性的访问。 说明 以下代码举例说明如何获得一个 File 对象并查看它的属性: Function ShowDateCreated(fil
Drive 对象 提供对磁盘驱动器或网络共享的属性的访问。 说明 以下代码举例说明如何使用 Drive 对象访问驱动器的属性: Function ShowFreeSpac
FileSystemObject 对象 提供对计算机文件系统的访问。 说明 以下代码举例说明如何使用 FileSystemObject 对象返回一个 TextStream 对象,此对象可以被读
我是 javascript OOP 的新手,我认为这是一个相对基本的问题,但我无法通过搜索网络找到任何帮助。我是否遗漏了什么,或者我只是以错误的方式解决了这个问题? 这是我的示例代码: functio
我可以很容易地创造出很多不同的对象。例如像这样: var myObject = { myFunction: function () { return ""; } };
function Person(fname, lname) { this.fname = fname, this.lname = lname, this.getName = function()
任何人都可以向我解释为什么下面的代码给出 (object, Object) 吗? (console.log(dope) 给出了它应该的内容,但在 JSON.stringify 和 JSON.parse
我正在尝试完成散点图 exercise来自免费代码营。然而,我现在只自己学习了 d3 几个小时,在遵循 lynda.com 的教程后,我一直在尝试确定如何在工具提示中显示特定数据。 This code
我是一名优秀的程序员,十分优秀!