基本的对象代码:
# 定义类 class Car: # 移动 def move(self): print('车在奔跑...') # 鸣笛 def toot(self): print("车在鸣笛...嘟嘟..") # 创建一个对象,并用变量BMW来保存它的引用 BMW = Car() BMW.color = '黑色' BMW.wheelNum = 4 #轮子数量 BMW.move() BMW.toot() print(BMW.color) print(BMW.wheelNum)
_ init_()方法
# 定义汽车类 class Car: #初始化函数,用来完成一些默认的设定 def __init__(self): self.wheelNum = 4 self.color = '蓝色' def move(self): print('车在跑,目标:夏威夷') # 创建对象 BMW = Car() print('车的颜色为:%s'%BMW.color) print('车轮胎数量为:%d'%BMW.wheelNum)
_ str _()方法
这个方法类似java的tostring方法。
class Car: def __init__(self, newWheelNum, newColor): self.wheelNum = newWheelNum self.color = newColor def __str__(self): msg = "嘿。。。我的颜色是" + self.color + "我有" + int(self.wheelNum) + "个轮胎..." return msg def move(self): print('车在跑,目标:夏威夷') BMW = Car(4, "白色") print(BMW)
_ del _()方法
当删除一个对象时,python解释器也会默认调用这个方法
import time class Animal(object): # 初始化方法 # 创建完对象后会自动被调用 def __init__(self, name): print('__init__方法被调用') self.__name = name # 析构方法 # 当对象被删除时,会自动被调用 def __del__(self): print("__del__方法被调用") print("%s对象马上被干掉了..."%self.__name) # 创建对象 dog = Animal("哈皮狗") # 删除对象 del dog cat = Animal("波斯猫") cat2 = cat cat3 = cat print("---马上 删除cat对象") del cat print("---马上 删除cat2对象") del cat2 print("---马上 删除cat3对象") del cat3 print("程序2秒钟后结束") time.sleep(2)
_ new _()方法
_ new _方法只负责创建对象,创建之后会去调用 _ init _ 去初始化对象的属性。
# 实例化一个单例 class Singleton(object): __instance = None def __new__(cls, age, name): #如果类数字能够__instance没有或者没有赋值 #那么就创建一个对象,并且赋值为这个对象的引用,保证下次调用这个方法时 #能够知道之前已经创建过对象了,这样就保证了只有1个对象 if not cls.__instance: cls.__instance = object.__new__(cls) return cls.__instance a = Singleton(18, "dongGe") b = Singleton(8, "dongGe") print(id(a)) print(id(b)) a.age = 19 #给a指向的对象添加一个属性 print(b.age)#获取b指向的对象的age属性
继承
1、python支持多继承,
2、继承规则
私有的属性,不能通过对象直接访问,但是可以通过方法访问
私有的方法,不能通过对象直接访问
私有的属性、方法,不会被子类继承,也不能被访问
一般情况下,私有的属性、方法都是不对外公布的,往往用来做内部的事情,起到安全的作用
# 定义一个父类 class A: def printA(self): print('----A----') # 定义一个父类 class B: def printB(self): print('----B----') # 定义一个子类,继承自A、B class C(A,B): def printC(self): print('----C----') obj_C = C() obj_C.printA() obj_C.printB()
类属性和实例属性
类属性
class People(object): name = 'Tom' #公有的类属性 __age = 12 #私有的类属性 p = People() print(p.name) #正确 print(People.name) #正确 print(p.__age) #错误,不能在类外通过实例对象访问私有的类属性 print(People.__age) #错误,不能在类外通过类对象访问私有的类属性
实例属性
class People(object): address = '山东' #类属性 def __init__(self): self.name = 'xiaowang' #实例属性 self.age = 20 #实例属性 p = People() p.age =12 #实例属性 print(p.address) #正确 print(p.name) #正确 print(p.age) #正确 print(People.address) #正确 print(People.name) #错误 print(People.age) #错误
实例属性的修改
class People(object): country = 'china' #类属性 print(People.country) p = People() print(p.country) p.country = 'japan' print(p.country) #实例属性会屏蔽掉同名的类属性 print(People.country) del p.country #删除实例属性 print(p.country)
注意:通过实例对象去引用,会产生一个同名的实例属性,这种方式修改的是实例属性,不会影响到类属性
类方法和静态方法
类方法
通过修饰器@classmethod来标识,通过它可以对类属性进行修改
class People(object): country = 'china' #类方法,用classmethod来进行修饰 def getCountry(cls): return cls.country def setCountry(cls,country): cls.country = country p = People() print p.getCountry() #可以用过实例对象引用 print People.getCountry() #可以通过类对象引用 p.setCountry('japan') print p.getCountry() print People.getCountry()
静态方法
通过修饰器@staticmethod来进行修饰
class People(object): country = 'china' #静态方法 def getCountry(): return People.country print People.getCountry()