Python面向对象(1)
2020-07-03 17:37
52 查看
1. 面向对象(只写了和java、c++不同之处)
1.1 面向对象介绍
1.2 面向对象的三大特征
-
继承
java --> 单继承,可以使用接口 c++/python --> 多继承
-
封装
-
多态
2. 类和对象
2.1 定义类和实例化
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
2.2 属性
2.2.1 类属性
- 类属性是该类所有实例化对象所共有的,相当于c++/java中的静态属性
- 类属性的定义能够在类内(方法外)、类之外(ClassName.classAttributeName=value)生成,也可以在方法中动态生成(ClassName.classAttributeName(不能在return语句中生成)) 无slots
- 类属性在类中只能通过类名来访问,通过对象来访问会为其生成一个同名的实例属性;在类外如果没有同名的实例属性类名访问和对象访问都可以;如果有同名的实例属性,只能够类名访问,对象访问到的为其实例属性
2.2.2 实例属性
- 实例属性为该对象所有,每个对象的实例属性储存的地址不同
- 实例属性的定义只能在方法内,可以在类中动态生成(self.classAttributeName(不能在return语句中生成)),还可以在类外生成(instance.classAttributeName=value) 无slots
- 类中类外实例属性的访问只能通过对象来访问,但是使用对象访问到的不一定都是实例属性(类外)
2.3 实例方法
-
魔法方法,其中最常用的是
__init__
构造方法,构造方法不能重载,因为在python中方法的形参没有直接定义其变量数据类型;__del__(self)
析构方法;__str__(self,...)
直接打印类的时候调用该方法,如果不重写,返回的是该对象的描述class Student: def __str__(self): return "直接打印该类的实例对象时返回" Jack=Student() print(Jack) --> '直接打印该类的实例象时返回'
-
实例方法可以在类中生成也可以在类外有定义好 的函数生成(instance.method=function(定义好的))
-
类中类外实例方法的调用只能由实例调用,但是使用实例调用到到不一定都是实例方法(类外)
-
每一个方法都要传入
self
参数,传入实例对象本身
2.4 公有、保护和私有
对于公有和保护,可以在类外直接访问;保护和其它语言不同,java/c++都是可以被继承不能在类外访问,python中可以被继承、在类外访问、可以跨包,但是如果被保护的是顶级的函数或变量,这些函数和变量不允许被import到其他包中
2.5判断类的实例
-
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
2.6 继承
2.6.1 单继承
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
2.6.2 多继承
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 # 如果继承多个父类中,有相同的方法,则在子类中调用的是先继承的那个父类的方法
2.6.3 子类的判断
-
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
2.7 多态
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
2.8 类的高级特征
2.8.1 @property
-
@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,可以传值 # 不规范,不建议这样写
2.8.2 __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'
2.9 对属性和方法动态生成再次总结
-
无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.生成静态方法 # 在类中,类调用类属性和静态方法,实例调用实例属性和实例方法 # 在类外,类属性可以类调用和实例调用 # 在类外,实例属性可以实例调用 # 在类外,静态方法可以类调用和实例调用 # 在类外,实例方法可以实例调用 # 一般,实例属性、实例方法比类属性、静态方法优先 # 在类外,类方法可以类访问和实例访问
2.10 类的静态方法和类方法
2.10.1 静态方法
-
@staticmethod
方法不传self -
可以用类名来动态生成
-
类中只能类访问,类外可以类访问和实例访问
class Student: @staticmethod def hello(): print("hello") Jack=Student() Jack.hello() --> hello Student.hello() --> hello
2.10.2 类方法
-
@classmethod
-
不能动态生成
-
类访问和实例访问
class Student: name="Jack" @classmethod def hello(cls): return cls.name Jack=Student() Jack.hello() --> 'Jack' Student.hello() --> 'Jack'
相关文章推荐
- python基础(面向对象(对象魔法三大特征)、多态)
- python-面向对象-14-私有方法
- 简明 Python 教程 / 面向对象的编程 / 类与对象的方法
- Python3 面向对象(基础篇)
- Python面向对象(一)
- 简明python教程 --C++程序员的视角(五):面向对象的编程
- 【Python基础】Python面向对象 - 7 - Mixin & MRO
- Python3.5——面向对象——类的继承和多态
- python-面向对象精髓-组合-封装
- 预习任务:Python课堂实录-面向对象-02(类的方法)
- Python面向对象01
- Python-面向对象
- python基础(面向对象(自定义property设置成员属性修饰符))
- Python全栈开发【面向对象】
- Python-Day6 面向对象
- python_面向对象(初始)
- python | 面向对象 | 异常
- Python学习 Day 7 面向对象 类和实例 访问限制
- python入门系列12―——面向对象
- 【Python】面向对象--类的特殊成员方法