类的继承python 简明_[简明python教程]学习笔记2014-05-04
今天學習的內容:
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__方法:在對象消逝的時候被調用
總結
以上是生活随笔為你收集整理的类的继承python 简明_[简明python教程]学习笔记2014-05-04的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 框架写mysql插入为空_学习sprin
- 下一篇: 阿里云香港防ddos攻击吗安全吗(阿里云