Python learning-object-oriented (I) and python learning-object-oriented
Defines an Animal class, which includes constructor, private method, public method, static method, and attribute query.
Double underscores (_) constitute the definition constraints of Private Members. In other cases, they are public members.
# _ Metaclass _ = type # confirm to use the new class
Class Animal:
Address = "acccd ";
Def _ init _ (self): # constructor call this method immediately after an object is created.
Self. Name = "Doraemon"
Print (self. Name );
Def accessibleMethod (self): # The binding method is made public.
Print (self. Name );
Self. _ inaccessible ()
Def _ inaccessible (self): # The Private method is not public and starts with a double underline.
Print ("U cannot see me ...");
@ Staticmethod
Def staticMethod ():
# Self. accessibleMethod () # You cannot directly call an instance method in a static method and throw an exception.
Print ("this is a static method ");
Def setName (self, name): # accessors Function
Self. Name = name
Def getName (self): # accessors Function
Return self. Name
# Name = property (getName, setName) # readable and writable attribute
Animal. staticMethod ();
T = Animal ();
T. setName ("ddd ");
Print (t. getName ());
Print (getattr (t, "address "));
Setattr (t, "address", "fjfkejkj ");
Print (getattr (t, "address "));
Some attribute members of the class, including the class name string, inheritance, etc.
Print ("Animal. _ name __:", Animal. _ name __);
Print ("Animal. _ module __:", Animal. _ module __);
Print ("Animal. _ bases __:", Animal. _ bases __);
Print ("Animal. _ dict __:", Animal. _ dict __);