Классы с дженериками java

Generic Types

A generic type is a generic class or interface that is parameterized over types. The following Box class will be modified to demonstrate the concept.

A Simple Box Class

Begin by examining a non-generic Box class that operates on objects of any type. It needs only to provide two methods: set, which adds an object to the box, and get, which retrieves it:

public class Box < private Object object; public void set(Object object) < this.object = object; >public Object get() < return object; >>

Since its methods accept or return an Object, you are free to pass in whatever you want, provided that it is not one of the primitive types. There is no way to verify, at compile time, how the class is used. One part of the code may place an Integer in the box and expect to get Integers out of it, while another part of the code may mistakenly pass in a String, resulting in a runtime error.

A Generic Version of the Box Class

A generic class is defined with the following format:

The type parameter section, delimited by angle brackets (<>), follows the class name. It specifies the type parameters (also called type variables) T1, T2, . and Tn.

To update the Box class to use generics, you create a generic type declaration by changing the code «public class Box» to «public class Box ". This introduces the type variable, T, that can be used anywhere inside the class.

With this change, the Box class becomes:

/** * Generic version of the Box class. * @param the type of the value being boxed */ public class Box  < // T stands for "Type" private T t; public void set(T t) < this.t = t; >public T get() < return t; >>

As you can see, all occurrences of Object are replaced by T. A type variable can be any non-primitive type you specify: any class type, any interface type, any array type, or even another type variable.

This same technique can be applied to create generic interfaces.

Type Parameter Naming Conventions

By convention, type parameter names are single, uppercase letters. This stands in sharp contrast to the variable naming conventions that you already know about, and with good reason: Without this convention, it would be difficult to tell the difference between a type variable and an ordinary class or interface name.

The most commonly used type parameter names are:

  • E - Element (used extensively by the Java Collections Framework)
  • K - Key
  • N - Number
  • T - Type
  • V - Value
  • S,U,V etc. - 2nd, 3rd, 4th types

You'll see these names used throughout the Java SE API and the rest of this lesson.

Invoking and Instantiating a Generic Type

To reference the generic Box class from within your code, you must perform a generic type invocation, which replaces T with some concrete value, such as Integer:

You can think of a generic type invocation as being similar to an ordinary method invocation, but instead of passing an argument to a method, you are passing a type argumentInteger in this case — to the Box class itself.

Читайте также:  Php int array type

Type Parameter and Type Argument Terminology: Many developers use the terms "type parameter" and "type argument" interchangeably, but these terms are not the same. When coding, one provides type arguments in order to create a parameterized type. Therefore, the T in Foo is a type parameter and the String in Foo f is a type argument. This lesson observes this definition when using these terms.

Like any other variable declaration, this code does not actually create a new Box object. It simply declares that integerBox will hold a reference to a "Box of Integer", which is how Box is read.

An invocation of a generic type is generally known as a parameterized type.

To instantiate this class, use the new keyword, as usual, but place between the class name and the parenthesis:

The Diamond

In Java SE 7 and later, you can replace the type arguments required to invoke the constructor of a generic class with an empty set of type arguments (<>) as long as the compiler can determine, or infer, the type arguments from the context. This pair of angle brackets, <>, is informally called the diamond. For example, you can create an instance of Box with the following statement:

For more information on diamond notation and type inference, see Type Inference.

Multiple Type Parameters

As mentioned previously, a generic class can have multiple type parameters. For example, the generic OrderedPair class, which implements the generic Pair interface:

public interface Pair  < public K getKey(); public V getValue(); >public class OrderedPair implements Pair  < private K key; private V value; public OrderedPair(K key, V value) < this.key = key; this.value = value; >public K getKey() < return key; >public V getValue() < return value; >>

The following statements create two instantiations of the OrderedPair class:

Pair p1 = new OrderedPair("Even", 8); Pair p2 = new OrderedPair("hello", "world");

The code, new OrderedPair , instantiates K as a String and V as an Integer. Therefore, the parameter types of OrderedPair's constructor are String and Integer, respectively. Due to autoboxing, it is valid to pass a String and an int to the class.

As mentioned in The Diamond, because a Java compiler can infer the K and V types from the declaration OrderedPair , these statements can be shortened using diamond notation:

OrderedPair p1 = new OrderedPair<>("Even", 8); OrderedPair p2 = new OrderedPair<>("hello", "world");

To create a generic interface, follow the same conventions as for creating a generic class.

Parameterized Types

You can also substitute a type parameter (that is, K or V) with a parameterized type (that is, List ). For example, using the OrderedPair example:

OrderedPairBox > p = new OrderedPair<>("primes", new Box(. ));

Источник

Классы с дженериками java

Обобщения или generics (обобщенные типы и методы) позволяют нам уйти от жесткого определения используемых типов. Рассмотрим проблему, в которой они нам могут понадобиться.

Допустим, мы определяем класс для представления банковского счета. К примеру, он мог бы выглядеть следующим образом:

class Account < private int id; private int sum; Account(int id, int sum)< this.id = id; this.sum = sum; >public int getId() < return id; >public int getSum() < return sum; >public void setSum(int sum) < this.sum = sum; >>

Класс Account имеет два поля: id - уникальный идентификатор счета и sum - сумма на счете.

В данном случае идентификатор задан как целочисленное значение, например, 1, 2, 3, 4 и так далее. Однако также нередко для идентификатора используются и строковые значения. И числовые, и строковые значения имеют свои плюсы и минусы. И на момент написания класса мы можем точно не знать, что лучше выбрать для хранения идентификатора - строки или числа. Либо, возможно, этот класс будет использоваться другими разработчиками, которые могут иметь свое мнение по данной проблеме. Например, в качестве типа id они захотят использовать какой-то свой класс.

Читайте также:  Css image cover div

И на первый взгляд мы можем решить данную проблему следующим образом: задать id как поле типа Object, который является универсальным и базовым суперклассом для всех остальных типов:

public class Program < public static void main(String[] args) < Account acc1 = new Account(2334, 5000); // id - число int acc1Id = (int)acc1.getId(); System.out.println(acc1Id); Account acc2 = new Account("sid5523", 5000); // id - строка System.out.println(acc2.getId()); >> class Account < private Object id; private int sum; Account(Object id, int sum)< this.id = id; this.sum = sum; >public Object getId() < return id; >public int getSum() < return sum; >public void setSum(int sum) < this.sum = sum; >>

В данном случае все замечательно работает. Однако тогда мы сталкиваемся с проблемой безопасности типов . Например, в следующем случае мы получим ошибку:

Account acc1 = new Account("2345", 5000); int acc1Id = (int)acc1.getId(); // java.lang.ClassCastException System.out.println(acc1Id);

Проблема может показаться искуственной, так как в данном случае мы видим, что в конструктор передается строка, поэтому мы вряд ли будем пытаться преобразовывать ее к типу int. Однако в процессе разработки мы можем не знать, какой именно тип представляет значение в id, и при попытке получить число в данном случае мы столкнемся с исключением java.lang.ClassCastException.

Писать для каждого отдельного типа свою версию класса Account тоже не является хорошим решением, так как в этом случае мы вынуждены повторяться.

Эти проблемы были призваны устранить обобщения или generics. Обобщения позволяют не указывать конкретный тип, который будет использоваться. Поэтому определим класс Account как обобщенный:

class Account < private T id; private int sum; Account(T id, int sum)< this.id = id; this.sum = sum; >public T getId() < return id; >public int getSum() < return sum; >public void setSum(int sum) < this.sum = sum; >>

С помощью буквы T в определении класса class Account мы указываем, что данный тип T будет использоваться этим классом. Параметр T в угловых скобках называется универсальным параметром , так как вместо него можно подставить любой тип. При этом пока мы не знаем, какой именно это будет тип: String, int или какой-то другой. Причем буква T выбрана условно, это может и любая другая буква или набор символов.

После объявления класса мы можем применить универсальный параметр T : так далее в классе объявляется переменная этого типа, которой затем присваивается значение в конструкторе.

Метод getId() возвращает значение переменной id, но так как данная переменная представляет тип T, то данный метод также возвращает объект типа T: public T getId() .

public class Program < public static void main(String[] args) < Accountacc1 = new Account("2345", 5000); String acc1Id = acc1.getId(); System.out.println(acc1Id); Account acc2 = new Account(2345, 5000); Integer acc2Id = acc2.getId(); System.out.println(acc2Id); > > class Account < private T id; private int sum; Account(T id, int sum)< this.id = id; this.sum = sum; >public T getId() < return id; >public int getSum() < return sum; >public void setSum(int sum) < this.sum = sum; >>

При определении переменной даннного класса и создании объекта после имени класса в угловых скобках нужно указать, какой именно тип будет использоваться вместо универсального параметра. При этом надо учитывать, что они работают только с объектами, но не работают с примитивными типами. То есть мы можем написать Account , но не можем использовать тип int или double, например, Account . Вместо примитивных типов надо использовать классы-обертки: Integer вместо int, Double вместо double и т.д.

Читайте также:  Firefox плагин для css

Например, первый объект будет использовать тип String, то есть вместо T будет подставляться String:

Account acc1 = new Account("2345", 5000);

В этом случае в качестве первого параметра в конструктор передается строка.

А второй объект использует тип int (Integer):

Account acc2 = new Account(2345, 5000);

Обобщенные интерфейсы

Интерфейсы, как и классы, также могут быть обобщенными. Создадим обобщенный интерфейс Accountable и используем его в программе:

public class Program < public static void main(String[] args) < Accountableacc1 = new Account("1235rwr", 5000); Account acc2 = new Account("2373", 4300); System.out.println(acc1.getId()); System.out.println(acc2.getId()); > > interface Accountable < T getId(); int getSum(); void setSum(int sum); >class Account implements Accountable < private String id; private int sum; Account(String id, int sum)< this.id = id; this.sum = sum; >public String getId() < return id; >public int getSum() < return sum; >public void setSum(int sum) < this.sum = sum; >>

При реализации подобного интерфейса есть две стратегии. В данном случае реализована первая стратегия, когда при реализации для универсального параметра интерфейса задается конкретный тип, как например, в данном случае это тип String. Тогда класс, реализующий интерфейс, жестко привязан к этому типу.

Вторая стратегия представляет определение обобщенного класса, который также использует тот же универсальный параметр:

public class Program < public static void main(String[] args) < Accountacc1 = new Account("1235rwr", 5000); Account acc2 = new Account("2373", 4300); System.out.println(acc1.getId()); System.out.println(acc2.getId()); > > interface Accountable < T getId(); int getSum(); void setSum(int sum); >class Account implements Accountable < private T id; private int sum; Account(T id, int sum)< this.id = id; this.sum = sum; >public T getId() < return id; >public int getSum() < return sum; >public void setSum(int sum) < this.sum = sum; >>

Обобщенные методы

Кроме обобщенных типов можно также создавать обобщенные методы, которые точно также будут использовать универсальные параметры. Например:

public class Program< public static void main(String[] args) < Printer printer = new Printer(); String[] people = ; Integer[] numbers = ; printer.print(people); printer.print(numbers); > > class Printer < public void print(T[] items) < for(T item: items)< System.out.println(item); >> >

Особенностью обобщенного метода является использование универсального параметра в объявлении метода после всех модификаторов и перед типом возвращаемого значения.

Затем внутри метода все значения типа T будут представлять данный универсальный параметр.

При вызове подобного метода перед его именем в угловых скобках указывается, какой тип будет передаваться на место универсального параметра:

printer.print(people); printer.print(numbers);

Использование нескольких универсальных параметров

Мы можем также задать сразу несколько универсальных параметров:

public class Program < public static void main(String[] args) < Accountacc1 = new Account("354", 5000.87); String sum = acc1.getSum(); System.out.printf("Id: %s Sum: %f \n", id, sum); > > class Account < private T id; private S sum; Account(T id, S sum)< this.id = id; this.sum = sum; >public T getId() < return id; >public S getSum() < return sum; >public void setSum(S sum) < this.sum = sum; >>

В данном случае тип String будет передаваться на место параметра T, а тип Double - на место параметра S.

Обобщенные конструкторы

Конструкторы как и методы также могут быть обобщенными. В этом случае перед конструктором также указываются в угловых скобках универсальные параметры:

public class Program < public static void main(String[] args) < Account acc1 = new Account("cid2373", 5000); Account acc2 = new Account(53757, 4000); System.out.println(acc1.getId()); System.out.println(acc2.getId()); >> class Account< private String id; private int sum; Account(T id, int sum) < this.id = id.toString(); this.sum = sum; >public String getId() < return id; >public int getSum() < return sum; >public void setSum(int sum) < this.sum = sum; >>

В данном случае конструктор принимает параметр id, который представляет тип T. В конструкторе его значение превращается в строку и сохраняется в локальную переменную.

Источник

Оцените статью