Day 18 Object-Oriented Programming Thoughts Object-Oriented Grammar Object-Oriented Encapsulation Features Hidden Properties

Object-oriented programming and process-oriented programming differences :
process-oriented programming: simplification of complex issues, poor scalability.
Object-oriented programming: complex reading becomes higher, and scalability is strong.

Object-oriented programming : the
core is the word object, which is a container used to hold data and functions.
Writing a program based on this idea is to create a container.

Special syntax :
class
object
attribute lookup

Three object-oriented features :
encapsulation: categorize and integrate, encapsulate related data and functions, which well reflects the characteristics of strong scalability.
Inheritance
, polymorphism

Instantiation :
The process of invoking a class to produce an object is called instantiation.
Invoking a class is called instantiation of the
class. The result of invoking a class is an object, and this object is called an instance of the class.

Object-oriented programming ideas

def tell_info(aaa):
    print("my name is %s, my age is %s,my gender is %s" % (student["name"], student["age"], student["gender"]))


student = {
    
    "name": "nana",
           "age": 18,
           "gender": "female",
           "tell_info": tell_info}    #学生对象,学生容器,self自己调自己的功能

student["tell_info"](student)

案例2:
类:用来存放相似数据,已经相似功能的容器,用来节省内存空间
def tell_info(aaa):
    print("my name is %s, my age is %s,my gender is %s" % (student["name"], student["age"], student["gender"]))

student = {
    
    
    "school": "清华大学",
    "name": "nana",
    "age": 18,
    "gender": "female",
    "tell_info": tell_info}  # 学生对象,学生容器,self自己调自己的功能

student["tell_info"](student)

student1 = {
    
    
    "school": "清华大学",
    "name": "dada",
    "age": 19,
    "gender": "male",
    "tell_info": tell_info}  # 学生对象,学生容器,self自己调自己的功能

student["tell_info"](student1)

The relationship diagram between classes and objects
Classes have two properties: data properties and function properties

  1. The data attributes of the class are shared by all objects

  2. The function attribute of the class is bound to the object.
    Insert picture description here
    obj.name will first look for the name from obj's own namespace, if it can’t find it, it will go to the class, and if the class is not found, it will look for the parent class... Throw an exception when it arrives

Principles of classes and objects
Define what happens to
classes. Classes and objects are essentially namespaces. Objects contain elements of a unique type in a dictionary. Classes contain elements of the same type.
The difference between the definition of classes and functions, and the namespace of functions The name space is generated only when it is called. As long as the class is defined, the code will be run to generate the name space.
Class name: Camel case

Classes and objects
Objects are containers for relevant data and functions.
Classes are containers for data and functions similar to objects.

class Student:
    school = "清华大学"

    def tell_info(student):
        print("my name is %s, my age is %s,my gender is %s" % (student["name"], student["age"], student["gender"]))

    print("=====")

# print(Student.__dict__)     #class就是在背后把Student的同类型元素做成了一个字典的模式,__dict__表示查看类里面的名称空间的元素

#调用类的底层逻辑
# 创造一个对象的字典格式,用来存放对象独有的数据
# 将对象与类建立好关联
obj1 = Student()
# print(obj1.__dict__)  #建了一个空字典
# obj1.name = "nana"  # obj1.__dict__["name"] = "nana"
# obj1.age = 18  # obj1.__dict__["age"] = 18
# obj1.gender = "female"  # obj1.__dict__["gender"] = "female"
#
obj2 = Student()
# obj2.name = "dada"  # obj2.__dict__["name"] = "dada"
# obj2.age = 19  # obj2.__dict__["age"] = 19
# obj2.gender = "male"  # obj2.__dict__["gender"] = "male"

# 把对象写成函数
def init(self, x, y, z):
    self.name = x
    self.age = y
    self.gender = z


init(obj1, "nana", 18, "female")
init(obj2, "dada", 19, "male")

print(obj1.__dict__)
print(obj2.__dict__)
print(Student.__dict__)

The process of calling the class

class Student:
    school = "清华大学"

    def __init__(self, x, y, z):         #Student.__init__(空对象,"nana", 18, "male")
        self.name = x      # 空对象.name="nana"
        self.age = y       # 空对象.age=18
        self.gender = z    # 空对象.gender="female"

    def tell_info(student):
        print("my name is %s, my age is %s,my gender is %s" % (student["name"], student["age"], student["gender"]))


调用类的过程
1.先创造一个空对象
2.自动触发类内的__init__函数的运行,将空对象当作第一个参数自动传入
3.返回一个初始化好的对象给obj1  #注意,obj1返回值是Student()的返回值,__init__规定不能有返回值
obj1 = Student("nana", 18, "female")  # 返回值obj1={
    
    "name"="nana","age"=18,"gender"="female"}
obj2 = Student("dada", 19, "male")

Attribute lookup

对象.属性的查找顺序,优先从对象里面找,如果找不到会再从类里面找
# class Student:
#     school = "清华大学"
#
#     def __init__(self, x, y, z):
#         self.name = x
#         self.age = y
#         self.gender = z
#
#     def tell_info(student):
#         print("my name is %s, my age is %s,my gender is %s" % (student.gender, student.age, student.gender))
#
#
# obj1 = Student("nana", 18, "female")
# obj2 = Student("dada", 19, "male")
#
# Student.tell_info(obj1)
# Student.tell_info(obj2)

# print(obj1.name)  # obj1.__dict__["name"]
# print(obj1.school)

# 类.属性,从类自己的字典里面找,类内部传参,无法去全局名称空间里面找值,只能再类内部找,类内部找不到就报错
# print(Student.school)    # 显示类里面school的值
# print(Student.__init__)  # 函数Student.__init__的内存地址<function Student.__init__ at 0x030B9538>
# print(Student.tell_info)  # 函数tell_info的内存地址<function Student.tell_info at 0x015F9460>

# 类中的数据属性是直接共享给所有对象用的
# 如果类的数据属性变了,那就所有数据的属性都改变,如果对象的数据属性改变了,那么只在该数据只在该对象内部发生改变
# obj1.school = "xxx"
# print(Student.school)  # 清华大学
# print(obj2.school)  # 清华大学
# print(obj1.school)  # xxx

Function attributes in the
class The function in the class can be called directly. If the class is called, it is an ordinary function. It can be used according to the usage rules
of the function. But in fact, the function in the class is for the object. If the object is called, it is A binding method, the binding method will pass the caller as the first parameter

class Student:
    school = "清华大学"

    def __init__(self, x, y, z):
        self.name = x
        self.age = y
        self.gender = z

    def tell_info(student):
        print("my name is %s, my age is %s,my gender is %s" % (student.gender, student.age, student.gender))


obj1 = Student("nana", 18, "female")
obj2 = Student("dada", 19, "male")

Student.tell_info(obj1)
Student.tell_info(obj2)
print(obj1.tell_info)   #<bound method Student.tell_info of <__main__.Student object at 0x00EAE6D0>>
绑定方法的概念
类中定义的函数一般都至少需要一个参数的,该参数是往定义的函数里面自动传入对象的,这种绑定关系就是绑定方法
obj1.tell_info()    #tell_info()会将调用者当作第一个参数传入,如果函数中还有其他参数,那么继续传其他的参数
obj2.tell_info()



The characteristics of the attribute at the beginning of the hidden attribute __: It
is not really hidden, but it is deformed.
This transformation is only performed during the class definition stage and when scanning the grammar. After that, the attributes at the beginning of __ will not be deformed.
It should be hidden. Inside

class People:
#     __country = "China"  # 将一个属性加了__开头就代表把该属性隐藏了
    def __init__(self, name, age):
        self.__name = name  # {
    
    '_People__name': 'nana'}
        self.__age = age  # '_People__age': 18

    def __func(self):  # _People__func
        print("xx")

    def tell_name(self):
        print(self.__name)  # {
    
    '_People__name': 'nana'}
        # self["__name"]

obj1 = People("nana", 18)
obj1.tell_name()

print(People.__country)    #加了__显示没有该属性
print(People.__dict__)     #__country显示结果:'_People__country': 'China'
print(People._People__country)
print(obj1.__dict__)

obj1.__gender = "female"
print(obj1.__gender)  # 可以直接访问到,female
obj1.tell_name()  # 对外不对内

Why do you want to hide attributes

1.隐藏数据属性为了严格控制类外部访问者对属性的操作
class People:
    def __init__(self, name, age):
        self.__name = name  # _People__name
        self.__age = age  # _People__age

    def tell_info(self):				#通过改接口的方式让用户只能查看
        print(self.__name, self.__age)		

    def set_info(self, name, age):				#通过调用函数改接口的的方式控制用户对属性的更改     
        if type(name) is not str:
            print("名字必须是字符串")
            return
        if type(age) is not int:
            print("年龄必须是数字")
            return
        self.__name = name
        self.__age = age


obj1 = People("nana", 18)
# obj1.tell_info()         
obj1.set_info("NANA", 18)
obj1.tell_info()

2.隐藏函数(功能)属性为了隔离复杂度
class ATM:
    def __card(self):
        print("插卡")
    def __auth(self):
        print("用户认证")
    def __input(self):
        print("取出")

    def withdraw(self):
        self.__card()
        self.__auth()
        self.__input()

a = ATM()
a.withdraw()

Guess you like

Origin blog.csdn.net/Yosigo_/article/details/112383310