继承
java --> 单继承,可以使用接口
c++/python --> 多继承
封装
多态
class Student:
#类属性,可以通过对象访问和类访问
count=0 #公有类属性
_a=0 #保护类属性,和c++、java不同,可以直接在类外访问到
__b=0 #私有类属性,不能直接在类外访问到
#Python不能对构造函数进行重载,也不能对函数进行重载,原因是因为python没有对形参
#规定数据类型
def __init__(self,age,name,id):
count+=1
self._age=age #保护方法,和c++、java不同,可以直接在类外访问到
self.name=name #公有方法
self.__id=id #私有方法
#实例方法
def setClassCount(self,count):
Student.count=count #公有属性count
def setObjectCount(self,count):
self.count=count #公有属性count
def setClassGrade(self,grade):
Student.grade=grade #公有属性grade
def setObjectGrade(self,grade):
self.grade=grade #公有属性grade
def setB(self,b):
Student.__b=b
def getB(self):
return Student.__b
def _protected(self): #保护实例方法,类外可以访问
print("Protected")
def __private(self): #私有实例方法,类外不能访问
print("private")
Jack=Student(20,"Jack","111111111111111111") #实例化对象
#可以直接获取就可以直接赋值;不能直接使用,就在类中写接口使用
Student.count --> 1
Jack.count --> 1
Student._a --> 0
Jack._a --> 0
Student.__b --> AttributeError
Jack.__b --> AttributeError
Jack.setClassCount(12)
Jack.count --> 12
Jack.setObjectCount(5)
Jack.count --> 5
Student.count --> 12
Jack.setB(9)
Jack.getB() --> 9
Student._Student__b --> 9
Jack.name --> 'Jack'
Jack._age --> 20
Jack.__id --> AttributeError
Jack._Student__id --> '111111111111111111'
Jack._protected() --> Protected
Jack.__private() --> AttributeError
Jack._Student__private() --> private
Student.new="new"
Student.new --> 'new'
Jack.new --> 'new'
Jack.hello="hello"
Jack.hello --> 'hello'
def introduce():
print("It is introduce")
Jack.introduce=introduce
Jack.introduce() --> It is introduce
魔法方法,其中最常用的是__init__
构造方法,构造方法不能重载,因为在python中方法的形参没有直接定义其变量数据类型;__del__(self)
析构方法;__str__(self,...)
直接打印类的时候调用该方法,如果不重写,返回的是该对象的描述
class Student:
def __str__(self):
return "直接打印该类的实例对象时返回"
Jack=Student()
print(Jack) --> '直接打印该类的实例象时返回'
实例方法可以在类中生成也可以在类外有定义好 的函数生成(instance.method=function(定义好的))
类中类外实例方法的调用只能由实例调用,但是使用实例调用到到不一定都是实例方法(类外)
每一个方法都要传入self
参数,传入实例对象本身
对于公有和保护,可以在类外直接访问;保护和其它语言不同,java/c++都是可以被继承不能在类外访问,python中可以被继承、在类外访问、可以跨包,但是如果被保护的是顶级的函数或变量,这些函数和变量不允许被import到其他包中
Bool isinstance(InstanceName,ClassName)
class Student:
def __init__(self):
pass
class Teacher:
def __init__(self):
pass
stu=Student()
isinstance(stu,Student) --> True
isinstance(stu,Teacher) --> False
class People:
def __init__(self,age,name):
print("People")
self.age=age
self.name=name
def introduce(self):
print("Introduce.......")
class Student(People):
def __init__(self,age,name,grade):
super(Student,self).__init__(age,name)
# super().__init__(age,name)
# People.__init__(self,age,name)
self.grade=grade
Jack=Student(20,"Jack","A") --> People
Jack.introduce() --> Introduce.......
##########################################
class People:
def __init__(self,age,name):
print("People")
self.age=age
self.name=name
def introduce(self):
print("Introduce.......")
class Student(People):
def __init__(self,grade): #不像c++、java一样要调用
self.grade=grade
def introduce(self): # 重写了父类的方法
print("I am a student")
Jack=Student("A")
Jack.introduce() --> I am a student
class People:
def __init__(self,age,name):
print("People")
self.age=age
self.name=name
def introduce(self):
print("Introduce.......")
class Man:
sex="male"
def __init__(self):
pass
def introduce(self):
print("I am a man")
####################################
class Student(People,Man):
def __init__(self,age,name):
People.__init__(self,age,name)
#如果需要可以调用父类的多个继承方法
Jack=Student(20,"Jack") --> People
Jack.sex --> 'male'
Jack.introduce() --> Introduce.......
#####################################
class Student(Man,People):
def __init__(self,age,name):
People.__init__(self,age,name)
#如果需要可以调用父类的多个继承方法
Jack=Student(20,"Jack") --> People
Jack.sex --> 'male'
Jack.introduce() --> I am a man
# 如果继承多个父类中,有相同的方法,则在子类中调用的是先继承的那个父类的方法
Bool issubclass(subclass,father_class)
class People:
def __init__(self,age,name):
print("People")
self.age=age
self.name=name
def introduce(self):
print("Introduce.......")
class Student(People):
def __init__(self,age,name,grade):
super(Student,self).__init__(age,name)
self.grade=grade
issubclass(Student,People) --> True
issubclass(People,Student) --> False
class People:
#自动生成默认无参构造方法
def introduce(self):
print("I am a people")
class Student(People):
#自动生成默认无参构造方法
def introduce(self):
print("I am a student")
class Teacher(People):
#自动生成默认无参构造方法
def introduce(self):
print("I am a teacher")
# 不同对象使用同一方法,得到的结果不同
People().introduce() --> I am a people
Student().introduce() --> I am a student
Teacher().introduce() --> I am a teacher
@property
(描述符)将类的方法当属性来使用
class Student:
def __init__(self,name,age):
self.name=name
self.__age=age
def set_age(self,age):
if age>150 and age<0:
print("Inputed age is wrong")
else:
self.__age=age
def get_age(self):
return self.__age
Jack=Student("Jack",20)
Jack.get_age() --> 20
Jack.set_age(21)
Jack.get_age() --> 21
############################
class Student:
def __init__(self,name,age):
self.name=name
self.__age=age
@property #必须在@age.setter之前
def age(self):
return self.__age
@age.setter
def age(self,age):
if age>150 and age<0:
print("Inputed age is wrong")
else:
self.__age=age
Jack=Student("Jack",20)
Jack.age --> 20
Jack.age=21
Jack.age -->21
# 然后直接调用函数的使用方式不可用
###########################
# 这种写法也可以
class Student:
def __init__(self,name,age):
self.name=name
self.__age=age
@property #必须在@age.setter之前
def get_age(self):
return self.__age
@get_age.setter
def set_age(self,age):
if age>150 and age<0:
print("Inputed age is wrong")
else:
self.__age=age
Jack=Student("Jack",20)
Jack.get_age --> 20
Jack.set_age=21
Jack.get_age -->20
# @property将get_age变成属性,方法失效
# @get_age.setter将设置成get_age的setter,可以传值
# 不规范,不建议这样写
__slots__
为指定的类设置一个静态的属性列表,为属性少的类节约内存空间
slots只针对于实例属性和实例方法
class People:
__slots__=("age",) #实例属性
def __init__(self,age):
self.age=age
class Student(People):
__slots__=("grade",) #扩充,如果不扩充,父类的也不生效
def __init__(self,age):
People.__init__(self,age)
#有slots之后属性和方法不能随意添加
#实例不合逻辑
Jack=Student(20)
Jack.age --> 20
Jack.grade="A"
Jack.grade --> 'A'
Jack.hello="hello" --> AttributeError
def introduce():
print("hello")
Jack.introduce=introduce --> AttributeError
Student.hello="hello"
Student.hello --> 'hello'
无slots时,可以随便动态生成实例属性和实例方法,类内类外都可以生成
有slots时,生成实例属性和实例方法受到限制,只能够生成Tuple中的,对类属性方法和静态方法不生效
类方法无法动态生成
class Test:
name="Jack" #1.生成类属性
@staticmethod #4.生成静态方法
def hello(self):
print("hello")
def __init__(self,name): #3.生成实例方法
Test.age=12 #1.生成类属性
self.name=name #2.生成实例属性
@classmethod #5.生成类方法
def introduce(cls): #cls为类
print("introduce...........")
return cls.name
Test.a=1 #1.生成类属性
Jack=Test("Jack")
Jack.b=2 #2.生成实例属性
def a():
print("a")
Jack.a=a #3.生成实例方法
def b():
print("b")
Test.b=b #4.生成静态方法
# 在类中,类调用类属性和静态方法,实例调用实例属性和实例方法
# 在类外,类属性可以类调用和实例调用
# 在类外,实例属性可以实例调用
# 在类外,静态方法可以类调用和实例调用
# 在类外,实例方法可以实例调用
# 一般,实例属性、实例方法比类属性、静态方法优先
# 在类外,类方法可以类访问和实例访问
@staticmethod
方法不传self
可以用类名来动态生成
类中只能类访问,类外可以类访问和实例访问
class Student:
@staticmethod
def hello():
print("hello")
Jack=Student()
Jack.hello() --> hello
Student.hello() --> hello
@classmethod
不能动态生成
类访问和实例访问
class Student:
name="Jack"
@classmethod
def hello(cls):
return cls.name
Jack=Student()
Jack.hello() --> 'Jack'
Student.hello() --> 'Jack'