First, composition: Methods and properties.
Class definition:
Class MyClass ():
Def say (self):
Print ("Hello")
Test=myclass ()
Test.say ()
2.
Property:
Self.attr = value
Normal properties:
Self.attr=value
Static properties:
Attr=value
Method:
def func (self):
Common methods:
def func (self):
Only object. Func to invoke
Class method:
def func (CLS):
In principle, the class name. Func should be called, in fact the class name. Func and object. Func can all be called
static method:
def func ():
Object. Func or class name. Func
3. Special methods:
constructor function:
def __init__ (self, name):
Self.name = Name
Other special functions
Len (x) # # x.__len__ ()
A+b # # a.__add__ (b)
a[b]## a.__getitem__ (b)
eg
Class MyClass ():
def __init__ (self, value):
Self.value = value
def __add__ (self, B):
Self.value = Self.value + b.value
def print_value (self):
Print (Self.value)
Class YourClass ():
def __init__ (self, value):
Self.value = value
Test=myclass (1)
Test+yourclass (2)
Test.print_value ()
Second, characteristics: encapsulation, inheritance, polymorphism.
1. Inheritance:
Class Fclass ():
Pass
Class S1class (Fclass):
Pass
Class S2class (Fclass):
Pass
Multiple inheritance:
Class Ssclass (S1class, S2class):
Pass
Multiple inheritance involves the selection order of the methods in the parent class, first of all to understand the noun classic class and the new style class.
Classic Class: Class Fclass ():
New Style classes: Class Fclass (object):
Classic classes are selected in the order of depth preference, i.e. Ssclass->s1class->fclass->s2class
New style classes are chosen in the order of breadth preference, i.e. Ssclass->s1class->s2class->fclass
As much as possible with the new style class, the classic class does not support Super,proterty and so on.
2. Polymorphism:
Python does not support polymorphism. For the already familiar Java, C + +, when using polymorphic, must pass in a specified type, that is, Java and C + + is a strongly typed language, so there is a concept of polymorphism.
For Python implementations, because you do not need to specify a type, you can implement it very simply:
eg
Class Fclass ():
Pass
Class S1class (Fclass):
Def show (self):
Print ("Show1")
Class S2class (Fclass):
Def show (self):
Print ("Show2")
Def show (f):
F.show ()
Show (S1class ())
Show (S2class ())
Python Object-oriented