Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement inheritance such as python object-oriented

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to achieve inheritance such as python object-oriented". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to implement inheritance such as python object-oriented.

I. Overview

One of the main functions of the object-oriented programming (OOP) language is inheritance. Inheritance is the ability to use all the functionality of an existing class and extend it without rewriting the original class.

The new class created by inheritance is called "subclass" or "derived class", and the inherited class is called "base class", "parent class" or "superclass". The process of inheritance is from general to special. In some OOP languages, a subclass can inherit multiple base classes. But in general, a subclass can only have one base class, and multiple inheritance can be achieved through multi-level inheritance.

There are two main ways to implement the concept of inheritance: implementation inheritance and interface inheritance.

Implementation inheritance refers to the ability to use the properties and methods of the base class without additional coding. Interface inheritance refers to using only the names of properties and methods, but the subclass must provide the ability to implement (the subclass reconstructs the parent method).

When considering inheritance, it is important to note that the relationship between the two classes should be a "belong" relationship. For example, Employee is a person, and Manager is a person, so both classes can inherit the Person class. But the Leg class cannot inherit the Person class because the leg is not a person.

The OO development paradigm is roughly as follows: dividing the object → abstract class → to organize the class into a hierarchical structure (inheritance and synthesis) → uses classes and instances to design and implement.

II. Class inheritance 2.1Definitions of inheritance class Person (object): # define a parent class def talk (self): # method print ("person is talking....") in the parent class Class Chinese (Person): # defines a subclass that inherits the Person class def walk (self): # defines its own method print ("is walking...") in the subclass C = Chinese () c.talk () # call the inherited Person class method c.walk () # call its own method # output the inheritance of the person is talking....is walking...2.2 constructor

If we want to pass parameters to instance c, we need to use the constructor, so how does the constructor inherit and how does the subclass define its own properties?

The constructor of the inherited class:

1. How to write a classical class: parent class name. _ _ init__ (self, parameter 1, parameter 2.)

two。 New class writing: super (subclass, self). _ _ 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): # inherit first and inherit the constructor of the parent class in refactoring Person.__init__ (self, name,age) #. It can also be written as: super (Chinese,self). _ init__ (name,age) self.language = language # define the class's own attribute def walk (self): print ("is walking...") Class American (Person): pass c = Chinese ("bigberg", 22, "Chinese")

If we simply define a constructor in the subclass Chinese, we are actually refactoring. In this way, the subclass cannot inherit the properties of the parent class. So when we define the constructor of the subclass, we have to inherit it and then construct it, so that we can also get the properties of the parent class.

The basic process of the subclass constructor is as follows:

Instantiate object c-> c invokes subclass _ _ init__ ()-> subclass _ _ init__ () inherits parent class _ _ init__ ()-> calls parent class _ _ init__ ()

2.3 overrides of parent methods by subclasses

If we need to modify the method of the base class / parent class, we can ReFactor the method in the subclass. The 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): # subclass refactoring method print ("% s is speaking chinese"% self.name) def walk (self): print ("is walking...") C = Chinese ("bigberg", 22, "Chinese") c.talk () # output bigberg 22 weight Chinesebigberg is speaking chinese III. Case of class inheritance class SchoolMember (object): "" Learning member base class "member = 0 def _ init__ (self, name, age) Sex): self.name = name self.age = age self.sex = sex self.enroll () def enroll (self): "register" print ("just enrolled a new school member [% s]."% self.name) SchoolMember.member + = 1 def tell (self): print ("- -% Smurmurf -"% self.name) for k V in self.__dict__.items (): print (k, v) print ("- end-") def _ del__ (self): print ("fired [% s]"% self.name) SchoolMember.member-= 1 class Teacher (SchoolMember): "teacher" 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): student 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 38sex Mname haitaoamount 0course pythontuition 30000----end-just enrolled a new school member [lichuang] .3 fired [lichuang] 2 fired [Wusir] fired [haitao] here I believe you have a deeper understanding of "how to achieve inheritance such as python object-oriented", so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report