#coding:UTF-8
#类 开始
class Person:
pass
p=Person()
print p
'''
结果:
<__main__.Person instance at 0x029C47B0>
'''
#类 结束
#=====================================
#对象的方法 开始
class Person2:
def sayHi(self):
print 'hello,world!'
p=Person2()
p.sayHi()
'''
结果:
hello,world!
'''
#对象的方法 结束
#==========================================
#使用__init__方法开始
'''
__init__方法在类的一个对象被建立时,马上运行。
这个方法可以用来对你的对象做一些你希望的 初始化 。注意,这个名称的开始和结尾都是双下划线。
'''
class Person3:
def __init__(self,name):
self.name=name
def sayHi(self):
print 'hello,my name is',self.name
p=Person3('blue')
p.sayHi()
'''
结果:
hello,my name is blue
解析:
这里,我们把__init__方法定义为取一个参数name(以及普通的参数self)。
在这个__init__里,我们只是创建一个新的域,也称为name。
注意它们是两个不同的变量,尽管它们有相同的名字。点号使我们能够区分它们。
最重要的是,我们没有专门调用__init__方法,只是在创建一个类的新实例的时候,
把参数包括在圆括号内跟在类名后面,从而传递给__init__方法。这是这种方法的重要之处。
hello,my name is blue
现在,我们能够在我们的方法中使用self.name域。这在sayHi方法中得到了验证。
给C++/Java/C#程序员的注释
__init__方法类似于C++、C#和Java中的 constructor
'''
#使用__init__方法结束
#===============================================
#类与对象的方法 开始
'''
记住,你只能使用self变量来参考同一个对象的变量和方法。这被称为 属性参考 。
在这个程序中,我们还看到docstring对于类和方法同样有用。
我们可以在运行时使用Person.__doc__和Person.sayHi.__doc__来分别访问类与方法的文档字符串。
就如同__init__方法一样,还有一个特殊的方法__del__,它在对象消逝的时候被调用。
对象消逝即对象不再被使用,它所占用的内存将返回给系统作它用。
在这个方法里面,我们只是简单地把Person.population减1。
当对象不再被使用时,__del__方法运行,但是很难保证这个方法究竟在 什么时候 运行。
如果你想要指明它的运行,你就得使用del语句,就如同我们在以前的例子中使用的那样。
给C++/Java/C#程序员的注释
Python中所有的类成员(包括数据成员)都是 公共的 ,所有的方法都是 有效的 。
只有一个例外:如果你使用的数据成员名称以 双下划线前缀 比如__privatevar,
Python的名称管理体系会有效地把它作为私有变量。
这样就有一个惯例,如果某个变量只想在类或对象中使用,就应该以单下划线前缀。
而其他的名称都将作为公共的,可以被其他类/对象使用。
记住这只是一个惯例,并不是Python所要求的(与双下划线前缀不同)。
同样,注意__del__方法与 destructor 的概念类似。
'''
class Person4:
population=0
def __init__(self,name):
self.name=name
print '(Initializing %s)' % self.name
Person4.population+=1
def __del__(self):
print '%s says bye.' % self.name
Person4.population-=1
if Person4.population==0:
print 'I am the last one.'
else:
print 'There are still %d people left.' % Person4.population
def sayHi(self):
print 'Hi, my name is %s.' % self.name
def howMany(self):
if Person4.population==1:
print"I am the only person here."
else:
print'We have %d persons here.' % Person4.population
name1=Person4('name1')
name1.sayHi()
name1.howMany()
print '\n'
name2=Person4('name2')
name2.sayHi()
name2.howMany()
print '\n'
name1.sayHi()
name1.howMany()
'''
结果:
(Initializing name1)
Hi, my name is name1.
I am the only person here.
(Initializing name2)
Hi, my name is name2.
We have 2 persons here.
Hi, my name is name1.
We have 2 persons here.
'''
#类与对象的方法 结束
#===================================================
#使用继承 开始
class SchoolMember:
'''Represents any school member.'''
def __init__(self,name,age):
self.name=name
self.age=age
print '(Initialized SchoolMember: %s)' % self.name
def tell(self):
print 'Name:%s Age:%s' %(self.name,self.age)
class Teacher(SchoolMember):
def __init__(self,name,age,salary):
SchoolMember.__init__(self,name,age)
self.salary=salary
print '(Initialized Teacher:%s)' % self.name
def tell(self):
SchoolMember.tell(self)
print 'Salary: %d' % self.salary
class Student(SchoolMember):
def __init__(self,name,age,marks):
SchoolMember.__init__(self,name,age)
self.marks=marks
print '(Initialized Student: %s)' %self.name
def tell(self):
SchoolMember.tell(self)
print 'Marks: %d' % self.marks
t=Teacher('Mr li',40,3000)
s=Student('blue bird',22,99)
members=[t,s]
for member in members:
member.tell()
'''
结果:
(Initialized SchoolMember: Mr li)
(Initialized Teacher:Mr li)
(Initialized SchoolMember: blue bird)
(Initialized Student: blue bird)
Name:Mr li Age:40
Salary: 3000
Name:blue bird Age:22
Marks: 99
'''
#使用继承 结束
python学习笔记——6_面向对象
最新推荐文章于 2020-11-21 04:30:04 发布