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

这篇博客介绍了Python面向对象编程的基础知识,包括类和对象的概念,`__init__`方法用于对象初始化,类的方法如`sayHi`,以及对象的生命周期方法`__del__`。此外,还讲解了继承的概念,通过`Teacher`和`Student`类展示了如何从`SchoolMember`类继承并添加特定属性和方法。
摘要由CSDN通过智能技术生成

今天学习的内容:

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__方法:在对象消逝的时候被调用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值