跟Java和C++类似,Python也是一门面向对象的语言。
- 类
类(Class): 用来描述具有相同的属性和方法的对象的集合。它定义了该集合中每个实例所共有的属性和方法。
类语法的格式
class ClassName:
类的属性定义
类的方法
(1)类的属性
一般在类内部定义类的属性,如Person
类,通常会定义姓名name
,年龄age
等基础属性。定义方式与定义变量相同:var = ""
。
将属性名以双下划线开头可将属性定义为私有属性,如__privateattr = 0
,表示其只能在类内部访问以self.
的方式访问,类的实例(实例)无法访问。
(2)类的方法
在类的内部,使用 def
关键字来定义一个方法,与一般函数定义不同,类方法必须包含参数self
, 且为第一个参数,self
代表的是类的实例。
def function(self):
类的方法
与私有属性同理,将方法名以双下划线开头可将方法定义为私有方法,如def __func(self):
,表示表示其只能在类内部访问以self.
的方式访问,类的实例(实例)无法访问。
一般创建实例的同时需要初始化实例,因此一般都会为类定义一个构造方法名为__init__()
,同样也是第一个参数必须为self
,在创建类的实例时,会自动调用该构造方法。
def __init__(self,name):
self.name = name
(3)类的实例
Python 创建类的实例语法格式:
object=ClassName(argu1,argu2,...)
以定义矩形类为例,熟悉类的使用:
class Rectangle:
#定义类的属性
width = 0
hight = 0
__color = ""#定义类的私有属性
##定义构造函数
def __init__(self,width,hight,color):
self.width = width
self.hight = hight
self.__color = color
##定义类的方法
##私有方法
def __getarea(self):
return self.width*self.hight
##定义类的方法
def printme(self):
print("width=%d,hight=%d,area=%d,color=%s"%(self.width,self.hight,self.__getarea(),self.__color))#类内部可以调用类的私有方法及私有属性
#创建类的实例,自动调用__init__()构造方法
r=Rectangle(3,4,"green")
#访问实例的属性
print("width:",r.width)
#访问实例的方法
r.printme()
#结果
#width: 3
#width=3,hight=4,area=12,color=green
#访问实例的私有属性和私有方法
print("color:%s","area:%d"%(r.__color,r.__getarea()))
#结果出现错误:AttributeError: 'Rectangle' object has no attribute '__color'
#因为__color和__getArea()是私有属性和私有方法,所以类的实例无法访问,只有类内部能访问。
- 继承
Python支持类的继承,同时也支持多继承,其派生类定义如下:
class DerivedClass(BaseClass1, BaseClass2,..., BaseClassN):
类的属性
类的方法
请注意基类的排列顺序,当子类调用自身没有定义的方法,也并没有指定具体的基类名称时,python会从左到右依次查找继承的基类中是否包含该方法,直到找到就停止,否则报错。
熟悉继承的使用:
#定义动物类
class animal:
def eat(self):
print("animal is eatting")
#定义people类
class people:
def eat(self):
print("people is eatting")
def speak(self):
print("people is speaking")
#定义student类,继承people类和animal类
class student(people,animal):
pass
#定义teacher类,继承animal类和people类
class teacher(animal,people):
pass
#student类的实例s
#student类没有定义构造方法,创建类的实例时调用默认的无参构造方法
s = student()
s.eat()
#teacher类的实例t
t = teacher()
t.speak()
#结果
#people is eatting
#people is speaking
当Student类的实例s调用eat()方法时,因为Student类继承父类的顺序是(People,Animal)
,所以先找People类是否有eat()方法,有就调用People的eat()方法,打印出people is eatting
。
当Teacher类的实例t调用speak()方法时,Teacher类本身没有定义speak()方法,所以搜索其父类(Animal,People)
,第一个父类Animal没有搜索到speak()方法,继续搜索第二个父类People,能搜索到speak()方法,于是调用People的speak()方法,打印people is speaking
。
- 多态
(1)先了解方法重写,子类继承父类,会继承父类的所有方法,当父类方法无法满足需求,可在子类中定义一个同名方法覆盖父类的方法,这就叫方法重写。当子类的实例调用该方法时,优先调用子类自身定义的方法,因为它被重写了。
class People:
def speak(self):
print("people is speaking")
class Student(People):
#方法重写。重写父类的speak方法
def speak(self):
print("student is speaking")
class Teacher(People):
pass
#Student类的实例s
s = Student()
s.speak()
#Teacher类的实例t
t = Teacher()
t.speak()
#结果
#student is speaking
#people is speaking
因为子类Student重写了父类People的speak()方法,当Student类的对象s调用speak()方法,优先调用Student的speak方法,而Teacher()类没有重写People的speak方法,所以会t.speak()
调用父类的speak()方法,打印people is speaking
。
(2)多态特性,多态意味着变量并不知道引用的对象是什么,根据引用对象的不同表现不同的行为方式。
#代码块A。定义Animal类,Dog类,Cat类
class Animal:
def eat(self):
print("animal is eatting")
class Dog(Animal):
def eat(self):
print("dog is eatting")
class Cat(Animal):
def eat(self):
print("cat is eatting")
#代码块B。定义eatting_double方法,参数为Animal类型
def eatting_double(animal):
animal.eat()
animal.eat()
#代码块C。代码执行区,相当于main方法。
animal = Animal()
dog = Dog()
cat = Cat()
eatting_double(animal)
eatting_double(dog) #接收的参数必须是拥有eat方法的对象,否则执行报错
eatting_double(cat)
#结果
animal is eatting
animal is eatting
dog is eatting
dog is eatting
cat is eatting
cat is eatting
上述代码中,Dog类继承自Animal类,重写了Animal的eat法。代码块B定义了eatting_double法,代码块C调用了该方法,多态的特性可以使得当我们再定义一个新的Animal子类——Cat类时,不需要修改eatting_double法的方法体,同样可以通过eatting_double(cat)来调用tiger的eat法,只需要Cat类正确地定义eat方法。
总结:子类继承父类,可以继承父类的所有方法,当父类的方法不满足需求时,还可以重写父类的方法,只需方法名称和基类的方法名称保持相同即可,另外子类还能定义自己特有的方法。