zoukankan      html  css  js  c++  java
  • 0702面向对象180

      1 print('hehda---------------------------------------------------------------------------------------------------------------------------------------------')
      2 
      3 class MyClass:
      4     i = 123456
      5     def f(self):
      6         # print('The function in class')
      7         return 'hhe '
      8 x = MyClass()
      9 # print(MyClass.i)
     10 # print(MyClass.f())
     11 
     12 print(x.i)
     13 print(x.f())
     14 print('hehda----------------------------------------------------------------------------------------------------------------------------------------')
     15 
     16 class complex:
     17     def __init__(self,realpart,imagpart):
     18         self.r = realpart
     19         self.i = imagpart
     20 x = complex(3.0,4.5)
     21 print(x.r,x.i)
     22 
     23 
     24 
     25 print('hehda----------------------------------------------------------------------------------------------------------')
     26 
     27 class Test:
     28     def prt(self):
     29         print(self)
     30         print(self.__class__)
     31 
     32 t = Test()
     33 t.prt()
     34 
     35 
     36 
     37 print('hehda--------------------------------------------------------------------------------------------------------------------------------------------')
     38 class info:
     39     name = ''
     40     age = 0
     41     __weight = 0
     42     def info1(self,n,a,w):
     43         self.name = n
     44         self.age = a
     45         self.__weight = w
     46     def speak(self):
     47         print('{0}说我{1}了重'.format(self.name,self.age,self.__weight))
     48 
     49 p = info()
     50 p.info1('Runoob',10,30)
     51 p.speak()
     52 # runoob 说: 我 10 岁。
     53 
     54 
     55 print('hehda---------------------------------------------------------------------------------------------------------------------------------------')
     56 class info:
     57     name = ''
     58     age = 0
     59     __weight = 0
     60     def info1(self,n,a,w):
     61         self.name = n
     62         self.age = a
     63         self.__weight = w
     64     def speak(self):
     65         print('{0}说我{1}了重{2}'.format(self.name,self.age,self.__weight))
     66 
     67 p = info()
     68 p.info1('Runoob',10,30)
     69 p.speak()
     70 # runoob 说: 我 10 岁。
     71 '-----------对比以上两种写法---------------------'
     72 class people:
     73     name = ''
     74     age = 0
     75     __weight = 0
     76     def __init__(self,n,a,w):
     77         self.name = n
     78         self.age = a
     79         self.__weight = w
     80     def speak(self):
     81         print('{0}speak i am {1} years old and {2} weight'.format(self.name,self.age,self.__weight))
     82 
     83 p = people('Runoob',10,20)
     84 p.speak()
     85 
     86 
     87 print('inherit----------------------------------------------------------------------------------------------------------------------------------------')
     88 class people:
     89     name = ''
     90     age = 0
     91     __weight = 0
     92     def __init__(self,n,a,w):
     93         self.name = n
     94         self.age = a
     95         self.__weight = w
     96     def speak(self):
     97         print('{0}speak i am {1} years old and weight {2}kg'.format(self.name,self.age,self.__weight))
     98 
     99 # p = people('Jo1',28,45)
    100 # p.speak()
    101 class student(people):
    102     grade = ''
    103     def __init__(self,n,a,w,g):
    104         people.__init__(self,n,a,w)
    105         self.grade = g
    106     def speak(self):
    107         #打印内容中如果添加上self.__weight是错误的,__私有的只能在内部访问,在这个函数中不能访问
    108         print('{0}speak i am {1} years ild and weight kg and in class {2}'.format(self.name,self.age,self.grade))
    109 
    110 info = student('Jo',22,45,'6')
    111 info.speak()
    112 
    113 
    114 
    115 
    116 print('inherit---------------------------------------------------------------------------------------------------------------------------------------------')
    117 # class DerivedClassName(modlename.BaseClassName)
    118 # 我叫 Tim,我是一个演说家,我演讲的主题是 Python
    119 
    120 class people:
    121     name = ''
    122     age = 0
    123     __weight = 0
    124     def __init__(self,n,a,w):
    125         self.name = n
    126         self.age = a
    127         self.__weight = w
    128     def speak(self):
    129         print('My name is {0},i am {1} years old and weight {2}kg'.format(self.name,self.age,self.__weight))
    130 
    131 # p = people('Jo1',28,45,)
    132 # p.speak()
    133 
    134 class student(people):
    135     grade = ''
    136     def __init__(self,n,a,w,g):
    137         people.__init__(self,n,a,w)
    138         self.grade = g
    139     def speak(self):
    140         print('My name is {0},i am {1} years old and weight and in class {2}'.format(self.name,self.age,self.grade))
    141 
    142 # s = student('jo2',29,46,'6')
    143 # s.speak()
    144 
    145 class speaker:
    146     #这里变量的声明发现有没有都可
    147     # name = ''
    148     # topic = ''
    149     def __init__(self,n,t):
    150         self.name = n
    151         self.topic = t
    152     def speak(self):
    153         print('My name is {0} .My topic is {1}'.format(self.name,self.topic))
    154 
    155 # sp = speaker('Jo3','Python')
    156 # sp.speak()
    157 
    158 class sample(speaker,student):
    159     def __init__(self,n,a,w,g,t):
    160         student.__init__(self,n,a,w,g)
    161         speaker.__init__(self,n,t)
    162 
    163 test = sample('Jo4',30,47,'8','Python')
    164 test.speak()
    165 
    166 
    167 
    168 
    169 print('methods to rewrite------------------------------------------------------------------------------------------------------------------------------')
    170 class Parent:
    171     def myMethod(self):
    172         print('调用父类方法')
    173 
    174 class Child(Parent):
    175     def myMethod(self):
    176         print('调用子类方法')
    177 
    178 c = Child()
    179 c.myMethod()
    180 super(Child,c).myMethod()
  • 相关阅读:
    Using Change Management and Change Control Within a Project
    swift3.0 label改变行间距
    swift清理缓存
    Alamofire的get请求,post请求,以及上传字典
    swift跳转时隐藏tabbar,跳回时显示
    swift获取当前的Version
    swift回收键盘
    OC中常用的设计模式
    关于iOS多线程
    理解 : UDID、UUID、IDFA、IDFV
  • 原文地址:https://www.cnblogs.com/jpr-ok/p/9253781.html
Copyright © 2011-2022 走看看