Object-Oriented Theory Summary

One: Classes and Objects

A class is a class of things

Object: concrete thing

For example, people are a class, and the specific name is an object.

In python, variables are used to represent features, and functions are used to represent skills, so a class of things with the same features and skills is a 'class', and an object is a specific one of this class of things.

One: first acquaintance

Declare a class class lowercase. The class name is capitalized and added:, () can be written or not

class Person: #Define a human Person is the class name
    role = 'person' #The role attributes of people are all people
    def walk(self): #Everyone can walk, that is, there is a walking method, also called dynamic property
        print("person is walking...")

Two roles of classes: attribute reference and instantiation

One: attribute reference (class name. attribute)

class Person: # define a person
    role = 'person' # The role attributes of people are all people

    def walk(self): # Everyone can walk, that is, there is a walking method
        print("person is walking...")

print(Person.role) # View the role attribute of the person
print(Person.walk) # Refers to the walking method of people, note that this is not calling

Two: Enhanced version (class role, theoretical knowledge)

class Person:    # 类名 Person
    role = 'person' # static variable of class is a property shared by all objects
    def attack(self): pass
 print(Person.role) # The calling method provided to us by the class
print(Person.attack)
Person.role = 'Chinese'
print(Person.role)
Person.attack() # error
print(Person.__dict__) # Storage method
print(Person.__dict__['role'])
print(Person.__dict__['attack'])
Person.__dict__['role'] = '人'

Classes have only two functions:
    1. Use the name in the class
        Check out the names in the class:
        Class name. Variable name # You can modify the value of the variable
        Class name.__dict__['variable name'] # Cannot be modified
    2. Create Object Instantiate Object
        Objects are instances, a practical example
        object = classname()
class Person:    # 类名 Person
    role = 'person' # static variable of class is a property shared by all objects
    def __init__(self,name,sex,aggr,hp):  #{}
        self.name = name
        self.sex = sex
        self.aggr = aggr
        self.hp = hp
def attack(self):    
        print(self.name)
        print(self.sex)

obj1 = Person('alex','female',250,1)
obj2 = Person('egon','male',500,2)
print(obj1.name,obj1.sex,obj1.aggr,obj1.hp)
print(obj2.name,obj2.sex,obj2.aggr,obj2.hp)
print(obj1.__dict__)
print(obj2.__dict__)
print(obj1)
Person.attack(obj1) # Call a method in a class
obj1.attack()
Object specifics:
    memory address inconsistency
    Different object properties should have different values

class name (parameter)
    Creates an empty object: self
    Call __init__ method: initialization method
    Pass parameters to the __init__ method
    Complete the code in the init method
    Automatically returns self to where it was instantiated

self is the object

Call a method in the class:
    classname.methodname(object)
    ObjectName.MethodName()

Three: Summary

#initial object oriented
'''
class Person:
    role = 'person' # static variable of class is a property shared by all objects
    def __init__(self):
       pass
    def attack(self):
1. Class: a class of transactions, such as people, computers, etc.
 Object: a specific matter, such as Xiao Ming, Xiao Ming's mobile phone
2. Define a class: class Person(): The first letter of the class name Person should be capitalized () can be written or not
              Add a shared attribute to the class role = 'person' The static variable of the class is an attribute shared by all objects
Use print(Person.role) when calling
3. View the name in the class:
3.1 The calling method provided by the print(Person.role) class can be modified, such as Person.role = 'Chinese'
3.2 The storage method provided by the print(Person.__dict__) class will display all the names and methods in the class
print(Person.__dict__['role'])
4. The class has only two functions:
4.1. Using names in classes
    View the name in the class: 1. Class name. Variable name# You can modify the value of the variable
                  2. Class name.__dict__['variable name'] # Cannot be modified
4.2. Creating Objects Instantiating Objects
    Objects are instances, a practical example
    object = classname() #obj1=person()
5. Instantiate:
1.obj1 = Person('alex','female',1,250) # Instantiate the object first and then initialize
print(obj1.name,obj1.sex,obj1.aggr,obj1.hp)
print(obj2.name,obj2.sex,obj2.aggr,obj2.hp)
print(obj1.__dict__)
2. Call the __init__ method: initialization method
    def __init__(self,name,sex,aggr,hp): #The built-in double down method of method dynamic attributes is the object self
        self.name = name self is understood to be equal to an empty dictionary, self={} name in self.name is the key value, and the right side of the equal sign is the value value
        self.sex = sex name, sex, etc. are attributes
        self.aggr = aggr
        self.hp = hp
6. The specifics of the object:
print(obj1) #object at 0x000001B8F1378A90>
print(obj2) #object at 0x000001B8F1378AC8>
    1. Inconsistency of memory addresses
    2. The value of different object properties should be different
7. Class name (parameter)
    1. Create an empty object: self (ie obj1)
    2. Call the __init__ method: initialization method
    3. Pass parameters to the __init__ method
    4. Complete the code in the init method
    5. Automatically returns self to the place where it is instantiated, self is the object
8. Call the method in the class:
 def attack(self): # custom method
        print(self.name)
1. Class name. Method name (object) Person.attack(obj1)
2. Object name. Method name() obj1.attack() def attack(self): self is obj1
9. Use object-oriented steps:
1. Completing the class
First, analyze what attributes init methods exist in roles and classes.
Then analyze what behaviors in this class can be defined as methods
2. Instantiate
Pass parameters to get the object after instantiation
    1. First create the object,
    2. Execute the init method
3. Object call: use the object to call object properties and methods in the class
The role of the object name:
    1. Use variable object name. Property name obj1.name
    2. Call method object name. method name () obj1.attack ()
'''
####Man and dog battle
class Person:    # 类名 Person
    role = 'person' # static variable of class is a property shared by all objects
    def __init__(self, name , sex , aggr , hp ): #The built-in double down method of method dynamic attributes
        self.name = name     # object attribute instance attribute
        self.sex = sex
         self.aggr = aggr
         self.hp = hp
 def attack(self, dog ): # custom method    
        print('%s打了%s'%(self.name,dog.name))
        dog.hp -= self.aggr

class Dog:
    def __init__(self,name,kind,aggr,hp):
        self.name = name    # object property
        self.kind = kind
        self.aggr = aggr
        self.hp = hp
def bite(self,person):    
        print('%s咬了%s'%(self.name,person.name))
        person.hp -= self.aggr
hei = Dog('Xiaohei','teddy',260,10000) # Instantiation: first create an object and then initialize
alex = Person('alex','female',1,250) # instantiate
egon = Person ('egon','male', 2,500) # Instantiation
alex.attack(hei)   # Person.attack(alex,hei)
egon.attack (hei) # Person.attack (alex, hei)
print (hei.hp)
hei.bite (alex)
print(alex.hp)
#
# ## Find the area of ​​a circle based on its radius##
# from math import pi
# class Circle:
#     def __init__(self,r):
#         self.r=r
#     def area(self):
#        return self.r**2*pi
#     def  perimeter(self):
#        return self.r*2*pi
# circle=Circle(5)
# print(circle.area())
# print(circle. perimeter())
# ###Xiao Ming, Pharaoh###
# '''
# Xiao Ming, male, 10 years old, went up the mountain to chop wood
# # Xiao Ming, male, 10 years old, driving to the Northeast
# # Xiao Ming, male, 10 years old, loves big health care
# # Lao Zhang, male, 90 years old, went up the mountain to chop wood
# # Lao Zhang, male, 90 years old, driving to the Northeast
# # Lao Zhang, male, 90 years old, loves big health care
# # Lao Wang, male, 70 years old, went up the mountain to chop wood
# '''
# class Person:
#     def __init__(self,name,sex,aggr):
#         self.name=name
#         self.sex = sex
#         self.aggr = aggr
#     def attack(self):
# print("%s%s%s went up the mountain to chop wood"%(self.name,self.sex,self.aggr ))
# print("%s%s%s drive to the northeast" % (self.name, self.sex, self.aggr))
# print("%s%s%s loves big health care" % (self.name, self.sex, self.aggr))
# obj1=Person("小明","男",10)
# obj1.attack()
# obj2=Person("Pharaoh","Male",90)
# obj2.attack()
# obj3=Person("Old Zhang","Male",75)
# ###Method Two###
# class Person:
#     def __init__(self,name,sex,age):
#         self.name = name
#         self.age = age
#         self.sex = sex
#     def dirve(self):
# print('%s,%s,%s old, drive to the northeast'%(self.name,self.sex,self.age))
#     def climb(self):
# print('%s,%s,%s old, go up the mountain to chop wood'%(self.name,self.sex,self.age))
#     def hobby(self):
# print('%s,%s,%s old, loves big health care'%(self.name,self.sex,self.age))
# ming = Person ('Komei','Man', 10)
# ming.dirve()
# ming.climb()
# ming.hobby ()
# zhang = Person('Old Zhang','Male',90)
# zhang.dirve()
# zhang.climb()
# zhang.hobby ()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325878665&siteId=291194637