Python learning-object-oriented (I) and python learning-object-oriented

Source: Internet
Author: User

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 __);

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.