Python学习笔记:05类
类
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类的更多相关文章
- python学习笔记4_类和更抽象
python学习笔记4_类和更抽象 一.对象 class 对象主要有三个特性,继承.封装.多态.python的核心. 1.多态.封装.继承 多态,就算不知道变量所引用的类型,还是可以操作对象,根据类型 ...
- Python学习笔记 - day7 - 类
类 面向对象最重要的概念就是类(Class)和实例(Instance),比如球类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都拥有相同的方法,但各自的数据可能不同.在Python中,定义类 ...
- python学习笔记1-元类__metaclass__
type 其实就是元类,type 是python 背后创建所有对象的元类 python 中的类的创建规则: 假设创建Foo 这个类 class Foo(Bar): def __init__(): ...
- Python学习笔记12—类
典型的类和调用方法: #!/usr/bin/env Python # coding=utf-8 __metaclass__ = type #新式类 class Person: #创建类 def __i ...
- Python 学习笔记 - 10.类(Class) 1
定义 Python 的 Class 比较特别,和我们习惯的静态语言类型定义有很大区别. 1. 使用一个名为 __init__ 的方法来完成初始化.2. 使用一个名为 __del__ 的方法来完成类似析 ...
- Python学习笔记008_类_对象_继承_组合_类相关的BIF
# 对象 = 属性 + 方法>>> # Python中的类名约定以大写字母开始>>> # tt = Turtle() 这就是创建类实例的方法,其它语言用new ,它 ...
- python学习笔记(七) 类和pygame实现打飞机游戏
python中类声明如下: class Student(object): def __init__(self, name, score): self.name = name self.score = ...
- Python学习笔记:类
类可以将数据与函数封装起来,用一个例子解释,先定义一个类: class athlete: def __init__(self,a_name,a_dob=None,a_times=[]): self.n ...
- Python 学习笔记16 类 - 导入
我们在编码的过程中,可能会给对象添加越来越多的功能,即使我们使用了继承,也不可避免的使文件越来越臃肿. 为了避免这种情况, Python允许将对象存储在模块中,并且可以在其他模块中进行导入. 其实这和 ...
- Python 学习笔记15 类 - 继承
我们在编程的过程中,并非都是要重头开始.比如其他人已经有现成的类,我们可以使用其他找人编写的类.术语称之为: 继承. 当一个类继承例外一个类时,它可以获得这个类的所有属性和方法:原有的类称之为 父类, ...
随机推荐
- iTerm2 + oh my zsh代替mac自带的bash shell
使用Solarized dark配色方案 需要字体menlo for powerline oh-my-zsh主题使用agnoster,这个主题默认的路径是全路径,当路径很长的时候,就会占很长的空间,可 ...
- Majority Element II——LeetCode
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- 微软Azure Services Bus中的工作流
在Azure Services Platform上对于工作流服务的支持,一直是我很感兴趣的内容.当然也是疑问 比较多的领域.鉴于这方面的资料太少,所以今天就从AzureServicesKit中的一个D ...
- 《程序设计中的组合数学》——polya计数
我们在高中的组合数学中常常会碰到有关涂色的问题,例如:用红蓝两种颜色给正方形的四个顶点涂色,会有几种不同的方案.在当时,我们下意识的认为,正方形的四个顶点是各不相同的,即正方形是固定的.而实际上我们知 ...
- Marzoni(玛佐尼)意大利顶级西服面料之一_HollandandSherry_新浪博客
Marzoni(玛佐尼)意大利顶级西服面料之一_HollandandSherry_新浪博客 Marzoni(玛佐尼)意大利顶级西服面料之一 (2013-01-08 17:30:04) 转载▼
- 在javascript中使用媒体查询media query
由于需要,我们可能会在js中用到一些media query,从而针对不同的分辨率做一些操作. //全兼容的 事件绑定 and 阻止默认事件 var EventUtil = { //Notice: ty ...
- Spring Boot 启动原理分析
https://yq.aliyun.com/articles/6056 转 在spring boot里,很吸引人的一个特性是可以直接把应用打包成为一个jar/war,然后这个jar/war是可以直接启 ...
- 【下载分】C语言for循环语句PK自我活动
想了解自己C语言for语句的掌握程度吗?敢和自己PK较量一番吗?參加"C语言for循环语句PK自我活动",仅仅要成绩70分以上.就可赢得CSDN下载分. 12道题目题库动态读取,每 ...
- 两个有序数组的第n大数
两个有序数组,各自含有n个元素,求第n大的元素 1.顺序遍历两个数组,计数变量k统计出现的第k小元素,时间复杂度为O(n) 代码例如以下: int getmid(int a[],int b[],int ...
- [Javascript] Intro to the Web Audio API
An introduction to the Web Audio API. In this lesson, we cover creating an audio context and an osci ...