day24-python之面向对象

1.面向对象

 1 # name='元昊'
 2 #
 3 # gender='母'
 4 #
 5 # type='藏獒'
 6 
 7 #狗的特征
 8 dog1={
 9     'name':'元昊',
10     'gender':'母',
11     'type':'藏獒'
12 }
13 dog2={
14     'name':'alex',
15     'gender':'母',
16     'type':'腊肠',
17 }
18 dog3={
19     'name':'hb',
20     'gender':'母',
21     'type':'哈士奇'
22 }
23 person1={
24     'name':'武sir',
25     'gender':'母',
26     'type':'人'
27 }
28 
29 person2 = {
30     'name':'流水',
31     'gender':'母',
32     'type':'狗'
33 }
34 
35 
36 def dog(name,gender,type):
37     # 狗的动作
38     def jiao(dog):
39         print('一条狗[%s],汪汪汪' % dog['name'])
40     def chi_shi(dog):
41         print('一条[%s] 正在吃屎' % dog['type'])
42     dog1 = {
43         'name':name,
44         'gender': gender,
45         'type': type,
46         'jiao':jiao,
47         'chi_shi':chi_shi,
48     }
49     return dog1
50 # d1=dog('元昊','母','中华田园犬')
51 # d2=dog('alex','母','藏敖')
52 # print(d1)
53 # print(d2)
54 # d1['jiao'](d1)
55 # d2['chi_shi'](d2)
56 
57  
58 # def dog(name,gender,type):
59 #     # 狗的动作
60 #     def jiao(dog):
61 #         print('一条狗[%s],汪汪汪' % dog['name'])
62 #     def chi_shi(dog):
63 #         print('一条[%s] 正在吃屎' % dog['type'])
64 #     def init(name,gender,type):
65 #         dog1 = {
66 #             'name':name,
67 #             'gender': gender,
68 #             'type': type,
69 #             'jiao':jiao,
70 #             'chi_shi':chi_shi,
71 #         }
72 #         return dog1
73 #     return init(name,gender,type)
74 #
75 # d1=dog('元昊','母','中华田园犬')
76 # d2=dog('alex','母','藏敖')
77 # print(d1)
78 # print(d2)
79 # d1['jiao'](d1)
80 # d2['chi_shi'](d2)

2.面向对象设计练习

 1 def school1(name,addr,type):
 2     def init(name,addr,type):
 3         sch = {
 4             'name':name,
 5             'addr':addr,
 6             'type':type,
 7             'kao_shi':kao_shi,
 8             'zhao_sheng':zhao_sheng
 9         }
10         return sch
11     def kao_shi(school):
12         print('%s 学校正在考试'%school['name'])
13     def zhao_sheng(school):
14         print('%s %s 正在招生'%(school['type'],school['name']))
15     return init(name,addr,type)
16 s1 = school1('将近','sb','肥猪')
17 s1['zhao_sheng'](s1)
18 def school(name,addr,type):
19     def init(name, addr, type):
20         sch = {
21             'name': name,
22             'addr': addr,
23             'type': type,
24             'kao_shi': kao_shi,
25             'zhao_sheng': zhao_sheng,
26         }
27         return sch
28     def kao_shi(school):
29         print('%s 学校正在考试' %school['name'])
30     def zhao_sheng(school):
31         print('%s %s 正在招生' %(school['type'],school['name']))
32     return  init(name,addr,type)
33 
34 # s1=school('oldboy','沙河','私立学校')
35 # print(s1)
36 # print(s1['name'])
37 #
38 # s1['zhao_sheng'](s1)
39 #
40 s2=school('清华','北京','公立学校')
41 
42 print(s2)
43 print(s2['name'],s2['addr'],s2['type'])
44 s2['zhao_sheng'](s2)

3.面向对象展示

 1 #用面向对象编程独有的语法class去实现面向对象设计
 2 class Dog:
 3     def __init__(self,name,gender,type):
 4         self.name=name
 5         self.gender=gender
 6         self.type=type
 7 
 8     def bark(self):
 9         print('一条名字为[%s]的[%s],狂吠不止' %(self.name,self.type))
10 
11     def yao_ren(self):
12         print('[%s]正在咬人' %(self.name))
13 
14     def chi_shi(self):
15         print('[%s]正在吃屎' %(self.type))
16 
17 
18 dog1=Dog('alex','female','京巴')
19 # print(dog1.__dict__)
20 dog2=Dog('wupeiqi','female','腊肠')
21 dog3=Dog('yuanhao','female','藏獒')
22 #
23 dog1.bark()
24 dog2.yao_ren()
25 dog3.chi_shi()

4.类相关知识

 1 # class Chinese:
 2 #     '这是一个中国人的类'
 3 #     pass
 4 #
 5 # print(Chinese)
 6 #
 7 # #实例化到底干了什么?
 8 # p1=Chinese() #实例化
 9 # print(p1)
10 
11 
12 '''
13 1.数据属性
14 2.函数属性
15 '''
16 
17 class Chinese:
18     '这是一个中国人的类'
19     dang='好'
20     def sui_di_tu_tan():
21         print('朝着墙上就是一口痰')
22     def cha_dui(self):
23         print('插到了前面')
24 #
25 # print(Chinese.dang)
26 # Chinese.sui_di_tu_tan()
27 # Chinese.cha_dui('元昊')
28 #
29 # print(dir(Chinese))
30 # print(Chinese.__dict__) #查看属性字典
31 # print(Chinese.__dict__['dang'])
32 # Chinese.__dict__['sui_di_tu_tan']()
33 # Chinese.__dict__['cha_dui'](1)
34 # print(Chinese.__name__)
35 # print(Chinese.__doc__)
36 print(Chinese.__module__)

5.对象相关知识

 1 '''
 2 1.数据属性
 3 2.函数属性
 4 '''
 5 class Chinese:
 6     '这是一个中国人的类'
 7 
 8     dang='公司'
 9 
10     # def __init__(name,age,gender):
11     #     dic={
12     #         'name':name,
13     #         'age':age,
14     #         'gender':gender
15     #     }
16     #     return dic
17     def __init__(self,name,age,gender):
18         # print('我是初始化函数,我开始运行了')
19         self.mingzi=name  #p1.mingzi=name
20         self.nianji=age   #p1.nianji=age
21         self.xingbie=gender
22         # print('我结束啦')
23 
24     def sui_di_tu_tan(self):
25         print('%s 朝着墙上就是一口痰' %self.mingzi)
26     def cha_dui(self):
27         print(self)
28         print('%s 插到了前面' %self.mingzi)
29 
30     def eat_food(self,food):
31         print('%s 正在吃%s' %(self.mingzi,food))
32 
33 p1=Chinese('元昊',18,'female') #--->__init__(self,name,age,gender)
34 # p1.sui_di_tu_tan()
35 # p1.eat_food('屎')
36 # #
37 # p2=Chinese('武sir',10000,'姑娘')
38 # p2.eat_food('韭菜馅饼')
39 # print(dir(p2))
40 #
41 # p1=Chinese.__init__(p1,name,age,gender)
42 
43 # print(p1.__dict__)
44 # print(p1.__dict__['xingbie'])
45 # print(p1.mingzi)
46 # print(p1.mingzi111111111111111)
47 
48 # print(p1.dang)
49 
50 
51 # print(Chinese.__dict__)
52 # Chinese.sui_di_tu_tan(p1)
53 # Chinese.cha_dui(p1)
54 #
55 # p1.sui_di_tu_tan()
56 #
57 # print('[------------------->')
58 # p1.cha_dui()

6.类属性增删改查

 1 class Chinese:
 2     country='China'
 3     def __init__(self,name):
 4         self.name=name
 5 
 6     def play_ball(self,ball):
 7         print('%s 正在打 %s' %(self.name))
 8 #查看
 9 # print(Chinese.country)
10 
11 #修改
12 # Chinese.country='Japan'
13 # print(Chinese.country)
14 #
15 p1=Chinese('alex')
16 # print(p1.__dict__)
17 # print(p1.country)
18 #
19 # #增加
20 Chinese.dang='对的'
21 # #
22 # print(Chinese.dang)
23 # print(p1.dang)
24 #
25 # #删除
26 del Chinese.dang
27 del Chinese.country
28 #
29 print(Chinese.__dict__)
30 # print(Chinese.country)
31 #
32 #
33 def eat_food(self,food):
34     print('%s 正在吃%s' %(self.name,food))
35 
36 Chinese.eat=eat_food
37 #
38 # print(Chinese.__dict__)
39 # p1.eat('屎')
40 #
41 #
42 def test(self):
43     print('test')
44 
45 Chinese.play_ball=test
46 p1.play_ball()# Chinese.play_ball(p1)

7.实例属性增删改查

 1 class Chinese:
 2     country='China'
 3     def __init__(self,name):
 4         self.name=name
 5 
 6     def play_ball(self,ball):
 7         print('%s 正在打 %s' %(self.name,ball))
 8 p1=Chinese('alex')
 9 # print(p1.__dict__)
10 
11 #查看
12 # print(p1.name)
13 # print(p1.play_ball)
14 
15 #增加
16 # p1.age=18
17 # print(p1.__dict__)
18 # print(p1.age)
19 
20 #不要修改底层的属性字典
21 # p1.__dict__['sex']='male'
22 # print(p1.__dict__)
23 # print(p1.sex)
24 
25 #修改
26 p1.age=19
27 print(p1.__dict__)
28 print(p1.age)
29 #
30 # #删除
31 del p1.age
32 print(p1.__dict__)

8.例子

 1 # class Chinese:
 2 #     country='China'
 3 #     def __init__(self,name):
 4 #         self.name=name
 5 #
 6 #     def play_ball(self,ball):
 7 #         print('%s 正在打 %s' %(self.name,ball))
 8 # p1=Chinese('alex')
 9 # print(p1.country)
10 # p1.country='日本'
11 # print('类的--->',Chinese.country)
12 # print('实例的',p1.country)
13 
14 
15 # country='中国'
16 # class Chinese:
17 #     def __init__(self,name):
18 #         self.name=name
19 #
20 #     def play_ball(self,ball):
21 #         print('%s 正在打 %s' %(self.name,ball))
22 # p1=Chinese('alex')
23 # print(p1.country)
24 
25 
26 
27 # country='中国'
28 # class Chinese:
29 #     def __init__(self,name):
30 #         self.name=name
31 #
32 #     def play_ball(self,ball):
33 #         print('%s 正在打 %s' %(self.name,ball))
34 #
35 # def shi_li_hua():
36 #     name=input('>>: ')
37 #     p1=Chinese(name)
38 #     # print(p1.country)
39 #     print(p1.name)
40 # shi_li_hua()
41 
42 
43 
44 # country='中国-------------------'
45 # class Chinese:
46 #     country='中国'
47 #     def __init__(self,name):
48 #         self.name=name
49 #         print('--->',country)
50 # 
51 #     def play_ball(self,ball):
52 #         print('%s 正在打 %s' %(self.name,ball))
53 # 
54 # print(Chinese.__dict__)
55 # print(Chinese.country)
56 # p1=Chinese('alex')
57 # print('实例--------》',p1.country)
58 
59 
60 
61 # Chinese.
62 # p.
 1 # class Chinese:
 2 #     country='China'
 3 #     def __init__(self,name):
 4 #         self.name=name
 5 #
 6 #     def play_ball(self,ball):
 7 #         print('%s 正在打 %s' %(self.name,ball))
 8 # p1=Chinese('alex')
 9 #
10 # print(p1.country)
11 # p1.country='Japan'
12 # print(Chinese.country)
13 
14 class Chinese:
15     country='China'
16     l=['a','b']
17     def __init__(self,name):
18         self.name=name
19 
20     def play_ball(self,ball):
21         print('%s 正在打 %s' %(self.name,ball))
22 p1=Chinese('alex')
23 # print(p1.l)
24 p1.l=[1,2,3]
25 print(Chinese.l)
26 print(p1.__dict__)
27 p1.l.append('c')
28 print(p1.__dict__)
29 print(Chinese.l)

 

上一篇:day24 面向对象与实例属性


下一篇:Java基础学习日志day24(多线程)