The way of the Python-day 8-polymorphic, class member, singleton mode, exception handling

Source: Internet
Author: User

1. Polymorphism (multiple types)

The manifestation of polymorphism is as follows:

    def func (ARG):         Print (ARG)    func (1)    func ("Alex")    func ([ 11,12,13]    #func can be anything    # but not for C#,java, arguments must specify type 

The disadvantage of polymorphism: In the absence of annotations, it is not known what type of ARG is, what is the use of it. For example, if it is OK to be a list, then you can append (). But I don't know how to use it when I'm not sure.

2. Class members in Object-oriented

1) field

-static field: The code was created when it was loaded and belongs to the class

-Normal field: There are objects to this, belong to the object

class Province:     " China " # static fields, saved in class    def __init__ (self,name):         # stored in the object, this is a normal field, if this function is not called, then this is not in memory

2) method (belongs to Class)

-Common method: definition must have self, execute through object

-static method: @staticmethod, remove self, parameter is optional, executed by class

-Class method: @classmethod, a special form of a static method. Automatic class parameters, executed by class

classProvince:def __init__(self,name): Self.name=namedefShow (self):#Common Methods        Print(self.name) @staticmethod#static methods, 1. Add this, 2. Remove self,3. Parameters are optional    defF1 (arg1):Print(arg1) @classmethod#class method, you must have parameters    defF2 (CLS):#This parameter is passed automatically and is the class name. One of the static methods.         Print(CLS)defF3 (self):returnSelf.name[1]##方法是由对象调用的#obj = Province ("Henan")#obj.show ()##普通方法由对象去调用执行 (method belongs to Class)#PROVINCE.F1 (111) #静态方法, is only related to the class, does not need the object, is called by the class, the same as the function#province.f2 () #类方法, parameter is class name#obj = Province ("Alex")#ret = obj.f3 ()#print (ret)

The way of the Python-day 8-polymorphic, class member, singleton mode, exception handling

Related Article

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.