2020-12-10
  • |
  • daafoor
  • |
  • مشاهدات: 1551

ما هو تعدد الأشكال Polymorphism : تعني كلمة تعدد الأشكال وجود العديد من الأشكال. في البرمجة ، يعني تعدد الأشكال نفس اسم التابع (لكن محتوى التابع مختلف ضمن كل صنف).

 

مثال على ال polymorphism في بايثون:

# 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])) 

المخرجات:

5
3

تعدد الأشكال ضمن الصنف:

يوضح الكود أدناه كيف يمكن لبايثون استخدام نوعين مختلفين من الفئات ، بنفس الطريقة. نقوم بإنشاء حلقة for تتكرر خلال مجموعة من الكائنات.

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() 

المخرجات:

New Delhi is the capital of India.
Hindi is the most widely spoken language of India.
India is a developing country.
Washington, D.C. is the capital of USA.
English is the primary language of USA.
USA is a developed country.

 

تعدد الأشكال مع الوراثة:

class Bird: 
def intro(self): 
	print("There are many types of birds.") 
	
def flight(self): 
	print("Most of the birds can fly but some cannot.") 
	
class sparrow(Bird): 
def flight(self): 
	print("Sparrows can fly.") 
	
class ostrich(Bird): 
def flight(self): 
	print("Ostriches cannot fly.") 
	
obj_bird = Bird() 
obj_spr = sparrow() 
obj_ost = ostrich() 

obj_bird.intro() 
obj_bird.flight() 

obj_spr.intro() 
obj_spr.flight() 

obj_ost.intro() 
obj_ost.flight() 

المخرجات:

There are many types of birds.
Most of the birds can fly but some cannot.
There are many types of birds.
Sparrows can fly.
There are many types of birds.
Ostriches cannot fly.

تعدد الأشكال مع التوابع والاصناف:

def func(obj): 
	obj.capital() 
	obj.language() 
	obj.type() 

obj_ind = India() 
obj_usa = USA() 

func(obj_ind) 
func(obj_usa) 

 

 

تطبيق الوراثة المتعددة الاشكال polymorphism باستخدام تابع:

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.") 

def func(obj): 
	obj.capital() 
	obj.language() 
	obj.type() 

obj_ind = India() 
obj_usa = USA() 

func(obj_ind) 
func(obj_usa) 

المخرجات:

New Delhi is the capital of India.
Hindi is the most widely spoken language of India.
India is a developing country.
Washington, D.C. is the capital of USA.
English is the primary language of USA.
USA is a developed country.

 

 

ابحث عن مسائل برمجة بايثون | Python programming بالانجليزي

هل أعجبك المحتوى؟

محتاج مساعدة؟ تواصل مع مدرس اونلاين الان!

التعليقات
لا يوجد تعليقات
لاضافة سؤال او تعليق على المشاركة يتوجب عليك تسجيل الدخول
تسجيل الدخول