Java Core Technology Volume 1 Chapter 8 Partial Knowledge Summary

Because when I read the book, I didn't understand this part of the knowledge. Just checked online to find out. The book is a bit confusing. Still like the book of c++ primer = = I feel that it is written more clearly.

8.5 Generic code

Type erasure: The virtual machine does not have a generic object. When defining a generic type, it will automatically provide its corresponding original type, that is, erase the type variable. If it is a qualified type variable, use the first qualified type variable. Instead, if it is an unqualified type variable, use Object instead.

//Pair<T>的原始类型:
public class pair
{
        private Object first;
        private Object second;
        public pair()
        {first = null;second = null;}
        public pair(Object first,Object second)
        {this.first = first;this.second = second;}
        public Object getFirst()
        {return first;}
        public Object getSecond()
        {return second;}
        public void setFirst(Object newinput)
        {first = newinput;}
        public void setSecond(Object newinput)
        {second = newinput;}
}
//限定的泛型类的原始类型
public class Interval<T extends Comparable & Serializable> implements Serializable
{
    private T lower;

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

When calling a generic method, the compiler inserts a cast if the return type is erased

Pair<Employee> buddies = ...;
Employee = buddies.getFirst();//getFirst()首先生成一个object类,再强制转换为Employee类
  • 1
  • 2

 
Type elimination also occurs in  bridge methods
in generic methods such as

class DateInterval extends Pair<LocalDate>
{
    public void setSecond(LocalDate second)
    {
        if(second.compareTo(getFirst()) >= 0)
            super.setSecond(second);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

There is a problem with the above code. DateInterval is a subclass of Pair< LocalDate>, which has a method setSecond(). Generally speaking, the method of the subclass needs to override the method of the superclass. However, due to the elimination of the type, the subclass will start from The original type of the superclass inherits from another setSecond()

public void setSecond(LocalDate second)//child-class
public void setSecond(Object second)//继承
//当调用setSecond(aDate)时,多态性会出现问题
  • 1
  • 2
  • 3

At this time, the method of the subclass cannot override the method of the superclass, thus affecting the polymorphism. The solution is that the compiler generates a bridge method in the subclass, which calls setSecond in the subclass

public void setSecond(Object second)
{setSecond((LocalDate) second);}
  • 1
  • 2

Put another way

class DateInterval extends Pair<LocalDate>
{
    public LocalDate getSecond()
    {return (Date) super.getSecond().clone();}
}
  • 1
  • 2
  • 3
  • 4
  • 5

At this point the compiler will also generate a bridge method

public Object getSecond()
{...}
  • 1
  • 2

There are two methods with no parameters and the same name in the subclass. 
This is not allowed in writing, because they have no parameters and only have different return types. In the compiler, the parameter type and return type are used to determine a method. method, so the compiler can parameterize the bytecode of two methods that differ only in return type, but such writing will not compile.

8.6 Constraints

1.不能用基本类型实例化类型参数,即不能有Pair< double>,因为类型擦去后Pair的类型为Object,不能将double赋给它,只能有Pair< Double> 
2.对于类型查询(instanceof),只产生原始类型。getClass()也是返回原始类型

if(a instanceof Pair<String>){...}//实际上只能查询a是否属于Pair
  • 1

3.不能创建参数化类型的数组,即new Pair< String>[10]是不允许的,Pair< String>[] 不能初始化 
只能使用ArrayList< Pair< String>>来搜集参数化类型 
4.由于不能创建参数化类型的数组,所以在提供一系列参数化类型作为参数的方法中,调用用时会发出警告,可用@SafeVarags来消除创建泛型数组的有关限制

@SafeVarags
public static <T> void addAll(collection<T> coll,T...st)//一系列T对象会被写为数组
  • 1
  • 2

5.不能实例化类型变量,即new T(..)/new T[…]/T.class等 
6.不能构造泛型数组,一般需要强制类型转换,即[T[]] new Object[size]; 
7.不能在静态域和静态方法中使用类型变量

8.7 继承

如果S为T的超类,Pair< S>和Pair< T>没有任何关系

8.8 通配符类型

通配符可以分为子类型限定通配符和超类型限定通配符 
子类型限定通配符: ? extends Employee 不能向更改器方法传递特定的类型,但是可以让访问器方法返回特定类型 
超类型限定通配符:? super Manager,不能像向改器传递Manager的超类,访问器方法只能返回Object类

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325345373&siteId=291194637