Object oriented python 21

1. Object-oriented preliminary understanding

面向过程编程VS函数编程:减少重复代码,增强了可读性
面向对象编程VS函数编程优点:
    ①对相似功能的函数,同一个业务的函数进行归类、分类;
    类:具有相同属性和功能的一类事物
    对象:类的具体体现(具体到一个事物),对象间是不同的。
    ②要站在上帝的角度构建代码,类就是一个公共的模板,对象就是从模板实例化出来的,得到对象就是得到了一切。

2. The object-oriented structure

class 类名:   # 类名用驼峰法,首字母大写,不可用下划线
    属性、方法
obj = 类名()  # 实例化对象
# example
class Human:
    mind = "有思想"    # 类的属性(静态属性、静态字段)
    language = "语言"
    def work(self):         # 动态方法
        print("都会工作")
    def eat(self):
        print("都能吃饭")

3. From the perspective of the class name class research

3.1 class name attribute operation class

3.1.1 class name to view all the content in class

print(Human.__dict__)  # 方法会显示地址

3.1.2 The name of the class action class static properties omnipotent point

# 增
Human.body = "有头"       # 类名.变量名="值"
# 删
def Human.mind 
# 改
Human.mind = "学习"       # 类名.变量名="改的值"
# 查
print(Human.language)

3.2 class name to call methods in the class

General class method will not be invoked through the class name.

Human.work("参数")  # 必须传参,不用,没有意义

# 总结  
一般类名就是操作类中的属性。

4. Study of the angle from the object class

4.1 class name ()

类名():实例化过程,会得到一个返回值。返回值就是对象、实例。

实例化一个对象发生了三件事:
    1. 开辟一个对象空间;(和类空间不同)
    2. 自动执行__init__方法,并且将对象空间的地址传给self,属于隐形传参;(此时对象空间有两个名称:self、obj)
    3. 运行__init__方法内的代码,给对象空间封装属性(与类空间无关,Human.__dict__无法显示对象中封装的属性。)
    
通过传参可以向对象内封装属性、传值。  

1564737358574

class Human:
    mind = "有思想"
    language = "语言"
    def __init__(self):
        self.name = name
        self.age = age
        
    def work(self):
        print("能够工作")

    def eat(self):
        print("可以吃饭")
obj = Human()

4.2 the operation target object space properties

4.2.1 Object object space to view all properties

print(obj.__dict__)   # 以字典的形式

4.2.2 Object attribute operation target space (external operation)

class Human:
    mind = "有思想"
    language = "语言"
    def __init__(self,name,age):
        self.name = name
        self.age = age

    def work(self):
        print("能够工作")

    def eat(self):
        print("可以吃饭")
obj = Human("meet",30)
# 增
obj.sex = "男"
# 删
del obj.name
# 改
obj.age = 18
# 查
print(obj.age)

4.2.3 Object view class Properties

Only view, not modify class attributes, otherwise it will add to the object's properties. For variable data types, such as a list, or the object class name may be CRUD (same address) to the element within the list.

print(obj.mind)

4.2.4 object calls methods in the class

class Human:
    mind = "有思想"
    language = "语言"
    def __init__(self,name,age):
        self.name = name
        self.age = age

    def work(self):
        print(f"{self.name}今年{self.age}岁,能够工作")

    def eat(self):
        print(f"{self.name}今年{self.age}岁,可以吃饭")
obj = Human("meet",30)
obj.work()
obj.eat()
self:本身来说就是类中的方法的第一个参数,会自动接收对象的内存地址。

5. A plurality of objects can be instantiated class

1564740652625

class Human:
    mind = "有思想"
    language = "语言"
    def __init__(self,name,age):
        self.name = name
        self.age = age

    def work(self):
        print(f"{self.name}今年{self.age}岁,能够工作")

    def eat(self):
        print(f"{self.name}今年{self.age}岁,可以吃饭")
obj = Human("meet",30)
obj.work()
obj.eat()
obj1 = Human("alex",18)
obj1.work()
obj1.eat()
obj2 = Human("dsb0",20)
obj2.work()
obj2.eat()

Guess you like

Origin www.cnblogs.com/yzm1017/p/11313555.html