day_25 面向对象(二)

"""
特性   property
类方法   classmethed
静态方法 staticmethed
"""

property

封装逻辑,让调用者感受不到逻辑 , property不可以赋值

class Room(object):
    #property    特性
    def __init__(self,name,owner,width,length,heigh):
        self.name = name
        self.owner=owner
        self.width = width
        self.length = length
        self.heigh = heigh

    def cal_area(self):
        print(self.width*self.length)

    @property
    def cal_areaw(self):
        return self.width*self.length


n1 = Room('yy','wc',10,10,10)

n1.cal_area()
print (n1.cal_areaw)  #100
print(n1.name)        #yy


import  math
class Circle:
    def __init__(self,radius):
        self.radius = radius

    @property
    def area(self):
        return math.pi * self.radius**2 #计算面积

    @property
    def perimeter(self):
        return 2*math.pi*self.radius #计算周长

c=Circle(10)
print(c.radius)
print(c.area)
print(c.perimeter)

c.area(33)      #不可以赋值

  不可以赋值

Traceback (most recent call last):
  File "D:/python/pythontest/练习2/10.py", line 85, in <module>
    c.area(33)
TypeError: 'float' object is not callable

  




classmethed
class Room(object):
    #类方法
    tag = 1
    def __init__(self,name,owner,width,length,heigh):
        self.name = name
        self.owner=owner
        self.width = width
        self.length = length
        self.heigh = heigh

    def cal_area(self):
        print(self.width*self.length)

    @classmethod
    def tell_info(cls):             #cls 是   类名
        print(cls)
        print(cls.tag)


# n1 = Room('yy','wc',10,10,10)
Room.tell_info()
# 结果
#  <class '__main__.Room'>
#  1

  

 staticmethod

class Room(object):
    #静态方法  staticmetched
    #静态方法只是名义上归类管理, 不能使用类变量和实例变量  是类的工具包
    tag = 1
    def __init__(self,name,owner,width,length,heigh):
        self.name = name
        self.owner=owner
        self.width = width
        self.length = length
        self.heigh = heigh


    @staticmethod
    def wash_body(a,b):
        print("%s %s正在洗澡" %(a,b))

    @staticmethod
    def test():
        print('66666666666')

Room.wash_body(2,3) #2 3正在洗澡
Room.test()   #66666666666

组合

class Hand:
    pass

class  Foot:
    pass

class Trunk:
    pass

class Head:
    pass


class Persion:
    def __init__(self,id_num,name):
        self.id_num = id_num
        self.name = name
        self.hand = Hand()
        self.foot = Foot()
        self.trunk = Trunk()
        self.head = Head()

p1 = Persion('1111','alex')
print(p1.__dict__)
#{'id_num': '1111', 'name': 'alex', 'hand': <__main__.Hand object at 0x00000000025A35F8>, 'foot': <__main__.Foot object at 0x00000000025A3630>, 'trunk': <__main__.Trunk object at 0x00000000025A36A0>, 'head': <__main__.Head object at 0x00000000025A3710>}

  

class School:
    def __init__(self,name,addr):
        self.name = name
        self.addr = addr

class Course:
    def __init__(self,name,price,period,school):
        self.name = name
        self.price = price
        self.period = period
        self.school = school

s1 = School('oldboy','北京')
s2 = School('oldboy','南京')
s3 = School('oldboy','东京')

c1 = Course('linux',10,'1h',s1)

print(c1.__dict__)
#{'name': 'linux', 'price': 10, 'period': '1h', 'school': <__main__.School object at 0x00000000025A3550>}

print(c1.school)
#<__main__.School object at 0x00000000025A3550>

print(s1)
#<__main__.School object at 0x00000000025A3550>

print(c1.school.addr)   #   北京

  

class School:
    def __init__(self,name,addr):                       #学校名  地址
        self.name = name
        self.addr = addr
    def zhao_sheng(self):
        print('%s 正在招生'%self.name)

class Teacher:
    def __init__(self,name,price,school):               #老师名   老师工资   学校名
        self.name = name
        self.price = price
        self.school = school

class Student:
    def __init__(self,name,school,teach):               #学生名   学校名   老师名
        self.name = name
        self.school = school
        self.teach = teach

class Course:
    def __init__(self,name,price,period,school,teach,student):  #科目名   学费 时间  学校名  老师名   学生名
        self.name = name
        self.price = price
        self.period = period
        self.school = school
        self.teach = teach
        self.student = student

sch1 = School('oldboy','北京')
sch2 = School('oldboy','南京')
sch3 = School('oldboy','东京')


t1 = Teacher('august','3w',sch1)
t2 = Teacher('yangyang','6w',sch1)

stu1 = Student('st_name1',sch1,t1)
stu2 = Student('st_name2',sch2,t2)
#
# c1 = Course('py','1w','3mount',sch1,t1,stu1)
# c2 = Course('py','1w','3mount',sch2,t2,stu2)


msg='''
学校:
1 老男孩 北京校区
2 老男孩 南京校区
3 老男孩 东京校区'''

msg1='''
老师:
1 august
2 yangyang
'''
msg2='''
学生:
1 q1   
2 q2
'''
while True:
    print(msg)
    menu = {
            '1':sch1,
            '2':sch2,
            '3':sch3,
            }
    menu1 = {
            '1':t1,
            '2':t2
            }
    menu2 = {
            '1':stu1,
            '2':stu2
            }
    school = input('学校名>>> ')
    school = menu[school]

    print(msg1)
    teach = input('老师名>>> ')
    teach = menu1[teach]

    print(msg2)
    student = input('学生名>>> ')
    student = menu2[student]

    name = input('课程名 >>> ')
    price = input('学费 >>> ')
    period = input('学习时间 >>> ')

    course_object = Course(name,price,period,school,teach,student)
    print("科目%s  学费%s 时间%s  学校%s 老师%s  学生%s"%(name,price,period,course_object.school.name,course_object.teach.name,course_object.student.name))

 

继承   封装  多态

继承

 

'''
单继承
多继承
'''

 

  

class Dad:
    money = 10
    def __init__(self,name):
        self.name = name

    def play(self):
        print('%s 打麻将'%self.name)

class Son(Dad):
    pass


print(Son.money)     #10

print(Son.__dict__)  #{'__module__': '__main__', '__doc__': None}
print(Dad.__dict__)  #{'__module__': '__main__', 'money': 10, '__init__': <function Dad.__init__ at 0x00000000025A12F0>, 'play': <function Dad.play at 0x00000000025A1400>, '__dict__': <attribute '__dict__' of 'Dad' objects>, '__weakref__': <attribute '__weakref__' of 'Dad' objects>, '__doc__': None}

s = Son('yw')
s.play()             #yw 打麻将

  

'''
子类继承父类的所有属性,子类自定义的属性如果和父类重名,那子类就用自己改写的, 不会覆盖父类的属性
'''
'''
什么时候用继承
1.当类之间有显著不同,并且较小的类是较大的类所需要的组件时, 用组合
2.当类之间有很多相同的功能, 提取这些共同的功能做成成基类,用继承比较好
'''

  

 

'''
继承的两种用途
一:继承基类的方法,并且做出自己的改变或者扩展(代码重用):实践中,继承的这种用途意义并不很大,甚至常常是有害的。因为它使得子类与基类出现强耦合。
二:声明某个子类兼容于某基类,定义一个接口类(模仿java的Interface),接口类中定义了一些接口名(就是函数名)且并未实现接口的功能,子类继承接口类,并且实现接口中的功能
'''

'''
# 声明某个子类兼容于某个基类, 定义一个接口类: 子类继承接口类, 并实现接口中定义的方法
# 接口继承      归一化
'''

 接口继承

import abc
class All_file(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def read(self):
        pass

    @abc.abstractmethod
    def write(self):
        pass

class Disk(All_file):
    def read(self):
        print('disk read')

    def write(self):
        print('disk write')

class Cdrom(All_file):
    def read(self):
        print('cdrom read')

    def write(self):
        print('cdrom write')


class Mem(All_file):
    def read(self):
        print('mem read')

    def write(self):
        print('mem write')
#
m1=Mem()
m1.read()
m1.write()

 

 继承

class Vehicle:
    Country='China'
    def __init__(self,name,speed,load,power):
        self.name=name
        self.speed=speed
        self.load=load
        self.power=power
    def run(self):
        print('开动啦')



class Subway(Vehicle):
        def __init__(self,name,speed,load,power,line):
           Vehicle.__init__(self,name,speed,load,power)
           # super(Subway,self).__init__(name,speed,load,power)
           # super().__init__(name, speed, load, power)
           self.line=line

        def show_info(self):
            print(self.name,self.speed,self.load,self.power,self.line)

        def run(self):
            Vehicle.run(self)
            #super().run()
            print('%s %s 线,开动啦' %(self.name,self.line))
line13=Subway('北京地铁','10km/s',1000000000,'电',13)

line13.show_info()

line13.run()

  

 

 

 

原文地址:https://www.cnblogs.com/augustyang/p/9083716.html