day32

 

一、绑定方法

绑定方法两种:

1、绑定给对象

 1 # 绑定给对象的
 2 class Student():
 3     country = 'CHINA'
 4 
 5     def __init__(self, name, age):
 6         self.name = name
 7         self.age = age
 8 
 9     def tell_info(self):
10         print("%s-%s" % (self.name, self.age))
11 
12 
13 obj = Student('egon',  30)
14 obj.tell_info()

 

2、绑定给类

 1 import settings
 2 class Mysql():
 3     country = 'CHINA'
 4 
 5     def __init__(self, ip, port):
 6         self.ip = ip
 7         self.port = port
 8 
 9     def tell_info(self):
10         print('%s-%s' % (self.ip, self.port))
11 
12     # 类方法是绑定给类的,类来调用,把类名当作第一个参数传递
13     @classmethod
14     def from_config(cls):
15         # obj = Mysql(settings.IP, settings.PORT)
16         obj = cls(settings.IP, settings.PORT)
17         return obj
18 
19 obj = Mysql('127.0.0.1', 80)
20 
21 # obj1 = obj.from_config()
22 # print(obj1.__dict__)
23 # print(obj1.country)
24 Mysql.from_config()     # Mysql.from_config(Mysql)
对象与类看情况使用
当对象与类都需要时
1 class Student():
2     def func(self):
3         self       
4         self.__class__        

二、非绑定方法

非绑定方法:不绑定给对象,也不绑定给类

 1 # uuid模块是用来生成不会重复的字符串
 2 class Myaql():
 3     def __init__(self, ip, port):
 4         self.ip = ip
 5         self.port = port
 6 
 7     # @staticmethod   # 静态方法
 8     def create_id(self):
 9         import uuid
10         print(uuid.uuid4())
11 
12 obj = Myaql('127.0.0.1', 3306)
13 obj.create_id()

调用方法参数该怎么传就怎么传

 

三、隐藏属性

1、如何隐藏属性

在类的数据属性变量名,或是在类的函数名前加__,相当于_类名__变量/函数名

2、为什么要隐藏

将类中的属性隐藏,在类中开放对外访问接口(定义一个功能函数),可以更好的对外部做限制

 1 # 类定义阶段,只是语法上的变形
 2 # 该隐藏对外不对内, 如果想访问类中隐藏属性,在类中开放对外访问的接口,可以更好的对外部做限制      # 变形操作只在类定义阶段, 之后的所有属性或者方法,都不会变形
 3 class People():
 4     __country = 'CHINA'     # _People__country 在类数据属性前面加__用于隐藏该属性
 5 
 6     def __init__(self, name, age):
 7         self.name = name
 8         self.age = age
 9 
10     # def __func(self):       #_People__func
11     #     print('func')
12     #
13     # def test(self):
14     #     return self.__country   # self._People__country
15     def get_country(self):
16         return '国家: %s' % self.__country
17 
18     def set_country(self, v):
19         if type(v) is not str:
20             print('country必须是str类型')
21             return
22         self.__country = v
23 
24     def del_country(self):
25         print('不能删除')
26 
27 obj = People('yuziqi', 18)
28 # print(obj.country)      
29 # print(People._People__country)
30 # print(obj._People__country)
31 # print(obj.test())
32 # print(obj.get_country())
33 obj.set_country('japan')
34 print(obj.get_country())
35 obj.del_country()

 

四、统一类和类型的概念

 1 # Python3 中统一了类与类型的概念
 2 
 3 # print(type(obj))
 4 
 5 l1 = list([1,2,3])
 6 l2 = ['a']
 7 
 8 l1.append(4)    # list.append(l1, 4)
 9 list.append(l2, 5)
10 # print(type(l))
11 print(l1)

五、property装饰器

将一个函数功能伪装成一个数据属性

 1 class Mysql():
 2     country = 'CHINA'
 3 
 4     def __init__(self, name, age):
 5         self.__name = name
 6         self.age = age
 7 
 8     @property
 9     def name(self):
10         return self.__name
11     
12     @name.setter
13     def name(self, v):
14         if type(v) is not str:
15             print('必须是str类型')
16             return
17         self.__name = v
18         
19     @name.deleter
20     def name(self):
21         print('不能删除')
22         
23 obj = Mysql('yuziqi', 18)
24 # print(obj.name)
25 print(obj.name)
26 obj.name ='YUZIQI'
27 del obj.name
 1 # 第二种方式
 2 class Mysql():
 3     country = 'CHINA'
 4 
 5     def __init__(self, name, age):
 6         self.__name = name
 7         self.age = age
 8 
 9     def get_name(self):
10         return self.__name
11 
12     def set_name(self, v):
13         if type(v) is not str:
14             print('必须是str类型')
15             return
16         self.__name = v
17 
18     def del_name(self):
19         print('不能删除')
20 
21     xxx = property(get_name, set_name, del_name)
22 obj = Mysql('yuziqi', 18)
23 print(obj.xxx)
24 obj.xxx = 'YUZIQI'
25 print(obj.xxx)
26 del obj.xxx

六、继承

面向对象的三大特征:

1、封装:指一种封装思想

2、继承

3、多态

 

什么是继承:

继承就是一种新建类的方式,新建的类我们称为子类或者叫派生类,被继承的类称为父类或者叫做基类

子类可以遗传父类的属性

 

为什么用继承:

类是解决对象与对象之间的代码冗余

继承是解决类与类之间的代码冗余

 

如何使用类

 1     python支持多继承
 2 
 3     经典类:没有继承object类的子子孙孙类都是经典类
 4     新式类:继承了object以及该类的子子孙孙类都是新式类
 5 
 6     python2中才区分新式类和经典类
 7     python3 中都是新式类
 8 """
 9 
10 # class Parent1:
11 #     pass
12 
13 
14 # class Parent2:
15 #     pass
16 #
17 #
18 # class Sub1(Parent1):
19 #     pass
20 #
21 #
22 # class Sub2(Parent1, Parent2):
23 #     pass
24 
25 # print(Parent1.__base__)
26 #
27 # print(Parent2.__base__)
28 
29 # print(Sub1.__bases__)
30 # print(Sub2.__bases__)
31 
32 # 继承的案例
33 
34 """
35 新知识点:字类如何使用父类的属性
36 方式1:指名道姓, 跟继承没有关系
37 方式2:super() 严格依赖继承
38 """
39 
40 
41 class People():
42     school = 'SH'
43 
44     def __init__(self, name, age, gender):
45         self.name = name
46         self.age = age
47         self.gender = gender
48 
49 
50 # class Test():
51 #     def test(self):
52 #         People.__init__()
53 
54 
55 class Student(People):
56     # 递归
57     def __init__(self, name, age, gender, course=None):
58         if course is None:
59             self.courses = []
60             # self => stu1
61         People.__init__(self, name, age, gender)
62 
63     def choose_course(self, course):
64         self.courses.append(course)
65         print("%s 选课成功 %s" % (self.name, self.courses))
66 
67 
68 class Teacher(People):
69     def __init__(self, name, age, gender, level):
70         People.__init__(self, name, age, gender)
71         self.level = level
72 
73     def score(self, stu_obj, num):
74         stu_obj.num = num
75         print("%s老师给%s打了%s分" % (self.name, stu_obj.name, num))
76 
77 
78 stu1 = Student('tom', 19, 'male')
79 print(stu1.school)
80 print(stu1.name)
81 print(stu1.age)
82 
83 tea1 = Teacher('tom', 19, 'male', 10)
84 print(tea1.school)
85 print(tea1.name)
86 print(tea1.age)

 

 

------------恢复内容结束------------

posted @ 2021-07-14 14:58  Gnomeshghy  阅读(52)  评论(0)    收藏  举报