import java.util.*;
public class Test
{
public static void main(String [] args)
{
List<Object> staff = new LinkedList<Object>();
staff.add("abc");
staff.add("efg");
staff.add("opq");
Iterator it = staff.iterator();
Object first = it.next();
Object second = it.next();
}
}
class A<T>
{
private T name;
}
This program does not make sense , is to practice grammar , why in the class can be used to indicate the type of T in the main why not use T instead of Object, not both said they did not limit the type of it?
------ Solution ---------------------------------------- ----
in a strongly typed language java T is referring to generics, generic meaning is beginning to know the type , like in the time do not need to use the cast operator then can while avoiding usage type of security and other issues .
For example, there is a bag, the bag I knew in advance must be put book , so I can define the class of such a package Bag
class Bag<T>{
public T getItem();
} then to declare an object schoolbag
Bag
So, when you use bag.getItem (), is directly accessible to the Book object.
------ Solution ---------------------------------------- ----
Java generics is the type erasure , both Java generics eventually became Object.
------ For reference only -------------------------------------- -
I'm not sure it should be , I'll express it,
The following two procedures
import java.util.*;
public class Test
{
public static void main(String [] args)
{
A<String> a = new A<String>();
}
}
class A<Object>
{
private Object name;
}
import java.util *.;
public class Test
{
public static void main (String [] args)
{
A
}
}
class A
{
private T name;
}
In class A, I Object and T can be compiled through . So I want to ask why the circumstances under which the process can be reversed, why I started the program in the main, are not interchangeable ?
------ For reference only -------------------------------------- -
still do not understand . Object is not to say that you can replace the T, but T is not a substitute Object?
没有评论:
发表评论