day 23 面向对象中类的成员 和嵌套

1、类的成员? 变量、方法、属性

 1 class Foo:
 2     
 3     # 方法
 4     def __init__(self,name):
 5         # 实例变量/字段
 6         self.name = name     
 7         
 8     # 方法
 9     def func(self):
10         pass 
11 
12 # obj,Foo类的对象
13 # obj,Foo类的实例
14 obj = Foo('朱奎峰')
View Code

  变量:1、实例变量(字段)

        2、类变量(静态字段)

 1 实例一:
 2 class Foo:
 3     # 类变量(静态字段)
 4     country = "中国"
 5     
 6     def __init__(self,name):
 7         # 实例变量(字段)
 8         self.name = name  
 9     
10     def func(self):
11         pass
12 
13 obj1 = Foo('季红')
14 obj2 = Foo('王晓东')
15 
16 Foo.country
17 
18 思考题: 如何验证儿子都不知道私有字段的存在.
19 
20 无法访问:
21 class Base(object):
22     __secret = "受贿"
23 
24 class Foo(Base):
25 
26     def func(self):
27         print(self.__secret)
28         print(Foo.__secret)
29 
30 obj = Foo()
31 obj.func()
32                 
33 可以访问:
34 class Base(object):
35     __secret = "受贿"
36 
37     def zt(self):
38         print(Base.__secret)
39 
40 class Foo(Base):
41 
42     def func(self):
43         print(self.__secret)
44         print(Foo.__secret)
45 
46 obj = Foo()
47 obj.zt()
实例

  

  方法:1、实例方法  

       2、静态方法

       3、类方法    @classmethod

 1 - 实例方法
 2     class Foo(object):
 3         def __init__(self, name):
 4             self.name = name
 5 
 6         # 实例方法
 7         def func(self):
 8             print(self.name)
 9             
10     obj = Foo('..')
11     obj.func()
12     
13 - 静态方法
14     class Foo(object):
15         def __init__(self, name):
16             self.name = name
17 
18         # 静态方法,如果方法无需使用对象中封装的值,那么就可以使用静态方法
19         @staticmethod
20         def display(a1,a2):
21             return a1 + a2
22     Foo.display(1,3)
23     
24 - 类方法
25     class Foo(object):
26     
27         # 类方法,cls是类
28         @classmethod
29         def show(cls,x1,x2):
30             print(cls,x1,x2)
31 
32     # 执行类方法
33     Foo.show(1,8)
34 
35 面试题: 静态方法/类方法和实例方法的区别?
36 
37 
38 属性(通过方法改造出来):
39 示例:
40 class Foo(object):
41     def __init__(self):
42         pass
43 
44     @property
45     def start(self):
46         return 1
47 
48     @property
49     def end(self):
50         return 10
51 
52 obj = Foo()
53 print(obj.start)
54 print(obj.end)
55 """
56     # 总结:
57     #     1. 编写时
58     #            - 方法上方写 @property
59     #            - 方法参数:只有一个self
60     #     2. 调用时:无需加括号  对象.方法
61     #     3. 应用场景: 对于简单的方法,当无需传参且有返回值时,可以使用 @property
62 """
View Code
@staticmethod  静态方法 ,如果方法无需使用对象中封装的值,那么就可以使用静态方法@staticmethod,放在定义的函数前面
def display(a1,a2):   

  

  属性: 通过方法改造出来

 1 class Foo(object):
 2     def __init__(self):
 3         pass
 4 
 5     @property
 6     def start(self):
 7         return 1
 8 
 9     @property
10     def end(self):
11         return 10
12 
13 obj = Foo()
14 print(obj.start)
15 print(obj.end)
16 """
17 # 总结:
18 #     1. 编写时
19 #            - 方法上方写 @property
20 #            - 方法参数:只有一个self
21 #     2. 调用时:无需加括号  对象.方法
22 #     3. 应用场景: 对于简单的方法,当无需传参且有返回值时,可以使用 @property
23 """
View Code

2、静态方法、类方法和实例方法的区别?

  定义 静态方法、类方法定义的时候需要添加@   ,实例方法不需要添加
  执行 静态方法、类方法执行时通过类. 可以直接调用,  实例方法 先实例化,然后通过对象. 调用
  应用场景?
    如果在方法内部不会用到对象相关的数据时,就可以用静态 和类
    如果需要用到对象 用实例方法
    如果在代码中会用到当前类,为了省事可以用类方法

3、属性:

属性总结

嵌套

 1 面向对象:
 2 """
 3 创建三个学校且三个学校的设施内容等都是一致.
 4 """
 5 
 6 class School(object):
 7     def __init__(self, name, address):
 8         self.name = name
 9         self.address = address
10 
11     def speech(self):
12         print('讲课')
13 
14 obj1 = School('老男孩北京校区', '美丽富饶的沙河')
15 obj2 = School('老男孩上海校区', '浦东新区')
16 obj3 = School('老男孩深圳校区', '南山区')
17 class Teacher(object):
18     def __init__(self, name, age, salary):
19         self.name = name
20         self.age = age
21         self.__salary = salary
22         self.school = None
23 
24 t1 = Teacher('李杰', 19, 188888)
25 t2 = Teacher('艳涛', 18, 60)
26 t3 = Teacher('女神',16, 900000)
27 # ############## 老师分配校区
28 t1.school = obj1
29 t2.school = obj1
30 t3.school = obj2
31 # ####################################
32 # 查看t1老师,所在的校区名称/地址
33 print(t1.school.name)
34 print(t1.school.address)
35 print(t1.name)
36 print(t1.age)
37 t1.school.speech()
38 
39 准则: 字段和方法的归类.
40 
41 以前:
42 [
43     {"name":'alex1','age':18,xxx:999},
44     {"name":'alex2',age':18},
45 ]
View Code
原文地址:https://www.cnblogs.com/xiaobai686/p/11747786.html