Java Casting Interface to Class

后端 未结 8 1371
闹比i
闹比i 2020-12-05 08:03
public class InterfaceCasting {

    private static class A{}

    public static void main(String[] args) {
        A a = new A();
        Serializable serializable          


        
8条回答
  •  广开言路
    2020-12-05 08:35

    Serializable serializable;
    a = (A)serializable;
    

    As for the compiler, the variable serializable can contain any object that implements Serializable, which includes subclasses of A. So it assumes that you know that the variables indeed contains an A object and allows that line.

提交回复
热议问题