本文实例讲述了Python编程之多态用法。分享给大家供大家参考。具体分析如下:
什么是多态?顾名思义,多态就是多种表现形态的意思。它是一种机制、一种能力,而非某个关键字。它在类的继承中得以实现,在类的方法调用中得以体现。多态意味着变量并不知道引用的对象是什么,根据引用对象的不同表现不同的行为方式。
我们先看一个简单的例子,运算符多态:
a=34 b=57 print(a+b) a="世界" b="你好" print(a+b)
我们不知道+法运算符左右两个变量是什么类型,当我们给的是int类型时,它就进行加法运算。当我们给的是字符串类型时,它就返回的是两个字符串拼接的结果。也就是根据变量类型的不同,表现不能的形态。
我们再来看一个例子,方法多态:
我们首先创建一个名为MyClass.py的文件,代码如下
__author__= 'MXi4oyu' classPeople: def say(self): print("大家好!") classStudent: def say(self): print("老师好!")
我们再创建一个Main.py的文件,代码如下:
__author__= 'MXi4oyu' fromrandom import choice importMyClass p1=MyClass.People() stu1=MyClass.Student() #通过choice方法我们可以随机选择列表中的某一项 obj=choice([p1,stu1]) print(type(obj)) obj.say()
我们创建的临时对象obj是由随机函数取出来的,我们不知道它的具体类型,但是我们可以对它进行相同的操作。即让它调用say方法,然后根据其类型的不同,它所表现的行为不同。这就是多态。
希望本文所述对大家的Python程序设计有所帮助。

pythonusesahybridmodelofcompilation andtertation:1)thepythoninterpretercompilessourcodeodeplatform-indopent bytecode.2)thepythonvirtualmachine(pvm)thenexecuteTesthisbytecode、balancingeaseoputhswithporformance。

pythonisbothintersedand compiled.1)it'scompiledtobytecode forportabalityacrossplatforms.2)bytecodeisthenは解釈され、開発を許可します。

loopsareideal whenyouwhenyouknumberofiterationsinadvance、foreleloopsarebetterforsituationsは、loopsaremoreedilaConditionismetを使用します

henthenumber ofiterationsisknown advanceの場合、dopendonacondition.1)forloopsareideal foriterating over for -for -for -saredaverseversives likelistorarrays.2)whileopsaresupasiable forsaresutable forscenarioswheretheloopcontinupcontinuspificcond

pythonisnotpurelyLepted; itusesahybridapproachofbytecodecodecodecodecodecodedruntimerttation.1)pythoncompilessourcodeintobytecode、whodythepythonvirtualmachine(pvm).2)

ToconcatenateListsinpythothesheElements、使用:1)Operatortokeepduplicates、2)asettoremoveduplicates、or3)listcomplunting for controloverduplicates、各メトドハスディフェルフェルフェントパフォーマンスアンドソーダーインプリテーション。

pythonisantertedlanguage、useaseofuseandflexibility-butfactingporformantationationsincriticalapplications.1)解釈されたlikepythonexecuteline-by-lineを解釈します

Useforloopswhenthenumberofiterationsisknowninadvance、andwhiloopswheniterationsdependonacondition.1)forloopsareidealforsecenceslikelistoranges.2)


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

Dreamweaver Mac版
ビジュアル Web 開発ツール

MantisBT
Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

SublimeText3 中国語版
中国語版、とても使いやすい

SublimeText3 英語版
推奨: Win バージョン、コードプロンプトをサポート!
