我自定义了Pair、Manager类,然后发现下面的代码居然可以运行:
- class Pair<T>
- {
- private T first;
- private T second;
- public Pair(T first, T second)
- {
- this.first=first;
- this.second=second;
- }
- public T getFirst()
- {
- return first;
- }
- public T getSecond()
- {
- return second;
- }
- public void setSecond(T second)
- {
- this.second=second;
- }
- public void setFirst(T first)
- {
- this.first=first;
- }
- public static void main(String[] args)
- {
- Manager ceo=new Manager("zhangsan");
- Manager cfo=new Manager("lisi");
- Pair<Manager> manager=new Pair<Manager>(ceo,cfo);
- Pair pr=manager;
- pr.setFirst("错误实例");
- }
- }
复制代码
这样操作会使得Pair<Manager>的实例里包含一个非Manager的实例,但是运行是没问题的?
|
|