日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

类的继承python 简明_[简明python教程]学习笔记2014-05-04

發布時間:2023/12/3 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 类的继承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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。