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

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
Chapter 1 Inheritance University Of Ha’il.
برنامه سازي پيشرفته 1 4. using System; //A class represents a reference type in C# class Fraction { public int numerator; public int denominator; public.
 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2Base Classes and Derived Classes.
Logic Block Architectures. 2 Crosspoint Solution  Requires the use of large amounts of programmable interconnect −  suffer from area-inefficiency 
در جامعه، افرادي که موقعيت و مسؤليت خود را مي‌دانند از راحتي و امنيت بيشتري برخوردارند هر کلاس از جامعه، مجموعه‌اي از امکانات و تواناييهاي مجاز برخوردار.
Inheritance Inheritance Reserved word protected Reserved word super
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
سازگاري فرايندهاي يادگيري Consistency of Learning Processes ارائه دهنده : الهام باوفای حقیقی استاد درس : آقای دکتر شيري دانشگاه امير كبير دانشكده ‌ مهندسي.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
1 به نام آفريننده زيباييها دانشگاه صنعتی خواجه نصیرالدین طوسی برنامه نویسی پيشرفته کامپیوتر نیم سال اول،
مهندسی نرم افزار مبتنی بر عامل
ارائه روشي براي شناسايي کاراکترهاي دستنويس، برپايه شبکه LVQ.
طراحي و ساخت سيستم‌هاي تجارت الکترونيک
نام و نام خانوادگي : فريد ملازم 1 آزمايشکاه سيستم هاي هوشمند ( موضوع ارائه ارتباط بين component ها.
1 Overloading vs. Overriding b Don't confuse the concepts of overloading and overriding b Overloading deals with multiple methods in the same class with.
طراحي و ساخت سيستم‌هاي تجارت الکترونيک ساخت سيستم‌هاي تجارت الکترونيک ECSE.
1 فصل 8 - طراحي زيرسيستم ها برگرفته از کتاب Large-Scale Software Architecture – Jeff Garland, Richard Anthony فرنوش گلشن آزمايشگاه سيستم هاي هوشمند بهار.
نام و نام خانوادگي : فريد ملازم 1 آزمايشکاه سيستم هاي هوشمند ( موضوع ارائه Process and Deployment Design.
شاخص هاي فرايند و پروژه درس مهندسي نرم‌افزار 2
Object-Orientated Design and Programming Unit 9: Abstract Classes and Interfaces Jin Sa.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Inheritance using Java
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
عرفان طاهری محمد رضا شیخی. ارائه در 1983 بازبینی در 1995 هفت لایه در دو دسته Open Systems Interconnection.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Computer Science [3] Java Programming II - Laboratory Course Lab 1 + 2: Review inheritance & abstract Review inheritance & abstractPolymorphism Faculty.
 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2Base Classes and Derived Classes.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
مرتضي صاحب الزماني 1 Basic Graph Algorithms. مرتضي صاحب الزماني 2 Graph Data Structures Adjacency Matrix [©Bazargan]
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
What Is Inheritance? Provides a way to create a new class from an existing class New class can replace or extend functionality of existing class Can be.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
 Hassan jamali In the name of God Grade: three CD شامل : مكالمه لغات جديد درس نقاط گرامري و تمرين است.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
Solving problems by searching Chapter 3 Modified by Vali Derhami.
Coming up: Inheritance
روابط مجموعه ها سلیمی. دکتر سلیمانی. نظریه فازی در سال 1965 بوسیله یک دانشمند ایرانی بنام پروفسور لطفی زاده معرفی گردید. گرچه این نظریه در ابتدا با.
Inherited Classes in Java CSCI 392 Ch 6 in O’Reilly Adapted from Dannelly.
اسامي شناسه ها (Identifier names) اسامي متغيرها ، توابع ، برچسب ها (labels) وبقيه اشياء تعريف شده توسط كاربر در C ، شناسه ( identifier ) ناميده مي شود.
1 فصل دوم تبديلات. 2 فصل دوم سرفصل مطالب مقدمه ضرب بردارها دستگاه ‌ هاي مختصات دوران ‌ ها مختصات همگن دوران ‌ ها و انتقال ‌ ها تبديلات تركيبي همگن تبديل.
شرط و تصميم اصول كامپيوتر 1. 2 الگوريتم اقليدس E1: [find remainder] Divide m by n and let r be the remainder. Clearly, 0
Class Inheritance SWE 344 Internet Protocols & Client Server Programming.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
پشته Stack ساختمان داده ها و الگوريتمها. 2 آشنايي مجموعه پويا (Dynamic Set ): مجموعه اي است كه تركيب و تعداد عناصر آن ممكن است در طول زمان اجراي برنامه.
1 فصل سوم سينماتيك مستقيم. 2 محتواي فصل   تعريف مجموعه فازي   تابع عضويت   نمايش مجموعه هاي فازي   برش آلفا   متغيرهاي زباني   ساخت مجموعه.
Information Retrieval
Week 8 Lecture -3 Inheritance and Polymorphism
OOP’S Concepts in C#.Net
ANOVA: Analysis Of Variance
ANOVA: Analysis Of Variance
به نام حق مبحث: ارث بری Inheritance.
تبدیل فوریه (Fourier Transform)
نمايش اعداد در کامپيوتر چهار عمل اصلي
Inherited Classes in Java
گزگز و خواب رفتگي انگشتان دست
سيستمهاي اطلاعات مديريت
هیدرولیک جریان در کانالهای باز
Object-Oriented Programming
اشياء و کلاس ها.
مدلسازي تجربي – تخمين پارامتر
Chapter 10: Method Overriding and method Overloading
Method Overriding and method Overloading
Inheritance in C++ Inheritance Protected Section
Programming in C# CHAPTER 5 & 6
Presentation transcript:

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

وراثت 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 ) را به ارث مي ‌ برد. خصوصيات كلاس پدر شامل متدها و اعضاي داده ‌ اي مي ‌ باشد.

انواع وراثت 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++).

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

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

1. using System; 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. } public class GermanShepard : Dog 14. { 15. public void OnGuard( ) 16. { Console.WriteLine("In Guard Mode"); } 17. } public class JackRussell : Dog 20. { 21. public void Chew( ) 22. { Console.WriteLine("I'm chewing your favorite shoes!"); } 23. }

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!

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

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

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. } 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. } 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. }

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; //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

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

1. using System; 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. } 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. } 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... فراخواني سازنده و مخرب

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