Python Learning Notes-classes and instances

Source: Internet
Author: User

1 #create a class, which is an abstraction with the same properties and methods2 #the first parameter of the method is the self, which is required to create the Class keyword, and when a method is defined3 classStudent (object):4 5     #This is not the same as C + +, C # syntax, the simple difference, the definition of class attributes need to be in a special method __init__ internal6     def __init__(self,name,score):7Self.name = Name#Note that this is the property within the class, for class properties8Self.score = Score#Similarly, this is also a class attribute9     Ten     defFun (self): One         Print("This is the method of defining a class within a class") A  -     defPrint_name (self): -         Print(Self.name) the  -     defPrint_score (self): -         Print(Self.score) -  + #Example: Creating an object and invoking a method of the object -Omega = Student ("Omega", 100) + Omega.print_name () A Omega.print_score () at  - #Some other examples are: - classPerson (object): -  -     def __init__(Self, name="Omega"): -Self.name =name in  -     defPrint_name (self): to         Print(Self.name) +  -Omega =Person () the Omega.print_name () *  $ Panax Notoginseng #Rectangle instance: - classRectangle (object): the  +     def __init__(Self,long = 0,breadth =0): ASelf.long =Long theSelf.breadth =Breadth +  -     defSetRect (self): $         Try: $             Print("Please enter the length and width of the rectangle ....") -Long = input ("Length:") -Breadth = input ("Width:") the             if  notLong.isdigit ()or  notBreadth.isdigit ()orLong = ="" orBreadth = ="": -                 RaiseValueError ("the value entered is wrong! ")Wuyi              theSelf.long =Int (long) -Self.breadth =int (breadth) Wu         except: -             Print("input Error! ") About          $     defGetRect (self): -         Print("Rectangle length is:%d, rectangle width is:%d"%(self.long,self.breadth)) -         returnSelf.long,self.breadth -  A     defGetarea (self): +         Print("the rectangular area is:", Self.long *self.breadth) the  -Rect = Rectangle (3,5) $ Rect.getarea () the Rect.setrect () the Rect.getrect () the Rect.getarea () the  -  in #Summary below: the """ the Note: The above example can only present the encapsulated features.  About here are just some of the things I've summed up, simple, don't want to get too complicated . the object-oriented has three basic characteristics: encapsulation, inheritance, polymorphism the encapsulation: is to say that objective things encapsulated into abstract class, and some untrusted class hidden information the inheritance: Primarily for code reuse.  + Polymorphic : Python polymorphism, shown in inheritance, for example a B C three classes have an Add method, the Print_value method is responsible for invoking their Add method.  - if they inherit with D at the same time, and D has an Add method, then Print_value (d): Print (D.add ()) can pass in an object of type a B C three.  the and, call the Add function, depending on the type of the instance itself. Bayi      the """

It is worth noting that the first parameter of the Python class method is self, which is actually the this* in C + +, which is the object itself.

Python Learning Notes-classes and instances

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.