Python base day-21[Object-oriented: polymorphic, binding Method]

Source: Internet
Author: User

Polymorphic:

Refers to the various forms of a thing.

For example:

Data types are available in several forms: string, list, tuple

Various forms of animals: dogs, cats, tigers, lions ...

An abstract class has multiple subclasses, so the concept of polymorphism relies on inheritance.

ImportABCclassA (metaclass=ABC. Abcmeta): @abc. AbstractmethoddefTalk (self): The #子类必须有 Talk methodPassclassB (A):defTalk (self):Print('say Hi')classC (A):defTalk (self):Print('say CCCC') e=B () #实例化对象d=C ()defTalk (obj): #定义一个函数, incoming object #obj就是多态性的具体表现形式 obj.talk () #调用对象的方法talk (e) talk (d) Results of execution: D:\Python\Python36-32\python.exe e:/python/day-21/day21_ practice. Pysay Hisay Ccccprocess finished with exit code 0

The above code: it can be understood that, for example, a refers to the animal class, the following B and C represent dogs and cats, dogs and cats will be called but, the sound is different, this is polymorphic.

polymorphism refers to functions with different functions that can use the same function name, so that functions can be called with a function name in different functions.

Method of binding:

There are two methods of binding:

Methods to bind to an object:

Definition: A function defined in a class (not decorated by any adorner) is bound to an object.

Consumer: Object

Features: Obj.bar () automatically passes obj as the first parameter.

Methods to bind to a class:

Definition: A function that is classmethod decorated in a class is a method that is bound to a class.

User: Class

Features: Class. Class_method () automatically passes a class as the first parameter.

Example:
Classpeople:def __init__(self,name): Self.name=namedefBar (self):Print('--->', Self.name) @classmethoddeffunc (CLS): F= CLS ('ldsly', 18,'nan') #实例化Print(CLS) #查看cls是什么Print(f.name,f.age,f.sex) #打印对象属性classUser (people):def __init__(self,name,age,sex): Self.name=name Self.age=Age Self.sex=Sexuser.func () #User类调用People类的func方法 <---Methods that are bound to classes P1= People ('LDSLY_P1') #实例化p1. Bar () #对象调用方法 <----methods that are bound to an object execution result: D:\Python\Python36-32\python.exe e:/python/day-21/day21_ practice. PY<class '__main__. User'>ldsly18nan--->Ldsly_p1process finished with exit code 0

There is also a non-binding method:

A function that does not depend on the parameters of a class to run, classes, and objects can be called. Does not bind to any object or class. A non-binding method is essentially a function, which is a tool.

ImportPickleclassStudent:def __init__(self,name,age,sex): Self.name=name Self.age=Age Self.sex=sex @staticmethod #使用 Staticmethod to set a non-binding methoddefload_info (): #这个非绑定函数仅仅是反序列化文件 and print the appropriate information with open ('STUDENT.PKL','RB') as F:res=f.read () ldsly=pickle.loads (RES)Print(Ldsly.name, Ldsly.age, ldsly.sex) ldsly= Student ('ldsly', 23,'male') with open ('STUDENT.PKL','WB'As F: #序列化保存 object ldsly Res=pickle.dumps (ldsly) f.write (res)Print(student.load_info) #类去调用Print(Ldsly.load_info) #对象去调用Student. Load_info () #执行, no value passed in Ldsly.load_info () execution result: D:\Python\Python36-32\python.exe e:/python/day-19/Serialization. Py<function Student.load_info at 0x03476198> #发现对象和类看到的是同一个 and is a common function <function student.load_info at 0x03476198>ldsly23male #执行结果, same ldsly23Maleprocess finished with exit code 0

Python base day-21[Object-oriented: polymorphic, binding Method]

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.