11.1 面向对象及其三大特性
面向对象程序设计(OOP Object Oriented Programming)作为一种新方法,其本质是以建立模型体现出来的抽象思维过程和面向对象的方法。模型是用来反映现实世界中事物特征的。任何一种模型都不可能反映客观事物的一切具体特征,只能对事物特征和变化规律的一种抽象,且在它所涉及的范围内更普遍、更集中、更深刻地描述客体的特征。
通过建立模型而达到的抽象是人们对客体认识的深化。
封装:
把对象的属性私有化,同时提供可以被外界访问的方法
继承:
使用已存在的类的定义,作为新的类的基础,新类在此基础上,添加属于自己的新的属性及方法,不能选择性的继承
多态:
不同子类对象调用相同的父类方法,产生不同的执行结果,可增加代码的调用灵活度
11.2 类和对象
类和对象是两种以计算机为载体的计算机语言的合称
对象是对客观事物的抽象,类是对对象的抽象,类是一种抽象的数据类型
class Person():
def say(self):
print("Hello World")
person=Person()
person.say()
11.3 构造函数
class Person():
#构造函数_init_(self)
def __init__(self):
print("Hello World")
person=Person()
class Person():
def __init__(self,name,age,height):
self.name=name
self.age=age
self.height=height
def say(self):
print("My name is %s ,I am %d years old and I am %d cm"%(self.name,self.age,self.height))
person=Person("Gulf",24,185)
person1=Person("Mew",30,183)
person.say()
person1.say()
11.4 类变量与实例变量
类变量定义在类中且在函数体之外,类变量通常不作为实例变量使用。类变量是在整个实例化的对象中是公用的
定义在方法中的变量,用self绑定到实例上,只作用于当前实例的类
类变量是对象公有、实例变量是对象私有。
访问实例变量
class Person():
def __init__(self,name,age,height):
self.name=name
person=Person("Gulf")
print(person.name)
访问实例变量和类变量
class Person():
#word类变量
total=0
word="We may not be the most beautiful couple, but we do cherish each other and love each other."
def __init__(self,name,age,height):
#name,age,height实例变量
#Person.total+=1 结果为1
#self._class_.total+=1 结果为1
self.name=name
self.age=age
self.height=height
def say(self):
print("My name is %s ,I am %d years old and I am %d cm"%(self.name,self.age,self.height))
person=Person("Gulf",24,185)
person1=Person("Mew",30,183)
person.say()
person1.say()
print(person.word)
如果实例变量名和类变量名相同,先访问实例变量
11.5 类方法与静态方法
类方法
class Person():
ask="Gulf,do you want to marry with Mew?"
@classmethod
def answers(cls):
cls.ask+="Yes,I do"
print(cls.ask)
Person.answers()
静态方法
class Person():
@staticmethod
def word():
print("Gulf and Mew will be together forever")
Person.word()
实例方法:与具体实例相关,操作实例变量
类方法:与类襄垣,操作类变量,可通过实例调用(不建议)
静态方法:与类和具体实例无关,相当于一个独立函数
11.6 访问限制
class Person():
def __init__(self,name,age):
self.name=name
self.__age=age
def say(self):
print(self.name,",his age is",self.__age)#self.__age可以访问
person=Person("Gulf",24)
person.say()
print(person.name)
print(person.__age)#self.__age不可以访问
输出错误:AttributeError: ‘Person’ object has no attribute ‘__age’
其中age在前面加两个下划线,表示私有变量,外部不能访问
怎么打破这种访问限制
class Person():
def __init__(self,name,age):
self.name=name
self.__age=age
def say(self):
print(self.name,",his age is",self.__age)
person=Person("Gulf",24)
person.say()
person.__age=25
print(person.__dict__)
print(person.name)
print(person.__age)
输出:
Gulf ,his age is 24
{‘name’: ‘Gulf’, '_Person__age’: 24, ‘__age’: 25}
Gulf
25
为什么__init__()可以直接调用
把__age改为__age__
class Person():
def __init__(self,name,age):
self.name=name
self.__age__=age
def say(self):
print(self.name,",his age is",self.__age__)
person=Person("Gulf",24)
person.say()
person.__age__=25
print(person.__dict__)
print(person.name)
print(person.__age__)
输出:
Gulf ,his age is 24
{‘name’: ‘Gulf’, ‘age’: 25}
Gulf
25