Java 學習記錄53 — Our Generics Class. Player.java
Tags: Coding, Programming, Java, Learning, Taiwan

Kevin 回答學生問的

Q：what is T in team<T>

A：

The T between angular brackets is a generic type. It could have been any lowercase or uppercase letter, any word that is not a reserved keyword, or even a string of characters in another writing system like 程序员! However, it’s standard practice to use an uppercase letter and we’ll stick with T for this example.

class MyGenericClass<T> {

private T id;



public MyGenericClass(T id) {

this.id = id;

}



public T getId() {

return id;

}



public void setId(T id) {

this.id = id;

}

}

T is generic because we have not specified the data or reference type of the class field, constructor parameter, method parameter or return type. In one instance of MyGenericClass, you might want T to be String. In another instance, you might want it to be Integer. Generics give us that flexibility.

MyGenericClass<String> myFavoriteGenericClass = new MyGenericClass<>("lucky");

myFavoriteGenericClass.setId("happy");

System.out.println(myFavoriteGenericClass.getId());



MyGenericClass<Integer> myLeastFavoriteGenericClass = new MyGenericClass<>(13);

myLeastFavoriteGenericClass.setId(4);

System.out.println(myLeastFavoriteGenericClass.getId());

Now, we have two distinct instances of the same class. myFavoriteGenericClass’s id is a String, its setId method takes a String parameter, and its getId method returns a String. By contrast, myLeastFavoriteGenericClass’s id is an Integer, its setId method takes a Integer parameter, and its getId method returns an Integer. Without generics, we would have needed to create two different classes. Generics are commonly used in collections such as ArrayLists.

Q：DIfference between Generics and Polymorphism

A：

generics and polymorphism are actually quite different. Generics allow for instances of a class to have functionality and structure in common while having fields and method return types of different types. The most common use is in collections. Thanks to generics, you can have an ArrayList of Longs, an ArrayList of Strings and an ArrayList of Floats without the need for more than one class. On the other hand, polymorphism involves an object’s ability to behave like an instance of its own class in some situations and like an instance of its parent class at other times. Unlike generics, these class’s variables have the same type and their methods have the same return type as well.

public class Main {

static class ParentClass {

private void myPrivateMethod() {

System.out.println("Parents private method.");

} void myPolymorphicMethod() {

System.out.println("Calling parent class's method.");

}

} static class ChildClass extends ParentClass {

void myPolymorphicMethod() {

System.out.println("Calling child class's method.");

}

} public static void main(String[] args) { ParentClass eldest = new ParentClass();

eldest.myPolymorphicMethod();

eldest.myPrivateMethod();

ChildClass youngest = new ChildClass();

youngest.myPolymorphicMethod();

ParentClass middle = new ChildClass();

middle.myPolymorphicMethod();

middle.myPrivateMethod();

}

} //輸出結果：

//

// Calling parent class's method.

//

// Parents private method.

//

// Calling child class's method.

//

// Calling child class's method.

//

// Parents private method.