zoukankan      html  css  js  c++  java
  • 对象呢?

    类:把一类事物的相同的特征和动作整合到一起就是类

      类是一个抽象的概念

    对象:就是基于类而创建的一个具体的事物(具体存在的)

       也是特征和动作整合到一起

    什么叫实例化:由类生产对象的过程叫实例化,类实例化的结果就是一个对象,或者叫做一个实例(实例=对象)

    学校类:

    特征:name,addr,type

    动作:考试,招生,开除学生

    整合特征和动作

    我的对象  

    类:     print()查看    赋值:增加、修改    dle:删除

    print(China.dang)  #查看类
    China.dang="" # 修改 类   增加 类
    del China.dang #删除 类
    print(p1.__dict__) #查看实例, 以字典的方式显示

    函数:

    增加

    def eat_food(self,food):
        print("%s正在吃%s“,%(self.food))
    China.eat=eat_food
    p1.eat("饭)

    修改

    def test(self):
        print("test")
    China.reproduction=test
    pl.reproduction()
    >>>test

    实例: 如果找不到就到类去找

    p1=China("szw")
    print(p1.__dict__)#查看实例
    print(p1.name)   #查看
    p1.age=18#增加  也可以修改
    del p1.age#删除

    .函数又作用域的概念,其实类也有作用域的概念,二者一样

    .你可以把class当做最外层的函数,是一个作用域

    #定义一个类,只当一个作用域去用,类似于c语言中的结构体
    class MyData:
        pass
    
    x=10
    y=20
    MyData.x=1
    MyData.y=2
    
    print(x,y)
    print(MyData.x,MyData.y)
    print(MyData.x+MyData.y)

     实例没有就去类修改

     1 country="中国"
     2 class China:
     3     dang="共产"
     4     country="中国-------"
     5     l=["a","b"]
     6     def __init__(self,name,age):
     7         self.name=name
     8         print(country)
     9         self.age=age
    10     def reproduction(self):
    11         print("shen%s"% self.age)
    12 p1=China("alex",18)
    13 p1.l.append("c")
    14 print(p1.l)
    >>>中国
    >>>['a', 'b', 'c']

     静态属性、类方法、静态方法

    @property           把 函数属性 变成 静态属性 的样式        return一个值

     1 class Room:
     2     def __init__(self,name,owner,width,lenght,heigh):
     3         self.name=name
     4         self.owner=owner
     5         self.width=width
     6         self.lenght=lenght
     7         self.heigh=heigh
     8     @property
     9     def cal_area(self):
    10         print("%s住的%s总体积%s"%(self.name,self.owner,self.width*self.lenght*self.heigh))
    11         return self.width*self.lenght*self.heigh
    12 sl=Room("alex","厕所",1,1,1)
    13 print(sl.cal_area)
    14 print(sl.name)
    15 >>>alex住的厕所总体积1
    16 >>>1
    17 >>>alex

    @classmethod      只用类方法  不用实例

    classmethod类方法只是给类使用(不管是否存在实例),只能访问实例变量
     1 class Room:
     2     bag=1
     3     def __init__(self,name,owner,width,lenght,heigh):
     4         self.name=name
     5         self.owner=owner
     6         self.width=width
     7         self.lenght=lenght
     8         self.heigh=heigh13     @classmethod
    14     def cal_info(cls,s):
    15         print(Room)
    16         print("---->",cls.bag,s)
    17 Room.cal_info(10)
    18 >>><class '__main__.Room'>
    19 >>>----> 1 10

    @staticmethod          不用类 不用实例

    静态方法只是名义上的归属类管理,不能使用类变量和实例变量,是类的工具包
     1 class Room:
     2     bag=1
     3     def __init__(self,name,owner,width,lenght,heigh):
     4         self.name=name
     5         self.owner=owner
     6         self.width=width
     7         self.lenght=lenght
     8         self.heigh=heigh
     9     @staticmethod
    10     def wash_body(x):
    11         print("opoupgjsjgosgo",x)
    12 Room.wash_body(10)
    13 >>>opoupgjsjgosgo 10

    组合

    1:做关联

    2:小的组成大的

    基本成型

     1 class School:
     2     def __init__(self,name,addr):
     3         self.name=name
     4         self.addr=addr
     5     def zhao_sheng(self):
     6         print("%s正在招生"%self.name)
     7 
     8 class Course:
     9     def __init__(self,name,price,period,school):
    10         self.name=name
    11         self.price=price
    12         self.period=period
    13         self.school=school
    14 
    15 class Teacher:
    16     def __init__(self,name,school,course):
    17         self.name=name
    18         self.school=school
    19         self.course=course
    20 
    21 sl=School("alex","北京")
    22 cl=Course("python",10,"1h",sl)   #添加
    23 tl=Teacher("海峰",sl,cl)         #添加
    24 print(tl.school.name,tl.school.addr)
    25 print(tl.course.name,tl.course.price,tl.course.period)
    26 print(cl.school.name,cl.school.addr)
    27 >>>alex 北京
    28 >>>python 10 1h
    29 >>>alex 北京

     与用户交互 

     1 class School:
     2     def __init__(self,name,addr):
     3         self.name=name
     4         self.addr=addr
     5     def zhao_sheng(self):
     6         print("%s正在招生"%self.name)
     7 
     8 class Course:
     9     def __init__(self,name,price,period,school):
    10         self.name=name
    11         self.price=price
    12         self.period=period
    13         self.school=school
    14 
    15 s1=School("alex","北京")
    16 s2=School("alex","南京")
    17 s3=School("alex","东京")
    18 
    19 msg='''
    20 1 老男孩 北京校区
    21 2 老男孩 南京校区
    22 3 老男孩 东京校区
    23 '''
    24 while True:
    25     print(msg)
    26     menu={
    27         '1':s1,
    28         '2':s2,
    29         '3':s3
    30     }
    31     choice=input("选择学校>>>")
    32     school_obj=menu[choice]
    33     name=input("课程名>>>")
    34     price=input("课程费用>>>")
    35     period=input("课程周期>>>")
    36     new_course=Course(name,price,period,school_obj)
    37     print("课程%s属于%s学校"%(new_course.name,new_course.school.name))

    继承

    继承父类

     1 class Dad:
     2     '这个是父类'
     3     money=10
     4     def __init__(self,name):
     5         print("")
     6         self.name=name
     7     def hit_son(self):
     8         print("%s正在打儿子"%self.name)
     9 class Son(Dad):
    10     money = 1000003
    11 print(Dad.__dict__)
    12 print(Son.__dict__)
    13 sl=Son("alex")
    14 sl.hit_son()
    15 print(sl.name)
    16 print(Dad.money)
    17 >>>{'__doc__': '这个是父类', 'hit_son': <function Dad.hit_son at 0x0124C348>, 'money': 10, '__init__': <function Dad.__init__ at 0x0124C390>, '__weakref__': <attribute '__weakref__' of 'Dad' objects>, '__dict__': <attribute '__dict__' of 'Dad' objects>, '__module__': '__main__'}
    18 >>>{'__doc__': None, 'money': 1000003, '__module__': '__main__'}
    19 >>>20 >>>alex正在打儿子
    21 >>>alex
    22 >>>10

    继承 的统一性

     1 import abc
     2 class All_file(metaclass=abc.ABCMeta):
     3     @abc.abstractmethod
     4     def read(self):
     5         pass
     6     @abc.abstractmethod
     7     def write(self):
     8         pass
     9 class Cdrom(All_file):
    10     def read(self):
    11         print("cdrom read")
    12     def write(self):
    13         print("cdrom write")
    14 class Mem(All_file):
    15     def read(self):
    16         print("mem read")
    17     def write(self):
    18         print("mem write")
    19 cl=Cdrom()
    20 cl.read()
    21 cl.write()

    继承顺序:python2深度优先、python3广度优先。

     1 class A:
     2     def test(self):
     3         print("a")
     4     pass
     5 class B(A):
     6     # def test(self):
     7     #     print("B")
     8     pass
     9 class C(A):
    10     # def test(self):
    11     #     print("C")
    12     pass
    13 class D(B):
    14     # def test(self):
    15     #     print("D")
    16     pass
    17 class E(C):
    18     # def test(self):
    19     #     print("e")
    20     pass
    21 class F(D,E):
    22     # def test(self):
    23     #     print("F")
    24     pass
    25 fl=F()
    26 fl.test()
    27 
    28 print(F.__mro__)
    29 >>>a
    30 >>>(<class '__main__.F'>, <class '__main__.D'>, <class '__main__.B'>, <class '__main__.E'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>)

    子调父

     1 class Vehicle: #定义交通工具类
     2     Country='China'
     3     def __init__(self,name,speed,load,power):
     4         self.name=name
     5         self.speed=speed
     6         self.load=load
     7         self.power=power
     8     def run(self):
     9         print("开动了")
    10         print("开动了")
    11         print("开动了")
    12         print("开动了")
    13         print("开动了")
    14 class Subway(Vehicle):
    15     def __init__(self,name,speed,load,power,line):
    16         Vehicle.__init__(self,name,speed,load,power)
    17         self.line=line
    18     def run(self):
    19         Vehicle.run(self)
    20         print("%s %s开动了"%(self.name,self.line))
    21 
    22 
    23 line13=Subway("中国地铁","180m/s",1000000000000,"","13号线")
    24 line13.run()
    25 >>>开动了
    26 >>>开动了
    27 >>>开动了
    28 >>>开动了
    29 >>>开动了
    30 >>>中国地铁 13号线开动了

    子调父的优化

    super().__init__(name,speed,load,power)
    super(Subway, self).run()
     1 class Vehicle: #定义交通工具类
     2     Country='China'
     3     def __init__(self,name,speed,load,power):
     4         self.name=name
     5         self.speed=speed
     6         self.load=load
     7         self.power=power
     8     def run(self):
     9         print("开动了")
    10         print("开动了")
    11         print("开动了")
    12         print("开动了")
    13         print("开动了")
    14 class Subway(Vehicle):
    15     def __init__(self,name,speed,load,power,line):
    16         super(Subway, self).__init__(name,speed,load,power)
    17         self.line=line
    18     def run(self):
    19         super(Subway, self).run()
    20         print("%s %s开动了"%(self.name,self.line))
    21 line13=Subway("中国地铁","180m/s",1000000000000,"","13号线")
    22 line13.run()
    23 >>>开动了
    24 >>>开动了
    25 >>>开动了
    26 >>>开动了
    27 >>>开动了
    28 >>>中国地铁 13号线开动了

     多态

    什么是多态:

    类的继承有两层意义:1.改变 2.扩展

    多态就是类的这两层意义的一个具体的实现机制 即,调用不同的类实例化得对象下的相同的方法,实现的过程不一样

    多态:   多态反应的是运行时候的状态

     1 class H20:
     2     def __init__(self,name,temperature):
     3         self.name=name
     4         self.temperature=temperature
     5     def turn_ice(self):
     6         if self.temperature<0:
     7             print('%s温度太低结冰了'%self.name)
     8         elif self.temperature>0 and self.temperature<100:
     9             print('%s液化成水'%self.name)
    10         elif self.temperature>100:
    11             print('%s温度太高变成水蒸气'%self.name)
    12 class Water(H20):
    13     pass
    14 class Ice(H20):
    15     pass
    16 class Steam(H20):
    17     pass
    18 w1=Water('',25)
    19 i1=Ice("",-20)
    20 s1=Steam("蒸汽",3000)
    21 def func(obj):
    22     obj.turn_ice()
    23 func(w1)
    24 func(i1)
    25 >>>水液化成水
    26 >>>冰温度太低结冰了

    封装

    python不依赖语言特性去实现第二层面的封装,而是通过遵循一定的数据属性和函数属性的命名约定来达到封的效果

    约定一:任何以单下划线开头的名字都应该是内部的,私有的

     1 #_*_coding:utf-8_*_
     2 __author__ = 'Linhaifeng'
     3 
     4 class People:
     5     _star='earth'
     6     def __init__(self,id,name,age,salary):
     7         self.id=id
     8         self.name=name
     9         self._age=age
    10         self._salary=salary
    11 
    12     def _get_id(self):
    13         print('我是私有方法啊,我找到的id是[%s]' %self.id)
    14 #我们明明约定好了的,只要属性前加一个单下划线,那他就属于内部的属性,不能被外部调用了啊,为何还能调用???
    15 print(People._star)
    16 p1=People('3706861900121221212','alex',28,10)
    17 
    18 print(p1._age,p1._salary)
    19 p1._get_id()

    约定二:双下划线开头的名字 python会 默认 重命名   __类名+双下划线的变量

     1 class People:
     2     __star="earth"
     3     def __init__(self,id,name,age,salary):
     4         self.id=id
     5         self.name=name
     6         self.age=age
     7         self.salary=salary
     8     def get_id(self):
     9         print("我是私有发放呀,我找到的id是%s"%self.id)
    10 p1=People("213132","alex","18",10000)
    11 print(People.__dict__)
    12 print(p1._People__star)
    13 >>>{'get_id': <function People.get_id at 0x013AD300>, '__init__': <function People.__init__ at 0x013AD348>, '__doc__': None, '_People__star': 'earth', '__dict__': <attribute '__dict__' of 'People' objects>, '__module__': '__main__', '__weakref__': <attribute '__weakref__' of 'People' objects>}
    14 >>>earth

     峰

    http://www.cnblogs.com/linhaifeng/articles/6182264.html#_label14
  • 相关阅读:
    TeX系列: tikz-3dplot绘图宏包
    TeX系列: MATLAB和LaTeX结合绘图
    Tex系列: pgfplots安装
    C 标准库
    C 标准库
    C 标准库
    C 标准库
    C 标准库
    C 标准库
    C 标准库
  • 原文地址:https://www.cnblogs.com/shizhengwen/p/6201806.html
Copyright © 2011-2022 走看看