day15: object-oriented

Catalog

1. Object-oriented

Object-oriented, process-oriented programming
Object-oriented features: encapsulation, inheritance, polymorphism
All classes in py inherit from a parent class

1. Create classes using classes

#Class class
class Person:
   pass

Add attributes to the class:
#Properties
name = "js"
age = 18
hair = "black"
Add methods to classes:
def tell(self)

#Class class
class Person:
    #attribute
    name = "js"
    age = 18
    hair = "black"
    
    #Method self: whoever calls it is whoever
    def tell(self):
        print("my name is %s,age %s,hair is %s"%(self.name,self.age,self.hair))
    pass

2. Instantiate Objects

Instantiate an object using the class name ()

p = Person()
print(p.name)
p.tell()

3. Using construction methods

A construction method that initializes an instance at instantiation time

#Class class
class Person:
    #attribute
    name = "js"
    age = 18
    hair = "black"
    #Construction method: called automatically when instantiated
    #If a constructor is defined, there is no default constructor
    def __init__(self,name,age):
        self.name = name
        self.age = age
    #Method self: whoever calls it is whoever
    def tell(self):
        print("my name is %s,age %s,hair is %s"%(self.name,self.age,self.hair))
    pass
#Class name () indicates instantiating an object
p = Person("js",18)
p.tell()
p1 = Person("LY",14)
p1.tell()

4. Accessing the original properties

Access through Person.

#Access original properties:
print(Person.name)
print(Person.age)
Person.tell(p1) #self can pass in instances

Be careful:
You can see that the attributes in the class are different from those in the instance.
Call class properties on a property that is not in the incoming parameter when called

5. Destructors

Called automatically when an instance is recycled

class Person:
    def __init__(self,name,age):
        self.name = name
        self.age = age
    #Destructor, called automatically when an instance is recycled
    def __del__(self):
        print("Destructor Call")
    def tell(self):
        print("mtname is %s,age %s"%(self.name,self.age))

p = Person("sj",18)
p.tell()
"""
//When the program ends, the destructor is called
"""

6. Use private properties, methods

Private properties and methods can be set with u for example
__name
def __setName(self,name):

Once private is set, it can only be called through public

class Person:
    def __init__(self,name,age):
        #Add two underscores to indicate private properties and cannot be accessed directly from outside
        self.__name = name
        self.__age = age
    def getAge(self):
        return  self.__age
    def setAge(self,age):
        self.__age = age
    #Private methods can be set, as can public methods only
    def __setName(self,name):
        self.__name = name
    def setNamePass(self):
        pwd = 123
        res = int(input("Please input a password:"))
        if res == pwd:
            self.__setName("haha Successful modification")
        else:
            print("pass word error")
    def tell(self):
        print("mtname is %s,age %s"%(self.__name,self.__age))

p = Person("sj",18)
p.tell()
print(p.getAge())
p.setAge(10)
p.tell() #You can see that the modification was successful

p.setNamePass()
p.tell()

7. Inheritance, Rewrite and Expansion

Inheritance, parentheses, so you can have the method properties of the parent class, reducing duplication of work

#Inheritance, parentheses filled in the parent class, such as
class MtList(list):
    pass

In inheritance, classes in other files are often referenced
Functions can only be imported by if name =='main', which is common when inheriting

A small exercise is as follows:

from day15.Object-oriented import Person
#Only functions can be imported by if u name_u =='u main_u'.
p = Person("LS",18)
p.tell()

class Basterd(Person):
    #Method expansion, while preserving parent methods
    def __init__(self,name,age,badhobby):
        super().__init__(name,age)
        self.badhobby = badhobby
    #Method override, completely overriding parent method
    def tell(self):
        print("My name is%s,age %s,I love%s"%(self.name,self.age,self.badhobby))
"""
//Create a Bad Bomb class,
//A bad egg is a person,
//A bad egg has his special attributes and hair style
"""
if __name__ == '__main__':
    p = Basterd("ly",18,"Fight")
    p.tell()

Among them, init() uses extensions.
tell() is directly overridden

8. Small exercises

class Clip:
    def __init__(self,bullet):
        self.bullet = bullet
class Gun:
    def __init__(self,clip):
        self.clip = clip
    def fier(self,n):
        if self.clip.bullet > n:
            self.clip.bullet -= n
            print("Shoot; left%s Bullet"%(self.clip.bullet))
            for i in range(n):
                print("tu~")
        else:
            print("Not enough bullets, go on!")
class Person:
    def __init__(self,gun):
        self.gun = gun
    def fill(self,n):
        self.gun.clip.bullet += n
        print("Bounced successfully, and%s hair"%(self.gun.clip.bullet))
from day15.clip import *
from day15.gun import *
from day15.Person import *

c = Clip(10)
g = Gun(c)
p = Person(g)
for i in range(4):
    p.gun.fier(3)
p.fill(5)
for i in range(4):
    p.gun.fier(3)
"""
//Running effect
//Shot; 7 bullets left
tu~
tu~
tu~
//Shot; 4 bullets left
tu~
tu~
tu~
//Shot; 1 bullet left
"""
18 original articles published. Zambia 1. Visits 916
Private letter follow

Tags: Attribute Programming

Posted on Wed, 11 Mar 2020 23:01:06 -0400 by NerdConcepts