Python instance object-oriented inheritance Detailed

This paper describes examples of object-oriented inheritance class Python. Share to you for your reference, as follows:

I. Overview
One of the main features of object-oriented programming (OOP) language is "inherited." Inheritance refers to such a capability: it can use all the features of an existing class, and without having to rewrite these functions extend the case of the original class.

Create a new class by inheriting process called "sub-class" or "derived class", inherited class called "base class", "parent" or "superclass", inheritance, that is, from the general to the particular process . In certain OOP languages, subclasses inherit a plurality of base classes. But under normal circumstances, a subclass can have only one base class to implement multiple inheritance can be achieved through multiple inheritance.

Implementation of the concept of inheritance are mainly two types: inheritance, interface inheritance.

Inheritance refers achieved without the use of extra coding capacity properties and methods of the base class.

Interface inheritance refers to the properties and methods using only the name, but the subclass must provide the ability to achieve (a subclass of class father reconstruction method).

When considering the use of inheritance, one thing to note that the relationship between the two classes should be "part of" relationship. For example, Employee is a person, Manager is a human being, so these two classes can inherit the Person class. However Leg class can not inherit the Person class, because the leg is not a person.

OO development paradigm roughly: abstract class division → → Object classes organized as a hierarchical structure (inherited and synthesized) Example → class with design and implementation stages.

Second, the class inheritance
2.1 Inheritance definition

class Person(object):  # 定义一个父类
  def talk(self):  # 父类中的方法
    print("person is talking....")
class Chinese(Person):  # 定义一个子类, 继承Person类
  def walk(self):   # 在子类中定义其自身的方法
    print('is walking...')
c = Chinese()
c.talk()   # 调用继承的Person类的方法
c.walk()   # 调用本身的方法

Output:

person is talking....
is walking...

2.2 inherit the constructor
if we give examples c mass participation, we will have to use the constructor, the constructor of the how to inherit, while subclasses and how to define your own property?

Constructor class inheritance:

1. Classic wording: parent name. The init (Self, parameter 1, parameter 2, ...)

  1. Writing new class:. Super (subclass, Self) the init (parameter 1, parameter 2, ...)
class Person(object):
  def __init__(self, name, age):
    self.name = name
    self.age = age
    self.weight = 'weight'
  def talk(self):
    print("person is talking....")
class Chinese(Person):
  def __init__(self, name, age, language): # 先继承,在重构
    Person.__init__(self, name, age) #继承父类的构造方法,也可以写成:super(Chinese,self).__init__(name,age)
    self.language = language  # 定义类的本身属性
  def walk(self):
    print('is walking...')
class American(Person):
  pass
c = Chinese('bigberg', 22, 'Chinese')

If we simply define a constructor function in a subclass in Chinese, in fact, in the reconstruction. Such a subclass can not inherit the property of the parent class. So when we define the constructor subclass inherits first and then construction, so we can get the properties of the parent class.

Subclass constructor based parent class constructor as follows:

Examples of object c ----> c call subclass __init __ () ----> subclass __init __ () inherit parent __init __ () -----> call the parent class the init ()

2.3 subclass overrides the parent class method
, if we need to modify the base class / parent class, which can be reconstructed in the subclass. Following Talk () method

class Person(object):
  def __init__(self, name, age):
    self.name = name
    self.age = age
    self.weight = 'weight'
  def talk(self):
    print("person is talking....")
class Chinese(Person):
  def __init__(self, name, age, language):
    Person.__init__(self, name, age)
    self.language = language
    print(self.name, self.age, self.weight, self.language)
  def talk(self): # 子类 重构方法
    print('%s is speaking chinese' % self.name)
  def walk(self):
    print('is walking...')
c = Chinese('bigberg', 22, 'Chinese')
c.talk()

Output:

bigberg 22 weight Chinese
bigberg is speaking chinese

Third, the class inheritance examples

class SchoolMember(object):
  '''学习成员基类'''
  member = 0
  def __init__(self, name, age, sex):
    self.name = name
    self.age = age
    self.sex = sex
    self.enroll()
  def enroll(self):
    '注册'
    print('just enrolled a new school member [%s].' % self.name)
    SchoolMember.member += 1
  def tell(self):
    print('----%s----' % self.name)
    for k, v in self.__dict__.items():
      print(k, v)
    print('----end-----')
  def __del__(self):
    print('开除了[%s]' % self.name)
    SchoolMember.member -= 1
class Teacher(SchoolMember):
  '教师'
  def __init__(self, name, age, sex, salary, course):
    SchoolMember.__init__(self, name, age, sex)
    self.salary = salary
    self.course = course
  def teaching(self):
    print('Teacher [%s] is teaching [%s]' % (self.name, self.course))
class Student(SchoolMember):
  '学生'
  def __init__(self, name, age, sex, course, tuition):
    SchoolMember.__init__(self, name, age, sex)
    self.course = course
    self.tuition = tuition
    self.amount = 0
  def pay_tuition(self, amount):
    print('student [%s] has just paied [%s]' % (self.name, amount))
    self.amount += amount
t1 = Teacher('Wusir', 28, 'M', 3000, 'python')
t1.tell()
s1 = Student('haitao', 38, 'M', 'python', 30000)
s1.tell()
s2 = Student('lichuang', 12, 'M', 'python', 11000)
print(SchoolMember.member)
del s2
print(SchoolMember.member)

Output:

----end-----
just enrolled a new school member [haitao].
----haitao----
age 38
sex M
name haitao
amount 0
course python
tuition 30000
----end-----
just enrolled a new school member [lichuang].
3
开除了[lichuang]
2
开除了[Wusir]
开除了[haitao]

Content on more than how many, and finally to recommend a good reputation in the number of public institutions [programmers], there are a lot of old-timers learning skills, learning experience, interview skills, workplace experience and other share, the more we carefully prepared the zero-based introductory information on actual project data every day to explain the timing of Python programmers technology, to share some of the ways to learn and need to pay attention to small details, to remember the attention I
Here Insert Picture Description

Published 30 original articles · won praise 0 · views 10000 +

Guess you like

Origin blog.csdn.net/chengxun02/article/details/105016995