“Polimorfisme dalam Python” Kode Jawaban

Menunjukkan polimorfisme di kelas Python

class India():
    def capital(self):
        print("New Delhi is the capital of India.")
 
    def language(self):
        print("Hindi is the most widely spoken language of India.")
 
    def type(self):
        print("India is a developing country.")
 
class USA():
    def capital(self):
        print("Washington, D.C. is the capital of USA.")
 
    def language(self):
        print("English is the primary language of USA.")
 
    def type(self):
        print("USA is a developed country.")
 
obj_ind = India()
obj_usa = USA()
for country in (obj_ind, obj_usa):
    country.capital()
    country.language()
    country.type()
mathiasgodwin

Polimorfisme dalam Python

# Python program to demonstrate in-built poly-
# morphic functions

# len() being used for a string
print(len("geeks"))

# len() being used for a list
print(len([10, 20, 30]))
launchable 720

Menggunakan polimorfisme dalam python

class Parrot:
    def fly(self):
        print("Parrot can fly")
        def swim(self):
        print("Parrot can't swim")
class Penguin:
    def fly(self):
        print("Penguin can't fly")
        def swim(self):
        print("Penguin can swim")
# common interface
def flying_test(bird):
    bird.fly()
#instantiate objects
blu = Parrot()
peggy = Penguin()
# passing the object
flying_test(blu)
flying_test(peggy)
SAMER SAEID

Jawaban yang mirip dengan “Polimorfisme dalam Python”

Pertanyaan yang mirip dengan “Polimorfisme dalam Python”

Lebih banyak jawaban terkait untuk “Polimorfisme dalam Python” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya