Presentation is loading. Please wait.

Presentation is loading. Please wait.

برنامه سازي پيشرفته 5 Inheritance. وراثت Inheritance allows a software developer to derive a new class from an existing one The existing class is called.

Similar presentations


Presentation on theme: "برنامه سازي پيشرفته 5 Inheritance. وراثت Inheritance allows a software developer to derive a new class from an existing one The existing class is called."— Presentation transcript:

1 برنامه سازي پيشرفته 5 Inheritance

2 وراثت Inheritance allows a software developer to derive a new class from an existing one The existing class is called the parent class, or superclass, or base class The derived class is called the child class, or subclass, or derived class. كلاس مشتق شده ( derived ) خصوصيات كلاس پدر ( parent ) را به ارث مي ‌ برد. خصوصيات كلاس پدر شامل متدها و اعضاي داده ‌ اي مي ‌ باشد.

3 انواع وراثت Single inheritance: a class may be inherited from one base class only (Java, C-Sharp). Multiple inheritance:a class may be inherited from many base classes (C++).

4 Declaring a Derived Class Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class contents }

5 دسترسي ‌ ها در وراثت اگر متدها و فيلدهاي كلاسي به صورت private تعريف شوند، فقط در همان كلاس قابل دستيابي هستند. اگر متدها و فيلدهاي كلاسي به صورت public تعريف شوند، در كلاس مشتق و هر كلاس ديگري قابل دستيابي هستند. اگر متدها و فيلدهاي كلاسي به صورت protected تعريف شوند، فقط در كلاس مشتق قابل دستيابي هستند.

6 1. using System; 2. 3. public class Dog 4. { 5. public string Name; 6. public int Weight; 7. public void Speak( ) 8. { Console.WriteLine("ruff!"); } 9. public void DrinkWater( ) 10. { Console.WriteLine("Gulp"); } 11. } 12. 13. public class GermanShepard : Dog 14. { 15. public void OnGuard( ) 16. { Console.WriteLine("In Guard Mode"); } 17. } 18. 19. public class JackRussell : Dog 20. { 21. public void Chew( ) 22. { Console.WriteLine("I'm chewing your favorite shoes!"); } 23. }

7 24. public class Inherit 25. { 26. public static void Main( ) 27. { 28. GermanShepard Simon = new GermanShepard( ); 29. JackRussell Daisy = new JackRussell( ); 30. //Both Simon and Daisy have a name and weight 31. Simon.Name = "Simon"; Simon.Weight = 85; 32. Daisy.Name = "Daisy"; Daisy.Weight = 25; 33. //Both Simon and Daisy have the speak and DrinkWater methods 34. Simon.Speak( ); Simon.DrinkWater( ); 35. Daisy.Speak( ); Daisy.DrinkWater( ); 36. //Only Simon has the OnGuard Method 37. Simon.OnGuard( ); 38. //Only Diasy has the Chew method 39. Daisy.Chew( ); 40. } 41. } ruff! Gulp ruff! Gulp In Guard Mode I'm chewing your favorite shoes!

8 چند ريختي (Polymorphism) قابليت لغو يك متد كلاس پايه و ارائه يك پياده ‌ سازي ديگر در يك كلاس مشتق را چندريختي گويند. كلمه كليدي virtual براي بيان متدي است كه مي ‌ تواند توسط كلاس مشتق لغو شود. كلاس مشتق شده مي ‌ تواند متد virtual را با استفاده از كلمه كليدي override لغو سازد.

9 Overloading vs. Overriding Overloading deals with multiple methods in the same class with the same name but different signatures Overloading lets you define a similar operation in different ways for different data Overriding deals with two methods, one in a parent class and one in a child class, that have the same signature Overriding lets you define a similar operation in different ways for different object types

10 1. using System; 2. public class Dog 3. { 4. public string Name; 5. public int Weight; 6. public virtual void Speak( ) 7. { Console.WriteLine("ruff!"); } 8. public void DrinkWater( ) 9. { Console.WriteLine("Gulp"); } 10. } 11. 12. public class GermanShepard : Dog 13. { 14. public void OnGuard( ) 15. { Console.WriteLine("In Guard Mode"); } 16. public override void Speak( ) 17. { Console.WriteLine("RUFF,RUFF,RUFF"); } 18. } 19. 20. public class JackRussell : Dog 21. { 22. public void Chew( ) 23. { Console.WriteLine("I'm chewing your favorite shoes!"); } 24. public override void Speak( ) 25. { Console.WriteLine("yap,yap,yap"); } 26. }

11 27. public class Inherit 28. { 29. public static void Main( ) 30. { 31. GermanShepard Simon = new GermanShepard( ); 32. JackRussell Daisy = new JackRussell( ); 33. //Both Simon and Daisy have a name and weight 34. Simon.Name = "Simon"; Simon.Weight = 85; 35. Daisy.Name = "Daisy"; Daisy.Weight = 25; 36. 37. //Polymorphism allows for the proper method to be called 38. //based on runtime type information 39. Dog d = new Dog(); 40. d.Speak(); 41. d = Simon; 42. d.Speak( );//calls GermanShepard.Speak( ); 43. d = Daisy; 44. d.Speak( );//calls JackRussell.Speak( ); 45. } 46. } ruff! RUFF,RUFF,RUFF yap,yap,yap

12 فراخواني سازنده و مخرب در كلاس ‌ هاي مشتق ابتدا سازنده كلاس پايه و سپس سازنده كلاس مشتق فراخواني مي ‌ شود. ابتدا مخرب كلاس مشتق و سپس مخرب كلاس پايه فراخواني مي ‌ شود. در مثال بعد ابتدا شيء fido از بين مي ‌ رود، چون ديرتر ايجاد شد. به عبارت ديگر هر شيء ‌ اي كه زودتر ايحاد شود، ديرتر از بين مي ‌ رود.

13 1. using System; 2. 3. public class Mammal 4. { 5. public Mammal() { Console.WriteLine("Mammal constructor..."); } 6. ~Mammal() { Console.WriteLine("Mammal destructor..."); } 7. public virtual void Speak() { Console.WriteLine("Mammal sound!"); } 8. } 9. 10. public class Dog : Mammal 11. { 12. public Dog() { Console.WriteLine("Dog constructor..."); } 13. ~Dog() { Console.WriteLine("Dog destructor..."); } 14. public override void Speak() { Console.WriteLine("Dog sound!"); } 15. } 16. 17. public class Inherit 18. { 19. public static void Main() 20. { 21. Mammal bigAnimal = new Mammal(); 22. Dog fido = new Dog(); 23. bigAnimal.Speak(); 24. fido.Speak(); 25. } 26. } Mammal constructor... Dog constructor... Mammal sound! Dog sound! Dog destructor... Mammal destructor... فراخواني سازنده و مخرب

14 public class Point { protected int xCoordinate, yCoordinate; public Point() : this(0, 0) {} public Point(int xValue, int yValue) { xCoordinate = xValue; yCoordinate = yValue; } } // end class Point public class Circle : Point { protected double radius; public Circle() { radius = 0.0; } public Circle(int xValue, int yValue, double radiusValue) : base(xValue, yValue) { radius = radiusValue; } public double Area() { return Math.PI * radius * radius; } } // end class Circle فراخواني سازنده كلاس پايه با استفاده از base


Download ppt "برنامه سازي پيشرفته 5 Inheritance. وراثت Inheritance allows a software developer to derive a new class from an existing one The existing class is called."

Similar presentations


Ads by Google