Python classes and Inheritance action code examples

Source: Internet
Author: User
Directly paste their own written code to do the reference:

#encoding =utf-8__author__ = The definition of the ' Administrator ' # class # **KW dynamically adds the parameter class person to the object when creating an instance object of the Class (object): Def __init__ (self,            NAME,AGE,**KW): # #每个类都有个init Method self.name = Name Self.age = age for K,v in Kw.iteritems ():    SetAttr (Self,k,v) # # # Use SetAttr to assign dynamic properties to class object classes Teacher (person): #继承Person类 __score=0 # # defines private properties of a class that cannot be accessed by the outside world def __init__ (self,name,age,score): Self.__score = Score Super (teacher,self). __init__ (name,age) # #继承父类的 Initialize Method print "Teacher.__score =", Self.__score #---Methods also classify methods and instance methods #定义类方法 @classmethod declarations can be used directly with teacher.getscorefor Class () to invoke @classmethod def Getscoreforclass (CLS): Return Cls.__score #定义实例方法 directly with Teacher.getscoreforclass ( ) to call will error # P.getscoreforexample () def getscoreforexample (self): return self.__scorefrom types Import Methodtyp  EIF __name__ = = "__main__": #te = Person ("TOM", 35,addr= "USA", sex= "M") # # Class Instance Object TE "can directly change the value of an object instance, such as: Te.name = "BOB" can also be directly to the object instance to add the genusSex, such as: Te.lover = "SUM" does not add properties to the class, when you are in this common instance of a class is not the property just added "print" befor: ", Teacher.getscoreforclass () p = Teacher ("TOM", 33,100) print "After:", P.getscoreforexample () print "After:", Teacher.getscoreforclass ()
  • 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.