python advancement (4): first understanding of object-oriented

Keywords: Python Attribute

Everything is an object!

 

I. Process Oriented - > Object Oriented

Process-oriented: stacking code from top to bottom according to business logic

Functional Formula: Encapsulate a function code into a function, so that it does not need to be repeated in the future, just call the function.

Object-Oriented: Classifying and encapsulating functions to make development "faster, better and stronger..."

 

II. Initial Classes and Objects

Everything in python is an object, and the essence of a type is a class, so you've been using classes for a long time.

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

1. Class

#Define a human being class Class keywords and defining functions def Empathy
class Person:   
    role = 'person'  
    #role Variable Attribute - Static Attribute
    def walk(self):  
    #self Method must be written, also known as dynamic attributes
        print("person is walking..."
class

Attribute Reference (Class Name. Attribute)

class Person:
    role = 'person' 
    def walk(self):
        print("person is walking...")


print(Person.role)  #Viewer's role attribute
print(Person.walk)  #Refer to the person's walking method. Note that this is not a call.
Quote

Instance: The bracketed class name is instantiation, which automatically triggers the operation of the _init_ function, which can be used to customize its own characteristics for each instance.

Syntax: Object name = class name (parameter)

ClassPerson:  Define a human
    role = 'person'
    def __init__(self,name):
        self.name = name # Each role has its own nickname;
 
Cangjing = Person('Cangjing')
Ozawa = Person('Ozawa')
# The process of instantiation is the process of class - > object
instantiation

The class name () is equivalent to executing Person. init (), and after executing init (), an object is returned. This object resembles a dictionary with some attributes and methods that belong to the person itself.

View Properties & Call Method

Print (Cangjing. name) # View object attributes direct object names. Attribute names

self: When instantiating, the second parameter that automatically passes the object/instance itself to _init_ must be written with an alias but not an alias.

Class Supplement

First: Where in the world are the attributes of the classes we define stored? There are two ways to view
 Dir (class name): A list of names is found
 Class name. _dict_: A dictionary is found, with key as attribute name and value as attribute value.

II: Special class attributes
 Class name. _name___________ class name (string)
Document string of class name. _doc______________ class
 Class name. _base_________________
Class name. _bases__________________
Class name. _dict_______________
Class name. _module________________
Class name. _class_________________
Supplementary Class Attributes

2. Objects

Let's write about a dog fight: Now we need to make a little change to our class. Humans should have some attacking skills besides being able to walk.

class Person:  # Define a human being
    role = 'person'  # All the attributes of human roles are human
    def __init__(self, name, aggressivity, life_value):
        self.name = name  # Each character has its own nickname.;
        self.aggressivity = aggressivity  # Every character has his own attack power.;
        self.life_value = life_value  # Every character has its own life value.;
    def attack(self,dog):
        # People can attack dogs, and dogs here are also a target.
        dog.life_value -= self.aggressivity
        print("{0}Hit{1}Once,{1}Remaining blood volume{2}".format(self.name, dog.name, dog.life_value))
Human beings

Object is an example of a class that actually exists, that is, an instance. Objects / instances have only one function: attribute references

Cangjing = Person('Cangjing', 10,1000)
Print (Cangjing. name)
Print (Cangjing. aggressivity)
Print (Cangjing. life_value)

You can also refer to a method because the method is also an attribute (dynamic attribute).

Class class name:
    Class attribute = None
    Def__init_ (self, parameter 1, parameter 2):
        self. Object property 1 = parameter 1
        self. Object property 2 = parameter 2

    def method name (self):pass

    def method name 2(self):pass

Object name = class name (1,2)  Object is an instance, representing a specific thing.
                  # Class name (): Class name + parentheses instantiate a class, which is equivalent to calling the _init_ method
                  # Pass parameters in parentheses, parameters do not need to pass self, and other parameters in init correspond one by one.
                  # The result returns an object
 Object name. Attribute 1 of the object # View the attributes of the object, directly using the object name. Attribute name can be used.
Object name. Method name ()# Calls the method in the class, directly using the object name. Method name ()
# Object adds attributes
 Object. New property name = 1000


    
Summary

Interaction between objects

Now that we're going to be a dog fight, we've got people. Now we're going to create another dog, the dog bites, and we're going to give the dog a bite method. With dogs, we instantiate a real dog. People and dogs can fight.

class Dog:  # Define a dog class
    role = 'dog'  # Dog's character attributes are all dogs.
    def __init__(self, name, breed, aggressivity, life_value):
        self.name = name  # Every dog has its own nickname.;
        self.breed = breed  # Every dog has its own breed.;
        self.aggressivity = aggressivity  # Every dog has its own attack power.;
        self.life_value = life_value  # Every dog has its own life value.;
    def bite(self,people):
        # Dogs can bite people. Dogs here are also a target.
        people.life_value -= self.aggressivity
        print("{0}Bite{1}Once,{1}Remaining blood volume{2}".format(self.name,people.name,people.life_value))
Dogs

Instantiate a wolf dog:

egon = Dog('egon','German Shepherd Dog',100,20000)  #Creating a real dog egon

Interactive well drilled egon once / egon bit the well once

a = Person("Sora Well",10,1000)
b = Dog("egon","German Shepherd Dog",100,20000)
a.attack(b)
b.bite(a)
interactive

Complete human-dog war code:

class Person:  # Define a human being
    role = 'person'  # All the attributes of human roles are human
    def __init__(self, name, aggressivity, life_value):
        self.name = name  # Each character has its own nickname.;
        self.aggressivity = aggressivity  # Every character has his own attack power.;
        self.life_value = life_value  # Every character has its own life value.;
    def attack(self,dog):
        # People can attack dogs, and dogs here are also a target.
        dog.life_value -= self.aggressivity
        print("{0}Hit{1}Once,{1}Remaining blood volume{2}".format(self.name, dog.name, dog.life_value))

class Dog:  # Define a dog class
    role = 'dog'  # Dog's character attributes are all dogs.
    def __init__(self, name, breed, aggressivity, life_value):
        self.name = name  # Every dog has its own nickname.;
        self.breed = breed  # Every dog has its own breed.;
        self.aggressivity = aggressivity  # Every dog has its own attack power.;
        self.life_value = life_value  # Every dog has its own life value.;
    def bite(self,people):
        # Dogs can bite people. Dogs here are also a target.
        people.life_value -= self.aggressivity
        print("{0}Bite{1}Once,{1}Remaining blood volume{2}".format(self.name,people.name,people.life_value))

a = Person("Sora Well",10,1000)
b = Dog("egon","German Shepherd Dog",200,20000)
while True :
    a.attack(b)
    b.bite(a)
    if a.life_value <= 0 :
        print(a.name+"cover"+b.name+"Bite dead!")
        break
    if b.life_value <= 0 :
        print(b.name + "cover" + a.name + "Bite dead!")
        break
egon battle Sakai well

 

Class Namespaces and Object and Instance Namespaces

Creating a class creates a class namespace that stores all the names defined in the class, called attributes of the class.

Static attributes are variables defined directly in classes, and dynamic attributes are methods defined in classes.

The data attributes of classes are shared to all objects.

print(id(a.role))
print(id(Person.role))  #identical

The dynamic properties of classes are bound to all objects.

print(a.attack)
print(Person.attack)  #difference

Creating an object/instance creates an object/instance namespace, which stores the name of the object/instance, which is called the attribute of the object/instance. In obj.name, the name is first found in the obj's own namespace. If it cannot be found, it will be found in the class. If it cannot be found, it will be found in the parent class. Finally, if it cannot be found, it will throw an exception.

 

IV. Object-Oriented Composite Usage

Composition refers to the combination of classes in which objects of another class are used as data attributes.

class Weapon:
    '''
        //This is a data type of weapon in the game.
    '''
    def __init__(self,name, price, aggrev, life_value):
        self.name = name    #Weapon Name
        self.price = price  #Weapon prices
        self.aggrev = aggrev    #Weapon damage bonus
        self.life_value = life_value    #Weapon Blood Addition

    def update(self, obj):  #obj It's the person with the equipment.
        obj.money -= self.price  # People who use this weapon spend money to buy it, so the corresponding money should be reduced.
        obj.aggressivity += self.aggrev  # Bring this equipment to increase attack
        obj.life_value += self.life_value  # Bring this equipment can add to your life.

    def prick(self, obj):  # This is the active skill of the equipment, the dragon.
        obj.life_value -= 3000  # Assuming attack power is 3000
Weapons

Combination Interactive Dog Warfare Complete Code:

class Person:  # Define a human being
    '''
        //This is a data type for the characters in the game.
    '''
    role = 'person'  # All the attributes of human roles are human
    def __init__(self, name, aggressivity, life_value):
        self.name = name  # Each character has its own nickname.;
        self.aggressivity = aggressivity  # Every character has his own attack power.;
        self.life_value = life_value  # Every character has its own life value.;
    def attack(self,dog):
        # People can attack dogs, and dogs here are also a target.
        dog.life_value -= self.aggressivity
        print("{0}Hit{1}Once,{1}Remaining blood volume{2}".format(self.name, dog.name, dog.life_value))

class Dog:  # Define a dog class
    '''
        //This is the data type of the dog in the game.
    '''
    role = 'dog'  # Dog's character attributes are all dogs.
    def __init__(self, name, breed, aggressivity, life_value):
        self.name = name  # Every dog has its own nickname.;
        self.breed = breed  # Every dog has its own breed.;
        self.aggressivity = aggressivity  # Every dog has its own attack power.;
        self.life_value = life_value  # Every dog has its own life value.;
    def bite(self,people):
        # Dogs can bite people. Dogs here are also a target.
        people.life_value -= self.aggressivity
        print("{0}Bite{1}Once,{1}Remaining blood volume{2}".format(self.name,people.name,people.life_value))

class Weapon:
    '''
        //This is a data type of weapon in the game.
    '''
    def __init__(self,name, price, aggrev, life_value):
        self.name = name    #Weapon Name
        self.price = price  #Weapon prices
        self.aggrev = aggrev    #Weapon damage bonus
        self.life_value = life_value    #Weapon Blood Addition

    def update(self, obj):  #obj It's the person with the equipment.
        obj.money -= self.price  # People who use this weapon spend money to buy it, so the corresponding money should be reduced.
        obj.aggressivity += self.aggrev  # Bring this equipment to increase attack
        obj.life_value += self.life_value  # Bring this equipment can add to your life.

    def prick(self, obj):  # This is the active skill of the equipment, the dragon.
        obj.life_value -= 3000  # Assuming attack power is 3000
        print("{0}Launching Initiative: Jiaolong==>{1}Remaining blood volume{2}".format(self.name, obj.name, obj.life_value))


a = Person("Sora Well",10,1000)
b = Dog("egon","German Shepherd Dog",200,20000)
c = Weapon("Jiaolong Whip",1000,40,2000)
a.money = 2000

#Judging whether weapons can be purchased or not
if a.money > c.price :
    c.update(a)
    a.weapon = c

#The Great War Begins
while True :
    a.attack(b)
    if b.life_value <= 0 :
        print(b.name + "cover" + a.name + "Killed!")
        break
    a.weapon.prick(b)
    if b.life_value <= 0 :
        print(b.name + "cover" + a.name + "Hanging!")
        break
    b.bite(a)
    if a.life_value <= 0 :
        print(a.name+"cover"+b.name+"Bite dead!")
        break
Upgraded version of egon battle Sakai

Posted by abigail on Wed, 05 Jun 2019 13:16:56 -0700