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

الوراثة في البرمجة هي قدرة صنف class ما على اشتقاق أو وراثة الخصائص من صنف class اخر.

فوائد الوراثة:

  • إنه يمثل علاقات العالم الحقيقي بشكل جيد.
  • يوفر إمكانية إعادة استخدام الكود البرمجي, لا يتعين علينا كتابة نفس الرمز مرارًا وتكرارًا. ويسمح لنا ايضا بإضافة المزيد من الميزات إلى الصنف class دون تعديله.
  • إنها متعدية بطبيعتها ، مما يعني أنه إذا ورثت الصنف B من صنف اخر A ، فإن جميع الاصناف الفرعية من B سترث تلقائيًا من الصنف A.

 

الصنف الاب يسمى super class  والصنف الابن يسمى sub class

مثال بسيط على الوراثة في بايثون Python:

# A Python program to demonstrate inheritance 

# Base or Super class. Note object in bracket. 
# (Generally, object is made ancestor of all classes) 
# In Python 3.x "class Person" is 
# equivalent to "class Person(object)" 
class Person(object): 
	
	# Constructor 
	def __init__(self, name): 
		self.name = name 

	# To get name 
	def getName(self): 
		return self.name 

	# To check if this person is an employee 
	def isEmployee(self): 
		return False


# Inherited or Subclass (Note Person in bracket) 
class Employee(Person): 

	# Here we return true 
	def isEmployee(self): 
		return True

# Driver code 
emp = Person("Geek1") # An Object of Person 
print(emp.getName(), emp.isEmployee()) 

emp = Employee("Geek2") # An Object of Employee 
print(emp.getName(), emp.isEmployee()) 

المخرجات:

Geek1 False
Geek2 True

 

مثال على الوراثة من الصنف object, الصنف object هو الاب لجميع الاصناف في بايثون:

# Python code to demonstrate how parent constructors 
# are called. 

# parent class 
class Person( object ):	 

		# __init__ is known as the constructor		 
		def __init__(self, name, idnumber): 
				self.name = name 
				self.idnumber = idnumber 
		def display(self): 
				print(self.name) 
				print(self.idnumber) 

# child class 
class Employee( Person ):		 
		def __init__(self, name, idnumber, salary, post): 
				self.salary = salary 
				self.post = post 

				# invoking the __init__ of the parent class 
				Person.__init__(self, name, idnumber) 

				
# creation of an object variable or an instance 
a = Employee('Rahul', 886012, 200000, "Intern")	 

# calling a function of the class Person using its instance 
a.display() 

المخرجات:

Rahul
886012

 

 

انواع الوراثة بالبرمجة:

1. الوراثة الفردية single Inheritance : عندما ترث فئة فرعية من فئة والد واحد فقط ، فإنها تسمى الميراث الفردي. و رأينا مثالا على ذلك أعلاه.

2. الوراثة المتعددة multiple Inheritance : عندما ترث فئة فرعية من عدة فئات رئيسية ، فإنها تسمى الوراثة المتعددة.

على عكس Java ومثل C++ ، تدعم Python الوراثة المتعددة. نحدد جميع الفئات الرئيسية كقائمة مفصولة بفواصل في القوس.

مثال على الوراثة المتعددة multiple Inheritance في بايثون:

# Python example to show the working of multiple 
# inheritance 
class Base1(object): 
	def __init__(self): 
		self.str1 = "Geek1"
		print("Base1") 

class Base2(object): 
	def __init__(self): 
		self.str2 = "Geek2"		
		print("Base2") 

class Derived(Base1, Base2): 
	def __init__(self): 
		
		# Calling constructors of Base1 
		# and Base2 classes 
		Base1.__init__(self) 
		Base2.__init__(self) 
		print("Derived") 
		
	def printStrs(self): 
		print(self.str1, self.str2) 
		

ob = Derived() 
ob.printStrs() 

 

المخرجات:

Base1
Base2
Derived
Geek1 Geek2

3.الوراثة متعددة المستويات Multilevel inheritance : عندما يكون لدينا صنف جد وصنف اب وصنف حفيد.

مثال على Multilevel inheritance: 

# A Python program to demonstrate inheritance 

# Base or Super class. Note object in bracket. 
# (Generally, object is made ancestor of all classes) 
# In Python 3.x "class Person" is 
# equivalent to "class Person(object)" 
class Base(object): 
	
	# Constructor 
	def __init__(self, name): 
		self.name = name 

	# To get name 
	def getName(self): 
		return self.name 


# Inherited or Sub class (Note Person in bracket) 
class Child(Base): 
	
	# Constructor 
	def __init__(self, name, age): 
		Base.__init__(self, name) 
		self.age = age 

	# To get name 
	def getAge(self): 
		return self.age 

# Inherited or Sub class (Note Person in bracket) 
class GrandChild(Child): 
	
	# Constructor 
	def __init__(self, name, age, address): 
		Child.__init__(self, name, age) 
		self.address = address 

	# To get address 
	def getAddress(self): 
		return self.address		 

# Driver code 
g = GrandChild("Geek1", 23, "Noida") 
print(g.getName(), g.getAge(), g.getAddress()) 

المخرجات:

Geek1 23 Noida

 

4. الوراثة الهرمية Hierarchical inheritance : يتم إنشاء أكثر من فئة مشتقة من قاعدة واحدة.

5. الوراثة الهجينة Hybrid inheritance : يجمع هذا الشكل بين أكثر من شكل من أشكال الوراثة. في الأساس ، هو مزيج من أكثر من نوع واحد من الوراثة.

 

 

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

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

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

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