Python是面向对象的语言,面向对象最重要的三个优点有:

  • 多态:多态使对象具备不同的行为方式。(可以认为声明了接口,但是实现方式可能多样)
  • 封装:封装是对全局作用域中隐藏多余信息的原则(创建对象,隐藏属性,不用担心对全局产生影响,也不用担心全局影响对象属性)
  • 继承:继承使得代码可以复用,而且使得类之间有超类和子类的概念

创建类

类的可见级别在类中分别定义了一个公共的方法greet,保护方法_protectmethod,私有方法__privatemethod。如同通过Tab键进行函数定义控制,通过下划线_可以表明方法的可见级别。

__metaclass__= type
class Person:
def setName(self,name):
self.name=name
def getName(self,name):
return self.name
def greet(self):
print "hello, world! I'm %s." % self.name
def _protectmethod(self):
print '_protectmethod'
def __privatemethod(self):
print '__privatemethod'
foo=Person()
foo.setName('foo')
foo.greet()
hello, world! I'm foo.
foo.name
'foo'
func=foo.greet
func()
hello, world! I'm foo.
foo.setName('notfoo')
func()
hello, world! I'm notfoo.

使用单下划线定义的方法不会被带星号的import语句导入(from module import *)

foo._protectmethod()
_protectmethod

使用双下划线定义的方法,外部无法访问,实际上是方法名发生了变化

foo.__privatemethod()
---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

<ipython-input-17-3353363f5043> in <module>()
----> 1 foo.__privatemethod() AttributeError: 'Person' object has no attribute '__privatemethod'

在知道方法名改变的规则之后,我们依然可以调用私有方法。

foo._Person__privatemethod()
__privatemethod

类属性

class MemberCounter:
memNum=0
def init(self):
MemberCounter.memNum+=1
m1=MemberCounter()
m1.init()
m2=MemberCounter()
m2.init()
print MemberCounter.memNum
2
m1.myname='M1'
print m1.myname
M1

类的继承

class Men(Person):
def greet(self):
print "hello, world I'm Mr %s" % self.name m=Men()
m.setName('Andrew')
m.greet()
hello, world I'm Mr Andrew

多个超类

class Singer():
def sing(self):
print 'singing'
class MenSinger(Men,Singer):
def greetandsing(self):
self.greet()
self.sing()
ms=MenSinger()
ms.setName('Adrew')
ms.greetandsing()
hello, world I'm Mr Adrew
singing

接口和内省

hasattr(ms,'greet')
True
hasattr(ms,'bark')
False

Python学习笔记:05类的更多相关文章

  1. python学习笔记4_类和更抽象

    python学习笔记4_类和更抽象 一.对象 class 对象主要有三个特性,继承.封装.多态.python的核心. 1.多态.封装.继承 多态,就算不知道变量所引用的类型,还是可以操作对象,根据类型 ...

  2. Python学习笔记 - day7 - 类

    类 面向对象最重要的概念就是类(Class)和实例(Instance),比如球类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都拥有相同的方法,但各自的数据可能不同.在Python中,定义类 ...

  3. python学习笔记1-元类__metaclass__

    type 其实就是元类,type 是python 背后创建所有对象的元类   python 中的类的创建规则: 假设创建Foo 这个类 class Foo(Bar): def __init__(): ...

  4. Python学习笔记12—类

    典型的类和调用方法: #!/usr/bin/env Python # coding=utf-8 __metaclass__ = type #新式类 class Person: #创建类 def __i ...

  5. Python 学习笔记 - 10.类(Class) 1

    定义 Python 的 Class 比较特别,和我们习惯的静态语言类型定义有很大区别. 1. 使用一个名为 __init__ 的方法来完成初始化.2. 使用一个名为 __del__ 的方法来完成类似析 ...

  6. Python学习笔记008_类_对象_继承_组合_类相关的BIF

    # 对象 = 属性 + 方法>>> # Python中的类名约定以大写字母开始>>> # tt = Turtle() 这就是创建类实例的方法,其它语言用new ,它 ...

  7. python学习笔记(七) 类和pygame实现打飞机游戏

    python中类声明如下: class Student(object): def __init__(self, name, score): self.name = name self.score = ...

  8. Python学习笔记:类

    类可以将数据与函数封装起来,用一个例子解释,先定义一个类: class athlete: def __init__(self,a_name,a_dob=None,a_times=[]): self.n ...

  9. Python 学习笔记16 类 - 导入

    我们在编码的过程中,可能会给对象添加越来越多的功能,即使我们使用了继承,也不可避免的使文件越来越臃肿. 为了避免这种情况, Python允许将对象存储在模块中,并且可以在其他模块中进行导入. 其实这和 ...

  10. Python 学习笔记15 类 - 继承

    我们在编程的过程中,并非都是要重头开始.比如其他人已经有现成的类,我们可以使用其他找人编写的类.术语称之为: 继承. 当一个类继承例外一个类时,它可以获得这个类的所有属性和方法:原有的类称之为 父类, ...

随机推荐

  1. 2013=10=19 ENGLISH 翻译

    数据结构习题及答案 严蔚敏_课后习题答案 http://www.doc88.com/p-243584884293.html 273089354 随着女性获得平等权力的趋势,女性日渐增长的经济权力以及为 ...

  2. poj1611 并查集

    题目链接:http://poj.org/problem?id=1611 #include <cstdio> #include <cmath> #include <algo ...

  3. Google Guava官方教程(中文版)地址

    Google Guava官方教程(中文版) http://ifeve.com/google-guava/ 瓜娃啊瓜娃

  4. STUCTS LABLE ‘S BENEFIT

    {LJ?Dragon}[注]Struts标签的三个好处 RELATED LINKS 0.UTF-8 有无BOM的区别 UTF-8 BOM 06. 几款网页数据抓取软件 SOFTWARE_INTRODU ...

  5. [Angular 2] Using Pipes to Filter Data

    Pipes allow you to change data inside of templates without having to worry about changing it in the ...

  6. String.Split()函数

    我们在上次学习到了 String.Join函数(http://blog.csdn.net/zhvsby/archive/2008/11/28/3404704.aspx),当中用到了String.SPl ...

  7. javascript 鼠標拖動功能

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. 【IOS】 XML解析和xml转plist文件(GDataXML)

    iOS对于XML的解析有系统自带的SDK--NSXMLParser,鄙人愚拙,只会用GDataXML进行解析,这里仅介绍GData的使用.(以下图为例) 1.对于一个xml文件,先读取出来 NSDat ...

  9. PHPMailer中文说明

    PHPMailer中文说明 A开头: $AltBody --属性出自:PHPMailer ::$AltBody文件:class.phpmailer .php说明:该属性的设置是在邮件正文不支持HTML ...

  10. dir()函数:罗列出参数所有的功能列表

    #coding=utf-8import sysprint dir(sys)#罗列出参数中所有的功能列表sys.__doc__#调用参数中的函数 #dir()函数扩展展详解python中dir()函数不 ...