• Day16:OOP:object-orinted-programing


     1 # dog1= {
     2 #     'name' : "aliu",
     3 #     'gender' : 'female',
     4 #     'type' : 'dog'
     5 # }
     6 # dog2= {
     7 #     'name' : "zxver",
     8 #     'gender' : 'male',
     9 #     'type' : 'dog'
    10 # }
    11 # people= {
    12 #     'name' : "zita",
    13 #     'gender' : 'female',
    14 #     'type' : 'people'
    15 # }
    16 # def chishi(dog):
    17 #     if dog['type'] == 'dog':
    18 #         print("the dog [%s] is eating shit" %dog["name"])
    19 #
    20 # def jiao(dog):
    21 #     if dog['type'] == 'dog':
    22 #         print("the dog [%s] is barking"  %dog["name"])
    23 #
    24 # chishi(dog1)
    25 # chishi(people)
    26 #>>>>>>>>>>>>>>>>>>>>>>>>>It's repeatable upon
    27 #>>>>>>>>>>>>>>>>>>>>>>>>>declare the fuctional area below
    28 
    29 # def dog(name,gender,type):
    30 #     def chishi(dog):
    31 #         print("the dog [%s] is eating shit" % dog["name"])
    32 #
    33 #     def jiao(dog):
    34 #         print("the dog [%s] is barking" % dog["name"])
    35 #     dog1 = {
    36 #         'name': name,
    37 #         'gender': gender,
    38 #         'type': type,
    39 #         'jiao': jiao,
    40 #         'chishi': chishi,
    41 #     }
    42 #     return dog1
    43 # # d1 = dog()
    44 # # print(d1)
    45 # # d1['chishi'](d1)
    46 
    47 # d1 = dog('alex','female','dog')
    48 # d2 = dog('zita','female','dog')
    49 # print(d1)
    50 # print(d2)
    51 # d1['jiao'](d1)
    52 # d2['chishi'](d2)
    53 
    54 #>>>>>>>>>>>>>>>>>>>>>>>>>>use 'init' to initialize the function
    55 
    56 def dog(name,gender,type):
    57     def chishi(dog):
    58         print("the dog [%s] is eating shit" % dog["name"])
    59 
    60     def jiao(dog):
    61         print("the dog [%s] is barking" % dog["name"])
    62 #>>>>>>>>>Action of the class
    63     def init(name,gender,type):
    64         dog1 = {
    65             'name': name,
    66             'gender': gender,
    67             'type': type,
    68             'jiao': jiao,
    69             'chishi': chishi,
    70         }
    71         return dog1
    72 #>>>>>>>>>>the traits of the class
    73     return init(name,gender,type)
    74 
    75 
    76 d1 = dog('alex','female','dog')   #>>>>>>>>>>>define the object:include the actions and the traits
    77 d2 = dog('zita','female','dog')
    78 print(d1)
    79 print(d2)
    80 d1['jiao'](d1)
    81 d2['chishi'](d2)
    View Code
    the traits:name,addr,type
    the actions:test,recruit,expel
    conbine the actions and the traits
     1 def school(name,addr,type):
     2     def kao_shi(school):
     3         print('%s is testing' %school['name'])
     4     def zhao_sheng(school):
     5         print('%s is recruting' %school['name'])
     6     def init(name,addr,type):
     7         sch = {
     8             'name': name,
     9             'addr': addr,
    10             'type': type,
    11             'kao_shi': kao_shi,
    12             'zhao_sheng': zhao_sheng,
    13         }
    14         return sch
    15     return init(name,addr,type)
    16 s1 = school('oldboy','chongqing','privite')
    17 s2 = school('tsinghua','beijing','public')
    18 print(s1)
    19 print(s2)
    20 s1['zhao_sheng'](s1)
    21 s2['kao_shi'](s2)
    View Code
    '''
    the attribute of the class:
    1:data;
    2:fuction

    #python为类内置的特殊属性
    类名.__name__# 类的名字(字符串)
    类名.__doc__# 类的文档字符串
    类名.__base__# 类的第一个父类(在讲继承时会讲)
    类名.__bases__# 类所有父类构成的元组(在讲继承时会讲)
    类名.__dict__# 类的字典属性
    类名.__module__# 类定义所在的模块
    类名.__class__# 实例对应的类(仅新式类中)

    '''
     1 # class Chinese:               #>>>>>>without () called classic(python2)
     2 #     'this is a chinese class'
     3 #     pass
     4 # print(Chinese)               #>>>>>><class '__main__.Chinese'>
     5 #
     6 # #>>>>>>>>>>>>>>>>>what the instantiation do?
     7 # p1 = Chinese()
     8 # print(p1)                    #>>>>><__main__.Chinese object at 0x0000000001DD6A60>
     9 #
    10 # class Chinese(object):      #>>>>with a () called new style(python3)
    11 #     'this is a chinese class'
    12 #     pass
    13 # print(Chinese)               #>>>>>><class '__main__.Chinese'>
    View Code
    '''
    the attribute of the instantiation:
    1:data;
    no function attribute
    '''
     1 # class Chinese:
     2 #     'this is a chinese class'
     3 #     government = 'The Communist Party'
     4 #     country = 'china'
     5 #     def __init__(self,name,gender,age):
     6 #         self.name = name
     7 #         self.gender = gender
     8 #         self.age = age
     9 #     def split(self):
    10 #         print('%s is spliting '%(self.name))
    11 #
    12 #     def eat_food(self,food):
    13 #         print('%s is eating %s ' % (self.name,food))
    14 #
    15 # p1 = Chinese('alex','female',18)
    16 # print(Chinese.government)
    17 # print(dir(Chinese))
    18 # print(Chinese.__dict__)
    19 # print(Chinese.__dict__['government'])
    20 # print(Chinese.__dict__['split'](p1))
    21 # print(p1.__dict__)
    22 # print(p1.name)
    23 # p1.split()
    24 # print(p1.government)
    25 # p1.eat_food('shit')
    26 # def speak(self,language):
    27 #     print('%s is speaking %s'%(self.name,language))
    28 
    29 #>>>>>>>>>>the Class:add,del,modify,check:
    30 # print(Chinese.government)
    31 # Chinese.lotion = 'China'
    32 # print(Chinese.lotion)
    33 # # del Chinese.lotion
    34 # # print(Chinese.lotion)
    35 # Chinese.spk = speak
    36 # # print(Chinese.__dict__)
    37 # p1.spk('english')
    38 
    39 #>>>>>>>>>>the instantiation :add,del,modify,check:pr
    40 
    41 # print(p1.name)
    42 # print(p1.split)
    43 # p1.age =18
    44 # print(p1.__dict__)
    45 # p1.spk1=speak
    46 # print(p1.__dict__)
    47 # p1.spk1(p1,'english')
    48 #Dont change the baselevel dict
    49 # p1.__dict__['sex']='male'
    50 # print(p1.__dict__)
    51 # p1.age = 19
    52 # print(p1.__dict__)
    53 # del p1.age
    54 # print(p1.__dict__)
    55 
    56 #>>>>>>define a functional area:
    57 # class MyDate:
    58 #     pass
    59 # x=1
    60 # y=10
    61 # MyDate.x=1
    62 # MyDate.y=2
    63 # print(MyDate.__dict__)
    64 
    65 #>>>>>>>>>>>>>>>>>>>>more complicated:
    66 # print(Chinese.country)
    67 # p1.country = 'us'
    68 # print(p1.country)         #>>>>>>>>us
    69 # print(Chinese.country)    #>>>>>>>>>>..china
    70 country = 'china'
    71 class Chinese:
    72     country = 'js'
    73     l=['a','b']
    74     def __init__(self,name):
    75         # country = 'us'
    76         print('------------>?',country)    #>>>us
    77         # name = input('please input the username:')
    78         #>>>>youd better not write the input or output blank here!!
    79         self.name = name
    80     def split(self):
    81         print('%s is spliting '%(self.name))
    82 
    83     def eat_food(self,food):
    84         print('%s is eating %s ' % (self.name,food))
    85 
    86 p1 = Chinese('alx')
    87 # # p1.split()
    88 # print(Chinese.country)   #>>>>>>>js
    89 # p1.l =[1,2,3]             #>>>>>>add the charameter l to p1
    90 p1.l.append('c')            #>>>>>>no adding so it changed the l of class
    91 # print(p1)
    92 print(p1.__dict__)
    93 print(Chinese.__dict__)
    View Code
  • 相关阅读:
    Django
    python django框架学习
    Http Header里的Content-Type
    Python 头部 #!/usr/bin/python 和 #!/usr/bin/env python的区别
    关于“编译型语言”和“解释性语言”的区别
    Axure XMind整理交互思路
    异常处理
    re模块
    模块
    正则表达式**************************
  • 原文地址:https://www.cnblogs.com/zxver/p/12680954.html
Copyright © 2020-2023  润新知