s21day19 python notes

Keywords: Python encoding

I. Basic Object-Oriented Knowledge

1.1 Basic Format

# Definition class 
class Class name:
    def Method name(self,name):
        print(name)
        return 123
    def Method name(self,name):
        print(name)
        return 123
    def Method name(self,name):
        print(name)
        return 123
    
# Call methods in classes
# 1. Create objects of this class
obj = Class name()
# 2. Calling methods through objects
result = obj.Method name('alex')
print(result)

1.2 Application Scenario

  1. Application Scenario: There are many functions that need to be categorized and partitioned.

  2. When do you use object-oriented?
    • There are many functions (business functions) that can be categorized using object-oriented methods
    • Want to do data encapsulation (object-oriented when creating dictionaries to store data)
    • Game Example: Create Characters and Recreate Characters as Characters Need

Two. Three Object-Oriented Characteristics

2.1 encapsulation

  1. Object: Store some values for your own convenience in the future

    class File:
        def read(self):
            with open(self.xxxxx, mode='r', encoding='utf-8') as f:
                data = f.read()
            return data
    
        def write(self, content):
            with open(self.xxxxx, mode='a', encoding='utf-8') as f:
                f.write(content)
    
    # # Instantiate an object of File class
    obj1 = File()
    # # Write an xxxxx ='test.log'in the object
    obj1.xxxxx = "test.log"
    # # By calling the read method in the class, the self in the read method is obj.
    # # obj1.read()
    obj1.write('alex')
    
    
    # Instantiate an object of File class
    obj2 = File()
    # Write an xxxxx ='test.log'in the object
    obj2.xxxxx = "info.txt"
    # By calling the read method in the class, the self in the read method is obj.
    # obj2.read()
    obj2.write('alex')
  2. The function of encapsulation: encapsulating data into objects for easy use

    class Person:
        def __init__(self,n,a,g): # The initialization method (construction method) initializes the interior of the object.
            self.name = n
            self.age = a
            self.gender = g
    
        def show(self):
            temp = "I am%s,Age:%s,Gender:%s " % (self.name, self.age, self.gender,)
            print(temp)
    
    # Class () instantiates the object and automatically executes the _init_ method in this class.
    p1 = Person('Li Zhao Qi',19,'male')
    p1.show()
    
    p2 = Person('Leqi Airlines',19,'male')
    p2.show()
  3. summary

    • If you write code, functions are more messy

      • Functions can be categorized and placed in the same class

      • Functions can be placed in objects if they have a common value that is used repeatedly

    # Example: The loop lets the user enter: username/password/mailbox. Print the data after the input is completed.
    # Previous wording:
    USER_LIST = []
    while True:
        user = input('Please enter a user name:')
        pwd = input('Please input a password:')
        email = input('Please enter your email address:')
        temp = {'username':user,'password':pwd,'email':email}
        USER_LIST.append(temp)
    for item in USER_LIST:
        temp = "My name:%s,Password:%s,mailbox%s" %(item['username'],item['password'],item['email'],)
        print(temp)
    
    # Object-Oriented Writing 1:
    class Person:
        def __init__(self,user,pwd,email):
            self.username = user
            self.password = pwd
            self.email = email
    
    USER_LIST = [object(user/Password/mailbox),object(user/Password/mailbox),object(user/Password/mailbox)]
    while True:
        user = input('Please enter a user name:')
        pwd = input('Please input a password:')
        email = input('Please enter your email address:')
        p = Person(user,pwd,email)
        USER_LIST.append(p)
    for item in USER_LIST:
        temp = "My name:%s,Password:%s,mailbox%s" %(item.username,item.password,item.email,)
        print(temp)
    
    # Object-oriented Writing II:
    class Person:
        def __init__(self,user,pwd,email):
            self.username = user
            self.password = pwd
            self.email = email   
     def info(self):
            return "My name:%s,Password:%s,mailbox%s" %(item.username,item.password,item.email,)
    
    USER_LIST = [object(user/Password/mailbox),object(user/Password/mailbox),object(user/Password/mailbox)]
    while True:
        user = input('Please enter a user name:')
        pwd = input('Please input a password:')
        email = input('Please enter your email address:')
        p = Person(user,pwd,email)
        USER_LIST.append(p)
    for item in USER_LIST:
        msg = item.info()
        print(msg)

2.2 inheritance

  1. basic content

    # Parent class (base class)
    class Base:
        def f1(self):
            pass
    # Subclass (derived class)
    class Foo(Base):
        def f2(self):
            pass
    
    # Create an object of a subclass
    obj = Foo()
    # To execute an object. Method, first find in its own class, if not in its parent class.
    obj.f2()
    obj.f1()
    
    # Create an object of a parent class
    obj = Base()
    obj.f1()
  2. summary

    • When will inheritance be used?
      • If there are common methods in multiple classes, they can be placed in base classes to avoid duplication.
    • When looking up the sequence of methods in inheritance relations, the following points should be noted:
      • Who is self?
      • Which class self was created? Start with this class and find the parent class if you don't have it.
    • Multi-inheritance, if a derived class has more than one base class, executing the object. Method, priority is found in its own class, if not, from left to right in turn from the base class.

2.3 polymorphism

  1. Basic Contents: Various Forms/Various Types, Duck Model

    def func(arg): # Various types, many things
        arg.send() # Must have send method, croak
  2. Interview question: What is a duck model?

    For a function, Python does not restrict the type of parameter, so it can be various types when passing in parameters. If there is an arg.send method in a function, it is a restriction on the type of input (type must have send method). This is the duck model.

    Similar to the functions mentioned above, we think that as long as we can quack, it's the duck (the only type we want is the send method).

ps: day18 test did not learn new content, did not write notes

Posted by xkellix on Tue, 23 Apr 2019 16:57:36 -0700