#!/usr/bin/env python
#-*- coding:utf-8 -*-
#class是关键字,Animal是类名
#类名首字母大写(规范)
#定义类里面的方法,方法要传入一个self参数进去
#self代表的就是实例本身,也就是对象本身
#cat.name 这是实例的属性 ;cat.cry()实例的方法
#封装:通过构造函数将信息封装到实例中去,通过实例字段取出来
class Animal:
def __init__(self,name,age):
self.name = name
self.age = age
def cry(self):
print('%s在叫' % self.name)
def nianling(self):
print('%sis..' %self.age)
#Dog类 继承了 Animal类
#继承就是父类有什么,子类就有什么
#方法的重写(在子类中重新定义了父类的方法)
class Dog(Animal):
def cry(self):
print("汪汪汪叫。。。")
class Cat(Animal):
def cry(self):
print('喵喵叫。。。')
#创建一个对象(实例)
#变量 = 类名()
cat = Cat('kitty','23')
#实例化一个dog
dog = Dog('wangcai','22')
#执行一个方法
cat.cry()
#cat.nianling()
dog.cry()
#访问实例变量
#print(cat.name)
#print(cat.age)
#多态
def f(obj):
obj.cry()
f(cat)
f(dog)
#############存放家具oop例子################
#!/usr/bin/env python
#-*-coding:utf-8 -*-
class Home:
def __init__(self,area,addr,info):
self.area = area
self.addr = addr
self.info = info
self.left_area = area
self.contain = []
def __str__(self):
msg = '房子的总面积:%d 房子的可用面积:%d 房子的地址:%s 房子的户型:%s' % (self.area,self.left_area,self.addr,self.info)
msg += '当前房子里的家具有:%s' %(str(self.contain))
return msg
def add_item(self,item):
#self.left_area -= item.size
#self.contain.append(item.name)
self.left_area -= item.get_size()
self.contain.append(item.get_name())
class Bed:
def __init__(self,name,size):
self.name = name
self.size = size
def __str__(self):
return '%s床的大小是%d平米' %(self.name,self.size)
def get_size(self):
return self.size
def get_name(self):
return self.name
house = Home(129,'厦门 金山小区','三室一厅')
print(house)
bed1 = Bed('席梦思',4)
print(bed1)
house.add_item(bed1)
print(house)
bed2 = Bed('双床',3)
house.add_item(bed2)
print(house)
###############03-私有方法##############
在方法面前加“__”两个下划线就是私有方法,私有方法直接调用,会报错:
dog.__send_msg()
AttributeError: 'Dog' object has no attribute '__send_msg'
私有方法的好处是:不想让他人直接调用的方法获取直接数据,满足我的条件,才调用,例如:
############04-__del__方法##############
###############测量一个对象的引用数量################
In [1]: import sys In [3]: class Dog: ...: pass ...: dog = Dog() ...: sys.getrefcount(dog) ...: Out[3]: 2 In [4]: tt = dog In [5]: class Dog: ...: pass ...: dog = Dog() ...: sys.getrefcount(dog) ...: Out[5]: 2 In [6]: tt = dog In [7]: sys.getrefcount(dog) Out[7]: 3 In [8]: del tt In [9]: sys.getrefcount(dog) Out[9]: 2
#############方法的重写###################
#!/usr/bin/env python #-*- coding:utf-8 -*- class Animal: def eat(self): print('吃...') def drink(self): print('喝...') def sleep(self): print('睡...') class Dog(Animal): def jiao(self): print('吠吠...') class Cat(Dog): def jiao(self): print('叫叫...') #Dog.jiao(self) ###既想要自己jiao方法的功能,又需要加上父类的jiao方法的,需加上self super().jiao() #第一种调用被重写的父类的方法 #Dog.jiao(self) #第二种调用被重写的父类的方法 #super().jiao() cat = Cat() cat.jiao()
结果:
###################私有方法和私有属性在继承中的表现##############
###############多继承##############
#!/usr/bin/env python #-*- coding:utf-8 -*- class Base(object): def test(self): print('---base') class A(Base): def test1(self): print('---A') class B(Base): def test2(self): print('---B') class C(A,B): pass c = C() c.test() c.test1() c.test2()
结果:
#!/usr/bin/env python #-*- coding:utf-8 -*- class Base(object): def test(self): print('---base') class A(Base): def test(self): print('---A') class B(Base): def test(self): print('---B') class C(A,B): def test(self): print('---C') c = C() c.test() print(C.__mro__)
结果:
当继承中有多个方法的名字一样,调用哪一个呢?可以用print(类名.__mro__) 查看调用的优先级
###################多态########################
#!/usr/bin/env python #-*- coding:utf-8 -*- class Dog(object): def print_self(self): print('大家好,我是xxxx,以后多多关照') class Xiaotq(Dog): def print_self(self): print('hello,everybody,我是你们的老大,我是xxxx') def introduce(tmp): tmp.print_self() dog1 = Dog() dog2 = Xiaotq() introduce(dog1) introduce(dog2)
提示:面向对象的三要素:封装,继承,多态
###############类属性,实例属性##############
###############类方法,实例方法,静态方法#############
#######################__new__方法 #######################
其他语言的构造方法:既包含初始化和创建(但python 不一样),python的__new__ 方法创建,__init__初始化,两个方法是分开的
##############创建单实例####################
#!/usr/bin/env python #-*- coding:utf-8 -*- class Dog(object): __instance = None def __new__(cls): if cls.__instance == None: cls.__instance = object.__new__(cls) return cls.__instance else: return cls.__instance dog1 = Dog() print(id(dog1)) dog2 = Dog() print(id(dog2))
结果:
##############只初始化一次对象################
#!/usr/bin/env python #-*- coding:utf-8 -*- class Dog(object): __instance = None __init_flag = False def __new__(cls,name): if cls.__instance == None: cls.__instance = object.__new__(cls) return cls.__instance else: return cls.__instance def __init__(self,name): if Dog.__init_flag == False: self.name = name Dog.__init_flag = True dog1 = Dog('test1') print(id(dog1)) print(dog1.name) dog2 = Dog('test2') print(id(dog2)) print(dog2.name)
结果: