今天学习的内容:
1.面向对象编程的概念
1)面向对象的三个基本特征:封装、继承、多态
2)类和对象是面向对象编程的2个主要方面。类使用class关键字创建。类的域和方法被列在一个缩进块中。
2.类
[root@reed 0504]# cat simpleclass.py
#!/usr/bin/python
class Person:
pass #an empty block
# print 'my name is reed'
p=Person()
print p
[root@reed 0504]# ./simpleclass.py
3.对象的方法
[root@reed 0504]# cat method.py
#!/usr/bin/python
class Person:
def sayHi(self):
print 'hello,my name is reed'
p=Person()
p.sayHi()
[root@reed 0504]# ./method.py
hello,my name is reed
4.__init__方法:__init__方法在类的一个对象被建立时,马上运行。这个方法可以用来对你的对象做一些你希望的 初始化 。注意,这个名称的开始和结尾都是双下划线。
[root@reed 0504]# cat class_init.py
#!/usr/bin/python
class Person:
def __init__(self,name):
self.name=name
def sayHi(self):
print 'hello,my name is',self.name
p=Person('reed')
p.sayHi()
[root@reed 0504]# ./class_init.py
hello,my name is reed
5.类与对象的方法
[root@reed 0504]# cat objvar.py
#!/usr/bin/python
#filename:objvar.py
class Person:
population=0
def __init__(self,name):
self.name=name
print '(initializing %s)'%self.name
#when this person is created,he/she adds to the population
Person.population+=1
def __del__(self):
print '%s says bye'%self.name
self.__class__.population-=1
if self.__class__.population==0:
print 'i am the last one'
else:
print 'there are still %d people left'%Person.population
def sayHi(self):
print 'hi,my name is %s'%self.name
def howMany(self):
if Person.population==1:
print 'i am the only person here'
else:
print 'we have %d persons here'%Person.population
swaroop=Person('swaroop')
swaroop.sayHi()
swaroop.howMany()
print '--------------'
reed=Person('reed')
reed.sayHi()
reed.howMany()
print '--------------'
deer=Person('deer')
deer.sayHi()
deer.howMany()
print '--------------'
swaroop.sayHi()
swaroop.howMany()
[root@reed 0504]# ./objvar.py
(initializing swaroop)
hi,my name is swaroop
i am the only person here
--------------
(initializing reed)
hi,my name is reed
we have 2 persons here
--------------
(initializing deer)
hi,my name is deer
we have 3 persons here
--------------
hi,my name is swaroop
we have 3 persons here
deer says bye
there are still 2 people left
swaroop says bye
there are still 1 people left
reed says bye
i am the last one
6.继承:继承完全可以理解成类之间的类型和子类型的关系
[root@reed 0504]# cat inherit.py
#!/usr/bin/python
#filename=inherit.py
class SchoolMember:
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 'Initalized 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('Mrs.Reed',40,30000)
t.tell()
s=Student('Lemon',22,75)
s.tell()
[root@reed 0504]# ./inherit.py
Initialized SchoolMember:Mrs.Reed
Initalized Teacher:Mrs.Reed
Name:"Mrs.Reed"Age:"40"
Salary:"30000"
Initialized SchoolMember:Lemon
Initialized Student:Lemon
Name:"Lemon"Age:"22"
Marks:"75"
7__del__方法:在对象消逝的时候被调用