【教程】莉沫酱教你学继承!?

Related tags

Miscellaneousinherit
Overview

【教程】莉沫酱教你学继承!

众所周知,类的继承就是说当一个类死亡的时候,它的子类会获得它拥有的资源。

根据类的继承法不同,各个子类能获得的资源也不同。

继承法的类型

在解释继承法之前,我们先定义三个类,一个父类A,和它的子类BC

它们都拥有xyz三个属性。

class A:
    x = 100
    y = 200
    z = 300
class B(A):
    x = y = z = 9
class C(A):
    x = y = z = 0

长子继承制

假如父类A采用了长子继承制,那么在继承中,所有资源都会被交给其最年长的子类,其他子类什么也得不到。

@长子继承制
class A:
    x = 100
    y = 200
    z = 300

在使用了长子继承制之后,我们杀掉父类,看看子类的资源有什么变化——

print(f'{A.x=}, {A.y=}, {A.z=}')
print(f'{B.x=}, {B.y=}, {B.z=}')
print(f'{C.x=}, {C.y=}, {C.z=}')
print('====继承====')
A.__del__()
print(f'{B.x=}, {B.y=}, {B.z=}')
print(f'{C.x=}, {C.y=}, {C.z=}')

可以看到,这个时候父类的所有属性都被第一个子类——也就是B——继承了。

A.x=100, A.y=200, A.z=300
B.x=9, B.y=9, B.z=9
C.x=0, C.y=0, C.z=0
====继承====
B.x=100, B.y=200, B.z=300
C.x=0, C.y=0, C.z=0

分割继承制

但是在软件开发的早期,我们无法import 长子继承制,因此我们得考虑一些其他的继承法,比如分割继承制

分割继承制中,父类的所有资源都会被划分给其子类。

@分割继承制
class A:
    x = 100
    y = 200
    z = 300

在继承之后,子类B获得了属性xz,而子类C获得了属性y

A.x=100, A.y=200, A.z=300
B.x=9, B.y=9, B.z=9
C.x=0, C.y=0, C.z=0
====继承====
B.x=100, B.y=9, B.z=300
C.x=0, C.y=200, C.z=0

斯堪的纳维亚选举制

斯堪的纳维亚选举制是北欧的维京程序员喜欢的继承法。

在这种制度下,程序中所有没有被回收的类都可以在该类的亲戚中提名一位继承人,最终由投票得分最高的类继承所有资源。

@斯堪的纳维亚选举制
class A:
    x = 100
    y = 200
    z = 300

在类A死亡时,所有的类都会在类B和类C中投票。尽管在这个例子里BC都是A的子类,实际上其他类也能投给A的兄弟等能DFS到的类。

通常,其他类会倾向于投给名字和自己长得比较像的类,一次典型的选举过程如下:

====继承====
_OrderedDictKeysView 投给 C
_OrderedDictItemsView 投给 C
_OrderedDictValuesView 投给 C
_Link 投给 C
Counter 投给 C
ChainMap 投给 C
UserDict 投给 C
UserList 投给 C
UserString 投给 C
partialmethod 投给 C
CacheInfo 投给 C
_HashedSeq 投给 B
singledispatchmethod 投给 C
cached_property 投给 B
AbstractContextManager 投给 C
AbstractAsyncContextManager 投给 C
ContextDecorator 投给 C

...

C: 142票
B: 133票
B.x=9, B.y=9, B.z=9
C.x=100, C.y=200, C.z=300

最终类C在选举中胜出,并继承了其父类A的所有属性。

使用方法

首先你需要1个3.6以上版本的Python,然后使用pip安装——

pip install git+https://github.com/RimoChan/inherit.git

然后你就可以在你的代码里使用继承法了,耶!

from 继承法 import 长子继承制, 分割继承制, 斯堪的纳维亚选举制

赞助

如果你觉得学习继承法对你的工作或学习有帮助(?),欢迎给作者介绍一些可爱的萝莉朋友。

就这样,大家88。

Owner
黄巍
字节跳动首席吃点心员
黄巍
This is an API to get user details for competitive coding platforms - Codeforces, Codechef, SPOJ, Interviewbit. More Platform will be Added Soon.

Competitive-Programming-Score-API An API to get user details for competitive coding platforms - Codeforces, Codechef, SPOJ, Interviewbit Platforms Ava

Aaditya Prakash 3 Jan 17, 2022
Use a real time weather API to apply wind to your mouse cursor.

wind-cursor Use a real time weather API to apply wind to your mouse cursor. Requirements PyAutoGUI pyowm Usage This program uses the OpenWeatherMap AP

Andreas Schmid 1 Feb 07, 2022
Buffer Overflows

BOF Buffer Overflows 1. BOF tips Practice using mona.py Download vulnerable exe from Exploit DB.

Vinh Nguyễn 27 Dec 08, 2022
LSO, also known as Linux Swap Operator, is a software with both GUI and terminal versions that you can manage the Swap area for Linux operating systems.

LSO - Linux Swap Operator Türkçe - LSO Nedir? LSO, diğer adıyla Linux Swap Operator Linux işletim sistemleri için Swap alanını yönetebileceğiniz hem G

Eren İnce 4 Feb 09, 2022
Package to provide translation methods for pyramid, and means to reload translations without stopping the application

Package to provide translation methods for pyramid, and means to reload translations without stopping the application

Grzegorz Śliwiński 4 Nov 20, 2022
An example project which contains the Unity components necessary to complete Navigation2's SLAM tutorial with a Turtlebot3, using a custom Unity environment in place of Gazebo.

Navigation 2 SLAM Example This example provides a Unity Project and a colcon workspace that, when used together, allows a user to substitute Unity as

Unity Technologies 183 Jan 04, 2023
Uproot - A script to bring deeply nested files or directories to the surface

UPROOT Bring deeply nested files or folders to the surface Uproot helps convert

Ted 2 Jan 15, 2022
Scraper pour les offres de stage Tesla et les notes sur Oasis (Polytech Paris-Saclay) sous forme de bot Discord

Scraper pour les offres de stage Tesla et les notes sur Oasis (Polytech Paris-Saclay) sous forme de bot Discord

Alexandre Malfreyt 1 Jan 21, 2022
Nesse repositório serão armazenados os conteúdos de aula

Lets_Code_DS_Degree_Alunos Nesse repositório serão armazenados os conteúdos de aula Formato das aulas: Notebook de aula já vem comentado para reduzir

Patricia Bongiovanni Catandi 6 Jan 21, 2022
The ROS publisher/subscriber example packaged as a snap

publisher-subscriber The ROS publisher/subscriber example packaged as a snap, based on ROS Noetic and Ubuntu Core 20. Strictly confined. This example

3 Dec 03, 2021
🦕 Compile Deno executables and compress them for all platforms easily

Denoc Compile Deno executables and compress them for all platforms easily. Install You can install denoc from PyPI like any other package: pip install

Eliaz Bobadilla 8 Apr 04, 2022
Cardano SundaeSwap ISO SPO vote ranking script

Cardano SundaeSwap ISO SPOs vote ranking This Python 3 script uses the database populated by cardano-db-sync from the Cardano blockchain to generate a

SM₳UG 1 Nov 17, 2021
The official Repository wherein newbies into Open Source can Contribute during the Hacktoberfest 2021

Hacktoberfest 2021 Get Started With your first Contrinution/Pull Request : Fork/Copy the repo by clicking the right most button on top of the page. Go

HacOkars 25 Aug 20, 2022
XAC HID Gamepad implementation for CircuitPython 7 or above.

CircuitPython_XAC_Gamepad Setup process Install CircuitPython 7 or above in your board. Add the init.py file under \lib\adafruit_hid directory of CIRC

5 Dec 19, 2022
Мой первый калькулятор!!!!!!

my_first_calculator Первый калькулятор созданный мною на питоне Версия калькулятора: 0.0.4 Как скачать? TERMUX Для скрипта нужен питон, скачиваем pkg

Lesha Russkiyov 2 Dec 29, 2021
emoji-math computes the given python expression and returns either the value or the nearest 5 emojis as measured by cosine similarity.

emoji-math computes the given python expression and returns either the value or the nearest 5 emojis as measured by cosine similarity.

Andrew White 13 Dec 11, 2022
Get the stats of a (or more) Hypixel player(s)

Hypixel_Stats Get the statistics of a (or more) Hypixel player(s) Who needs this? Everyone who plays a lot of Minecraft and often plays on mc.hypixel.

Finnomator 1 Feb 12, 2022
用于红队成员初步快速攻击的全自动化工具。

关于 Author:m0sway Mail:[email protected] Github:https://www.github.com/m0sway/Jud JuD是

m0sway 46 Jul 21, 2022
It is Keqin Wang first project in CMU, trying to use DRL(PPO) to control a 5-dof manipulator to draw line in space.

5dof-robot-writing this project aim to use PPO control a 5 dof manipulator to draw lines in 3d space. Introduction to the files the pybullet environme

Keqin Wang 4 Aug 22, 2022
Remove Sheet Protection from .xlsx files. Easily.

🔓 Excel Sheet Unlocker Remove sheet protection from .xlsx files. How to use Run Run the script/packaged executable from the command line. Universal u

Daniel 3 Nov 16, 2022