Click here to load reader

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

Embed Size (px)

Citation preview

CMSC 202

Generics 2 OOP . . . 3 type type . type . type . . type ( ) . 33 Cell4public class Cell3< T >{private T prisoner;public Cell3( T p){ prisoner = p; }public T getPrisoner( ) { return prisoner; }} . Cell5public class Cell3Demo{ public static class Cell3< T >{private T prisoner;public Cell3( T p) { prisoner = p; }public T getPrisoner( ) { return prisoner; }}public static void main (String[ ] args){// define a cell for PickPocketsCell3 ppCell = new Cell3( new PickPocket() );

// define a cell for thievesCell3 tCell = new Cell3( new Thief( ) );

// compiler error if we remove a Thief from PickPocket CellThief thief = (Thief)ppCell.getPrisoner( );}}

6 type ( ) . int double char . ( Integer Character) . 667 type type . . . .

T object = new T();T[] a = new T[10]; . Cell3[] a = new Cell3[10];

778 (1 4)

889

(2 4)9910

(3 4)101011

(4 4)111112

(1 3)1212

(2 3)

(3 3)

(1 3)

(2 3)

(3 3)18 type (1 4)

181819

type (2 4)191920

type (3 4)202021

type (4 4)212122 (1 2)

222223

(1 2)232324 . Comparable .

public class RClass

Comparable . 242425// Pair container, but only for those classes that// implement the Comparable interfacepublic class Pair{ private T first; private T second;

public T max( ) { if (first.compareTo(second) well 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< T extends Animal >The phrase T extends Animal means Animal or any subtype of Animal

Generics and HierarchyNov 7. 200730For example, suppose we revisit the Animals.Each animal has a weight and a name. Lets 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 its clear you can compare Dogs with Dogs, but not with CatsWe can use our bubbleSort method with Dogs or with Cats

Sorting DogsNov 7. 200731public class DogSort{public static void main( String[ ] args ){// create an array of DogsDog[ ] dogs = new Dog[ 42 ];

// put some dogs in the array// ......

// sort the dogsSort.bubbleSort( dogs );

// rest of main }}Generics and HierarchiesNov 7. 200732What 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 thisclass 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 cant use bubbleSort to sort an array of Dogs because Dog doesnt explicitly implement Comparable (its inherited from Animal)

New bubbleSortNov 7. 200733The solution is to use a wildcard when defining bubbleSortpublic class Sort{public static