Object-oriented namespaces
Methods in a class
Static properties in a class
Properties of the Object
Combined application of Class
Object-oriented namespaces:
Class A:
country= ' China '
A=a ()
A.name= ' Alex '
The print (a.name) result is that Alex wants to add properties that you can add
B=a ()
Print (B.name) result is a property without name
The object has an address book to invoke the method of the class, showing different memory, but the actual call memory is the same
Objects and classes invoke dynamic properties with different addresses, actually
Static properties are exactly the same address
a.country= ' China '
Print (A.country) China
Print (A.country) India
Print (B.country) India
For static properties about the class:
If the class. property is called by a property in the class
Object. Property first from its own memory space to find the name, found the use of their own, did not find the use of classes, the class did not error
About the dynamic properties of a class:
This method has the ability to exist in the class, and there is no memory in the object
When an object invokes a method in a class, it relies on an address book to find the corresponding method in the class.
About the properties of an object
object's properties exist in the object's namespace
Can only be called and modified by an object
cannot be called by class
Object-oriented combination:
Can be decoupled, you can use other classes of objects to do properties
Class Ban:
def __init__ (Self,name,ke,jiang):
Self.name=name
Self.ke=ke.name
Self.jiang=jiang.name
Class Student:
Quanxian= ' Xuesheng '
def __init__ (self,name,ban,ke,grade=100):
Self.name=name
Self.ban=ban.name
Self.ke=ke.name
Self.jiang = Ban.jiang
Self.grade=grade
Class Jiang:
Quanxian= ' Jiaoshi '
def __init__ (Self,name,sex,ke,bang):
Self.name = Name
Self.sex = Sex
self.ke = Ke.name
Self.bang=bang
Python learning Object-oriented composition