Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GenericsType<T> {
- private T t;
- public T get(){
- return this.t;
- }
- public void set(T t1){
- this.t=t1;
- }
- public static void main(String args[]){
- GenericsType<String> type = new GenericsType<>();
- type.set("Pankaj"); //valid
- GenericsType type1 = new GenericsType(); //raw type
- type1.set("Pankaj"); //valid
- type1.set(10); //valid and autoboxing support
- }
- }
- public interface MyInyerface<E> {
- void print(E e);
- }
- public class MyClass implements MyInyerface<String> {
- @Override
- public void print(String s) {
- }
- class MyClassInteger implements MyInyerface<Integer>{
- @Override
- public void print(Integer integer) {
- }
- }
- }
- ArrayList<String> stringList = new ArrayList<String>(); // список строк
- ArrayList<Integer> intList = new ArrayList<Integer>(); // список чисел
- stringList.add("123"); // OK
- stringList.add(123); // Ошибка!
- intList.add("123"); // Ошибка!
- intList.add(123); // OK
- class SomeClass < T > {
- ...
- private T value;
- public T getValue() { ... }
- }
- SomeClass< Integer > intInstance = new SomeClass< Integer >();
- SomeClass< ArrayList > listInstance = new SomeClass< ArrayList>();
- ....
- int i = intInstance.getValue(); // OK
- int j = listInstance.getValue(); // Error!!!
- ArrayList list = listInstance.getValue(); // OK
Add Comment
Please, Sign In to add comment