Python object oriented class inheritance and composite instance analysis

  • 2020-10-07 18:45:42
  • OfStack

This article provides an example of Python object-oriented class inheritance and composition. To share for your reference, the details are as follows:

In python3, all classes inherit from object by default, all classes that inherit from object are new classes, and subclasses of that subclass, Python3, are new classes, and subclasses that do not integrate with object are classic classes (classes that do not integrate with object and their subclasses are classic classes in Python2)

Inheritance is a way to create new classes with the benefit of less repetitive code


class People:
  def __init__(self,name,age):
    self.name=name
    self.age=age
  def walking(self):
    print('%s is walking ' %self.name)
  def talking(self):
    print('%s is talking '%self.name)
class Teacher(People):
  pass
class Student(People):
  pass
t1=Teacher('egon',18)
print(t1.name,t1.age)
t1.walking()
t1.talking()
s1=Student('xiaobai',22)
print(s1.name,s1.age)
s1.talking()
s1.walking()

The execution result

[

egon 18
egon is walking
egon is talking
xiaobai 22
xiaobai is talking
xiaobai is walking

]

class People:
  def __init__(self,name,age,sex):
    self.name=name
    self.age=age
    self.sex=sex
  def walking(self):
    print('%s is walking ' %self.name)
  def talking(self):
    print('%s is talking '%self.name)
class Teacher(People):
  def __init__(self,name,age,sex,level,salary):
    People.__init__(name,age,sex)
    self.level=level
    self.salary=salary
  def teaching(self):
    People.talking(self)
    print('%s is teaching'%self.name)
class Student(People):
  def __init__(self,name,age,sex,group):
    People.__init__(name,age,sex)
    self.group=group
  def studying(self):
    People.talking(self)
    print('%s is studying'%self.name)

combination


class Date:
  def __init__(self,year,mon,day):
    self.year=year
    self.mon=mon
    self.day=day
  def tell_birth(self):
    print(' Born in <%s> years  <%s> month  <%s> day '%(self.year,self.mon,self.day))
class Teacher:
  def __init__(self,name,age,sex,year,month,day):
    self.name=name
    self.age=age
    self.sex=sex
    self.birth=Date(year,month,day)
  def teaching(self):
    print('%s is teaching'%self.name)
class Student:
  def __init__(self,name,age,sex,year,mon,day):
    self.name=name
    self.age=age
    self.sex=sex
    self.birth=Date(year,mon,day)
  def studying(self):
    print('%s is studying'%self.name)
xiaobai=Student('xiaobai',22,'male','1995','3','16')
xiaobai.birth.tell_birth()

The execution result

[

Born in < 1995 > years < 3 > month < 16 > day

]

Inheritance and composition


class People:
  def __init__(self,name,age,sex,year,mon,day):
    self.name=name
    self.age=age
    self.sex=sex
    self.birth=Date(year,mon,day)
  def walking(self):
    print('%s is walking ' %self.name)
  def talking(self):
    print('%s is talking '%self.name)
class Date:
  def __init__(self,year,mon,day):
    self.year=year
    self.mon=mon
    self.day=day
  def tell_birth(self):
    print(' Born in <%s> years  <%s> month  <%s> day '%(self.year,self.mon,self.day))
class Teacher(People):
  def __init__(self,name,age,sex,level,salary,year,mon,day):
    People.__init__(self,name,age,sex,year,mon,day)
    self.level=level
    self.salary=salary
  def teaching(self):
    People.talking(self)
    print('%s is teaching'%self.name)
class Student(People):
  def __init__(self,name,age,sex,year,mon,day,group):
    People.__init__(self,name,age,sex,year,mon,day)
    self.group=group
  def studying(self):
    People.talking(self)
    print('%s is studying'%self.name)

The parent class is restricted

1. Subclasses must have superclass methods

2. A subclass must implement a method with the same name as the method of the parent class


import abc
class File(metaclass=abc.ABCMeta):
  @abc.abstractclassmethod
  def read(self):
    pass
  @abc.abstractclassmethod
  def write(self):
    pass

More about Python related content interested readers to view this site project: introduction to Python object-oriented programming and advanced tutorial ", "Python data structure and algorithm tutorial", "Python function using techniques", "Python string skills summary", "Python coding skills summary" and "introduction to Python and advanced tutorial"

I hope this article has been helpful in Python programming.


Related articles: