Python Foundation day-18[Object-oriented: inheritance, composition, interface normalization]

Source: Internet
Author: User

Inherited:

The object class is inherited by default in Python3. But the class that inherits the object class and subclass is called the new class (This is all in Python3). There is no inheritance called the Classic class (the object is not inherited in Python2 and his subclasses are classic classes.) )

Inheritance is a class-to-class relationship that solves the problem of code reuse and reduces redundant code. In Python, the parent class is defined first, and then the subclass is defined. Finally, the subclass is used to instantiate the object.

"Inheritance is a relationship of what is what." For example: The following S1 is a subclass of obj. "

  #父类的定义: 
class obj: #定义一个obj类 pass class S1 (obj): #定义s1类, enclose its parent class in parentheses, and multiple parent classes are separated by commas. Passprint (obj.__bases__) #查看其继承关系print (s1.__bases__) execution Result: D:\Python\Python36 - 32 \python.exe e:/python/day-18 /tmp.py ( <class '

When an object references a property, but the class does not have a word, it goes to the class's parent query, and does not continue to query upward.

class obj:       ' ABC '   #父类中有一个变量 ' a = ' abc ' "class  S1 (obj):    pass= S1 ()    #使用子类实例化一个对象 Print (p1.a)  #查看对象p1的属性a信息执行结果:D: \python\python36-32\python.exe e:/python/day-18/tmp.pyabc    #显示的是父类的属性信息Process finished with exit code 0

Derived:

Refers to a subclass of properties that inherit from the parent class, with its own unique properties.

classobj_1:def __init__(self, name, age, Sex): #父类中共有的属性 self.name=name Self.age=Age Self.sex=Sexclassobj_2 (obj_1): School='Oldboy' #<------: This is also a subclass of their own unique    def __init__(self, name, age, sex, salary): Obj_1.__init__(self, name, age, Sex) #传值给父类 self.salary=Salary #自己独有的属性classStu (obj_1): Job='Student'Zhang= Obj_2 ('Zhang', 88,'nan', 5000) #实例化对象wang= Stu ('Wang', 55,'nan')Print(Wang.name, Wang.age, Wang.job)Print(Zhang.name, Zhang.age, Zhang.sex, zhang.salary, zhang.school) execution result: D:\Python\Python36-32\python.exe E:/PYTHON/DAY-18/1. Pywang55Studentzhang5000 NanOldboyprocess finished with exit code 0

Combination:

"The combination is a kind of what has what relationship"

It is also used to reduce duplicate code.

classobj_1:def __init__(self, name, age, Sex): Self.name=name Self.age=Age Self.sex=Sexclassobj_2 (obj_1): School='Oldboy'    def __init__(self, name, age, Sex, salary,year,mon,day): Obj_1.__init__(self, name, age, sex) self.salary=Salary Self.birth=Date (year,mon,day) #boj_1子类有个 Date <-----Call the date class hereclassStu (obj_1): Job='Student'classDate: #定义 Date classdef __init__(self,year,mon,day): Self.year=Year Self.mon=Mon self.day= DaydefTell_birth (self):Print('%s%s%s'%(self.year,self.mon,self.day)) Zhang= Obj_2 ('Zhang', 88,'nan', 5000,1994,11,32) Wang= Stu ('Wang', 55,'nan')Print(Wang.name, Wang.age, Wang.job)Print(Zhang.name, Zhang.age, Zhang.sex, Zhang.salary, Zhang.school,) Zhang.birth.tell_birth () #查看信息执行结果:D: \python\ Python36-32\python.exe E:/PYTHON/DAY-18/1. Pywang55Studentzhang5000 NanOldboy19941132 #输出信息Process finished with exit code 0

interface to the design of a:

Restricting subclasses must have a method of the parent class that the subclass implements must be the same as the method name of the parent class.

ImportABC #导入abc模块classObj (metaclass=ABC. Abcmeta): #元类 @abc. abstractclassmethod @ Call Adorner to restrict method names of subclassesdefRead (self):Pass@abc. AbstractclassmethoddefWrite (self):PassclassS1 (obj):defRead (self): #子类的方法名必须跟父类一致, otherwise errorPrint('is read')    defWrite (self):Print('is write')classS2 (obj):defRead (self):Print('is read')    defWrite (self):Print('is write') P1=S1 () P1.read () execution result: D:\Python\Python36-32\python.exe e:/python/day-18/tmp.py isReadprocess finished with exit code 0

Python Foundation day-18[Object-oriented: inheritance, composition, interface normalization]

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.