Sixth day of Python cultivation

Source: Internet
Author: User

It's getting harder .... Now I don't want to say anything, and roll up my sleeves. 1 Object-oriented

Let's take a first example:

Like the man-dog war.
Need to have a dog, man
So create two categories of molds
def person (Name,sex,hp,dps):
DIC = {"name": Name, "Sex": Sex, "HP": HP, "DPS":d PS, "bag", []}
return dic

def Dog (Name,kind,hp,dps):
DIC = {"name": Name, "kind": Sex, "HP": HP, "DPS":d PS}
return dic

Alex = Person ("Alex", "Geil")
HA2 = Dog ("", "",)

Mold-making---object-oriented
Specification of a Category

Concept:
A class of things that have the same properties and the same actions, forming a class
An entity that has an actual property and a specific action on a specific object.
The class is abstract and the object is specific

Syntax for the class:
Class:
1 Definition:
Class Name:
static property = None
Def dynamic Property (self):
Pass
The first feature of Class 2;
# as long as the name is written in the class name, whether it is a variable or a function name, can not be called directly outside the class
# can only be used with the class name: Class name. static Properties
Static properties can be modified
The class name. static Property = 345
You can add static properties
The class name. static Property 2 = "abc"
You can delete static properties
Del class name. static Property 2

Dynamic Properties:
To define a dynamic property, add self
def func (self):
Print ("--->", self)
The class name can view a method, but in general we do not use the class name directly to invoke the method
Instead, the object is used to invoke the method.

Second function of Class 3: Instantiation (Creation object)
Class Person:pass
Alex = person ()
# object = Class name () class name plus parentheses are instantiated,
In the process of instantiation, there are a lot of things that are not visible outside the steps:
1 An object was created
2 Automatic Call __init__ method
3 The created object is passed as an actual parameter to the __init__ method and passed to the first argument self
4 then execute the contents of the Init method
5 Automatic bar Self as the return value, returned to the instantiated place

__DICT__ # Records the necessary default values, and also records all names defined by the programmer in the class
Print (alex.__dict__)
alex.__dict__["name"] = "Alex"
alex.__dict__["HP"] = "Alex"
alex.__dict__["DPS"] = "Alex"
can be abbreviated as:
Alex.name = "Alex"
ALEX.HP = 250
Alex.dps = 5

Initialization method:
Above is the property of creating an object outside of the class, can you generate properties directly in the class?
The created object is passed as an actual parameter to the __init__ method, and the first argument to the self
Can you do this: Alex = person ("Alex", "Boy", 250,5)
Class Person:
def __init__ (Self,name,hp,dps):
Self.name = Name
SELF.HP = HP
Self.dps = DPS
Alex = Person ("Alex", 250,5)
Print (alex.__dict__)
Print (Alex.name)

The name of the object. The method name corresponds to the function that invokes the class, and the first argument is the object name, and the second argument is

When a class creates an object, it creates a connection between the instance and the class.
An instance object can be found to instantiate his class.
But the class could not find how many objects he instantiated.
So:
Object can access the class's space
Class cannot access object's space

Namespace issues: Namespaces for class namespaces and object instances
Alex.country = "Indians"
When accessing variables, use the variables in your own namespace, if you don't have a
And go to the class space to find
In the process of modifying a static variable with an object, you create a new variable relative to your own space.


Alex.country[0] = "Indian"
In the operation of a static variable of a class, the class name should be used to manipulate it directly. There will be no oolong problem.



Study Questions
Create a class that can calculate how many instances of this class are created


Combination
An object of one class, as a property of another class
Like a man with weapons.
The ring has a circle
2 Object-oriented three major features
1 Inheritance accounted for 2/1
Why there is inheritance: it is to solve the problem of code redundancy.
How to inherit?
Single Inheritance:
Class Parent:
Pass
Class Sun (parent): This means that the Sun class inherits the Parent class
Pass
Produce many nouns: parent class, base class, superclass---parent class
Subclass derived class-----Sun class

Multiple inheritance, there are several parent classes:
Class Parent1:pass
Class Parent2:pass
Class Sun (Parent1,parent2):p

Look for the object's memory space first, and then find the memory space of the class that created the object, if you have not yet found the parent class memory space to create the class.
Class Animal:
def __init__ (Self,name,hp,dps)
Self.name = Name
SELF.HP = HP
Self.dps = DPS
Def cat ()
Print ("%s took medicine",%self.name)

Class Person (Animal):
def __init__ (Self,name,hp,dps,sex)
Animal.__init__ (Self,name,hp,dps) # or write as Super (). __init__ (Name,hp.dps), but if it is multiple inheritance to write this.
Self.sex = Sex # Derived Property
Def attack (self):
Pass

Class Dog (Animal):
def __init__ (Self,name,hp,dps,kind)
animal.__init__ (Self,name,hp,dps)
Self.kind = Kind
def bite ():
Pass

Interview questions:
Class Foo;
def __init__ (self):
Self.func ()
def func (self):
Print ("in Foo")
Class Son (Foo):
def func (self):
Print ("in Son")
Son () # calls Self's method first. So it's in Son.


Multiple inheritance:
Diamond Succession Issues:
A 4
/ \
/ \
2 B C 3
\ /
\ /
1 D
Priority: D,b,c,a

Small Turtle Model:
A 6
/ \
/ \
3 B C 5
| |
| |
2 D E 4
\ /
\ /
F 1
Priority: F,d,b,e,c,a
Traversal algorithm: Breadth-First algorithm
Python3 all follow the breadth-first algorithm

Python3 there are 2 species
1 Classic class Python3 has been extinct, still exist in the Python2, in Py2 as long as programmers do not actively inherit object, this class is the classic class
The depth-first algorithm is followed. A road goes to the black.

2 new class Python3 All classes are new, and all modern classes inherit from object---Follow the breadth-first algorithm in multiple inheritance.

Super Note:
In single inheritance super is looking for the parent class
The trajectory of the multi-level inheritance super is a breadth-first sequence based on the starting point of the entire model, followed by the MRO rules


Abstract class and interface classes are mainly for Java, and slightly



2 polymorphic
In Python, there are many states everywhere. I haven't spoken yet.



3 Package
I haven't spoken yet.

Sixth day of Python cultivation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.