• 静态类型检查与继承


    类型检查(type checking)是指确认任一表达式的类型并保证各种语句符合类型的限制规则的过程。

    Java是静态类型检查的语言,可是仍然须要执行期类型检查,并抛出可能的执行时异常。

    Wiki:
    Static type-checking is the process of verifying the type safety of a program based on analysis of a program's source code.
    Dynamic type-checking is the process of verifying the type safety of a program at runtime

    package typeSystem;
    import static tips.Print.*;//pln()
    
    class Sup{
        Sup me() { return this;} 
        public void inSup() {pln("Sup");}
    }
    class Sub extends Sup {
        public void inSub() {pln("Sub");}
    }
    ①"new Sub().me()"返回的是什么?

    A: 返回的对象实际类型Sub。声明类型Sup。编译器仅仅知道me()在Sup中返回Sup,执行时子类继承me()并返回this。

    ② new Sub().me().inSub(); 为什么编译错误?

    A:“子类扩展的丧失”。编译器仅仅知道new Sub().me()为Sup类型,而Sup中没有子类特定的方法。

    new Sub().me() 如今仅仅可以调用.inSup()。

    ((Sub)new Sub().me()).inSub(); //能够向下造型

    ③如果Sub2 extends Sup,有方法 inSub2。

    ((Sub2)new Sub().me()).inSub2(); 会如何?

    A:编译合法,可是执行时抛出 java.lang.ClassCastException: typeSystem.Sub cannot be cast to typeSystem.Sub2



  • 相关阅读:
    pytest
    pytest 跳过测试用例 skip
    pytest
    pytest -allure标记用例级别severity
    pytest-
    pytest-分布式执行(pytest-xdist)
    4、win10搭建easy-mock平台
    pandas日期缺失,空值填充方法
    Pandas拼接操作(concat,merge,join和append)的区别
    关于docker容器和镜像的区别
  • 原文地址:https://www.cnblogs.com/cxchanpin/p/6899292.html
Copyright © 2020-2023  润新知