Presentation is loading. Please wait.

Presentation is loading. Please wait.

مظفر بگ محمدی دانشگاه ایلام Generics. کدهای عمومی 2 یکی از اهداف OOP ایجاد قابلیت نوشتن برنامه های عمومی با قابلیت استفاده ی مجدد است. کدهای چندریخت تا.

Similar presentations


Presentation on theme: "مظفر بگ محمدی دانشگاه ایلام Generics. کدهای عمومی 2 یکی از اهداف OOP ایجاد قابلیت نوشتن برنامه های عمومی با قابلیت استفاده ی مجدد است. کدهای چندریخت تا."— Presentation transcript:

1 مظفر بگ محمدی دانشگاه ایلام Generics

2 کدهای عمومی 2 یکی از اهداف OOP ایجاد قابلیت نوشتن برنامه های عمومی با قابلیت استفاده ی مجدد است. کدهای چندریخت تا حدودی این کار را انجام می دهند ولی در چندریختی ما به سلسله مراتب محدود هستیم. تعمیم ابزار قدرتمند تری است که اجازه می دهد کدهای عمومی طوری نوشته شوند که با هر کلاسی در هر سلسه مراتبی کار کنند.

3 3 تعریف کلاس به همراه پارامتر type کلاسی که یک یا چند پارامتر type دارد کلاس تعمیمی یا دارای پارامتر نامیده می شود. پارامتر type در داخل علائم <> قرار داده می شود و به سرآیند کلاس اضافه می گردد. از هر شناسه ای می توان برای مشخص کردن پارامتر type استفاده کرد. ولی معمولاً از حروف بزرگ استفاده می گردد. می توان از پارامتر type مثل هر نوع دیگری در تعریف کلاس (بدنه ی آن) استفاده کرد.

4 تعمیم Cell 4 public class Cell3 { private T prisoner; public Cell3( T p){ prisoner = p; } public T getPrisoner( ) { return prisoner; } } باید قبل از استفاده ی کلاس در برنامه، نوع پارامتر کلاس مشخص شود.

5 مثال تعمیم Cell 5 public class Cell3Demo { public static class Cell3 { private T prisoner; public Cell3( T p) { prisoner = p; } public T getPrisoner( ) { return prisoner; } } public static void main (String[ ] args) { // define a cell for PickPockets Cell3 ppCell = new Cell3 ( new PickPocket() ); // define a cell for thieves Cell3 tCell = new Cell3 ( new Thief( ) ); // compiler error if we remove a Thief from PickPocket Cell Thief thief = (Thief)ppCell.getPrisoner( ); }

6 6 نمی توان از انواع اصلی به عنوان پارامتر استفاده کرد پارامتر type حتماً باید از نوع مرجع (مثل کلاسها و آرایه ها) باشد. لذا نمی تواند int ، double یا char باشد. اما با توجه به پشتیبانی جاوا از بسته بندی خودکار و وجود کلاسهای معادل (مثل Integer و Character) این موضوع محدودیتی ایجاد نمی کند.

7 7 در همه ی جاهایی که از اسم type استفاده می شود، نمی توان از پارامتر type استفاده کرد. با توجه به تعریف پارامتر نوع در سرآیند کلاس تعمیمی، جاهایی در متدهای این کلاس وجود دارد که استفاده از یک اسم معمولی کلاس مجاز است. اما نمی توانیم که از پارامتر نوع استفاده کنیم. مثلاً نمی توان از پارامتر نوع در ایجاد شی استفاده کرد. T object = new T(); T[] a = new T[10]; استفاده از آرایه ای مثل زیر هم غیر قانونی است. Cell3 [] a = new Cell3 [10];

8 8 کلاس زوج مرتب تعمیمی (1 از 4)

9 9 کلاس زوج مرتب تعمیمی (2 از 4)

10 10 کلاس زوج مرتب تعمیمی (3 از 4)

11 11 کلاس زوج مرتب تعمیمی (4 از 4)

12 12 استفاده از کلاس زوج مرتب تعمیمی (1 از 3)

13 استفاده از کلاس زوج مرتب تعمیمی (2 از 3)

14 استفاده از کلاس زوج مرتب تعمیمی (3 از 3)

15 استفاده از کلاس زوج مرتب تعمیمی و بسته بندی خودکار (1 از 3)

16 استفاده از کلاس زوج مرتب تعمیمی و بسته بندی خودکار (2 از 3)

17 استفاده از کلاس زوج مرتب تعمیمی و بسته بندی خودکار (3 از 3)

18 18 استفاده از چندین پارامتر type (1 از 4)

19 19 استفاده از چندین پارامتر type (2 از 4)

20 20 استفاده از چندین پارامتر type (3 از 4)

21 21 استفاده از چندین پارامتر type (4 از 4)

22 22 استفاده از زوج مرتب دونوعی (1 از 2)

23 23 استفاده از زوج مرتب دونوعی (1 از 2)

24 24 محدودیت برای پارامترهای نوع بعضی اوقات لازم است نوع پارامترهای قابل استفاده در کلاس را محدود کنیم. مثلاً ممکن است فقط از کلاسهایی استفاده کنیم که واسط Comparable را پیاده کرده باشند. public class RClass > هر گونه تلاشی برای استفاده از پارامتری که واسط Comparable را پیاده نکرده است، منجر به تولید خطای کامپایل می شود.

25 مثال 25 // Pair container, but only for those classes that // implement the Comparable interface public class Pair > { private T first; private T second; public T max( ) { if (first.compareTo(second) <= 0) return second; else return first; } // remaining Pair code }

26 مرتب کردن عمومی 26 public class Sort { public static > void bubbleSort(T[] a) { for (int i = 0; i< a.length - 1; i++) for (int j = 0; j < a.length -1 - i; j++) if (a[j+1].compareTo(a[j]) < 0) { T tmp = a[j]; a[j] = a[j+1]; a[j+1] = tmp; }

27 27 Bounds for Type Parameters A bound on a type may be a class name Then only descendent classes of the bounding class may be plugged in for the type parameters public class ExClass A bounds expression may contain multiple interfaces and up to one class

28 Generics and Hierarchies Nov 7. 2007 28 What if we want a somewhat specialized container that assumes the objects it holds are part of a hierarchy so that the container code can assume the existence of a particular method? Let’s look at Animals, Dogs, and Cats class Animal { public void speak(){...}... } class Dog extends Animal {...} class Cat extends Animal {...} We would like to create a container named Zoo to hold some animals that speak.

29 Zoo Nov 7. 2007 29 If we define the Zoo like this public class Zoo we’ll get a compiler error when we try to invoke the speak( ) method. Not all classes provide a method called speak( ). Only Animals provide speak( ). The solution is to place a bounds on T. The Zoo can only contain Animals or any type that inherits from Animal. public class Zoo The phrase T extends Animal means “Animal or any subtype of Animal”

30 Generics and Hierarchy Nov 7. 2007 30 For example, suppose we revisit the Animals. Each animal has a weight and a name. Let’s say two Dogs (or two Cats) are the equal if they have the same name and weight. class Animal { private String name; private int weight;...} class Dog extends Animal implements Comparable {... } class Cat extends Animal implements Comparable {... } Since Dog implements comparable it’s clear you can compare Dogs with Dogs, but not with Cats We can use our bubbleSort method with Dogs or with Cats

31 Sorting Dogs Nov 7. 2007 31 public class DogSort { public static void main( String[ ] args ) { // create an array of Dogs Dog[ ] dogs = new Dog[ 42 ]; // put some dogs in the array //...... // sort the dogs Sort.bubbleSort( dogs ); // rest of main }

32 Generics and Hierarchies Nov 7. 2007 32 What happens if we want to sort a class in an inheritance hierarchy, but some ancestor of the class implements comparable, and not the class itself? But suppose we wanted compare all Animals using only their weight. The class definitions would look something like this class Animal implements Comparable {...} class Dog extends Animal {... } class Cat extends Animal {... } Since Animal implements comparable, any two Animals can be compared (albeit only by weight). The problem is now that we can’t use bubbleSort to sort an array of Dogs because Dog doesn’t explicitly implement Comparable (it’s inherited from Animal)

33 New bubbleSort Nov 7. 2007 33 The solution is to use a “wildcard” when defining bubbleSort public class Sort { public static > void bubbleSort(T[] a) { for (int i = 0; i< a.length - 1; i++) for (int j = 0; j < a.length -1 - i; j++) if (a[j+1].compareTo(a[j]) < 0) { T tmp = a[j]; a[j] = a[j+1]; a[j+1] = tmp; } ? super T is read as “any supertype of T”. Now, because Dog extends Animal which implements Comparable, bubbleSort can be used with an array of Dogs as before.

34 Nov 7. 2007 34 Pitfall: A Generic Class Cannot Be an Exception Class It is not permitted to create a generic class with Exception, Error, Throwable, or any descendent class of Throwable A generic class cannot be created whose objects are throwable public class GEx extends Exception The above example will generate a compiler error message Copyright © 2008 Pearson Addison-Wesley. All rights reserved

35 Nov 7. 2007 35 Tip: Generic Interfaces An interface can have one or more type parameters The details and notation are the same as they are for classes with type parameters Copyright © 2008 Pearson Addison-Wesley. All rights reserved

36 Nov 7. 2007 36 Generic Methods When a generic class is defined, the type parameter can be used in the definitions of the methods for that generic class In addition, a generic method can be defined that has its own type parameter that is not the type parameter of any class A generic method can be a member of an ordinary class or a member of a generic class that has some other type parameter The type parameter of a generic method is local to that method, not to the class Copyright © 2008 Pearson Addison-Wesley. All rights reserved

37 Nov 7. 2007 37 Generic Methods The type parameter must be placed (in angular brackets) after all the modifiers, and before the returned type public class Utility {... public static T getMidPoint( T[ ] array) { return array[array.length / 2]; } public static T getFirst( T[ ] a ) { return a[0]; }... } Copyright © 2008 Pearson Addison-Wesley. All rights reserved

38 Generic Methods Nov 7. 2007 38 When one of these generic methods is invoked, the method name is prefaced with the type to be plugged in, enclosed in angular brackets String s = Utility. getMidPoint(arrayOfStrings); double first = Utility. getFirst(arrayOfDoubles);

39 Nov 7. 2007 39 Inheritance with Generic Classes A generic class can be defined as a derived class of an ordinary class or of another generic class As in ordinary classes, an object of the subclass type would also be of the superclass type Given two classes: A and B, and given G: a generic class, there is no relationship between G and G This is true regardless of the relationship between class A and B, e.g., if class B is a subclass of class A Copyright © 2008 Pearson Addison-Wesley. All rights reserved

40 Nov 7. 2007 40 A Derived Generic Class (1 of 2) Copyright © 2008 Pearson Addison-Wesley. All rights reserved In this example UnorderedPair overrides equals( ) that was inherited from Pair

41 Nov 7. 2007 41 A Derived Generic Class (Part 2 of 2) Copyright © 2008 Pearson Addison-Wesley. All rights reserved

42 Nov 7. 2007 42 Using UnorderedPair (Part 1 of 2) Copyright © 2008 Pearson Addison-Wesley. All rights reserved

43 Nov 7. 2007 43 Using UnorderedPair (Part 2 of 2) Copyright © 2008 Pearson Addison-Wesley. All rights reserved


Download ppt "مظفر بگ محمدی دانشگاه ایلام Generics. کدهای عمومی 2 یکی از اهداف OOP ایجاد قابلیت نوشتن برنامه های عمومی با قابلیت استفاده ی مجدد است. کدهای چندریخت تا."

Similar presentations


Ads by Google