Object:
All data in Python exists as an object, meaning ' Everything is object ', and the object contains both the data and the code.
Class:
There are many classes in Python that are used to create other standard data types, such as lists, dictionaries, and so on. If you want to create an object in Python that belongs to you. A class must be defined with a keyword class. For example
Class person (): def __init__ (self,name):
Self.name = Name
Print ('%s '% self.name)
N1 = person (' Sunlieqi ')
The above code does the following work:
- View the definition of the person class
- A new object was created in memory;
- Call the cashed __init__ method, pass the newly created object as self and pass another parameter (' Sunlieqi ') as name to the
- Return to this new object
- The object that was just passed in is written in memory and can be read and written directly.
Class has three properties
(1) Package
Class Foo: def __init__ (self, Name, age): self.name = name Self.age = Age obj1 = Foo (' Sunlieqi ') print obj1 . name Print obj1.age obj2 = Foo (' SLQ ', () print Obj2.name
Self is a formal parameter, when executing obj1 = Foo (' Sunlieqi ', 18), self equals obj1
When executing obj2 = Foo (' SLQ ', 78), self equals obj2
In the case of object-oriented encapsulation, the content is actually encapsulated into an object using a construction method, and the encapsulated content is obtained indirectly through the object directly or self.
(2) inheritance
Derive new classes from existing classes, add or modify some functions
Class Cat: Def Eat (): pass def drink (): Pass Def Pull (): Pass def scatter (): pass def () : Print (' Miao ') class dog: def Eat (): pass def drink (): pass def pull (): pass def scatter (): Pass def called (): print (' Wang ')
It is the same way that a cat and a dog can be executed except for a different name.
We can use a large class to include these
Class Animal:
Def eat (): pass def drink (): pass def pull (): pass def scatter (): Pass
Class Cat (Animal):
Def called ():
Print (' Miao ')
Class Dog (animal):
Def called ():
Print (' Wang ')
Therefore, for object-oriented inheritance, it is actually the method of extracting multiple classes common to the parent class, and the subclass inherits only the parent class without having to implement each method in one.
(3) polymorphic
Python does not need to be polymorphic. Slightly
Python objects and classes