静态类不能被继承(其继承类不能被实例化,等于不能继承)-创新互联

开始听同事在那讨论,有人问静态类是否能被继承,我觉得是不能,但没有找到有说服力的例子,于是写了一段简单的代码来测试,如下:
public class staticTest {
static class aa{
void abc(){
System.out.println("1111111");
}
}
class bb extends aa{
void abc(){
System.out.println("222222");
}
}

创新新互联,凭借十多年的网站制作、网站建设经验,本着真心·诚心服务的企业理念服务于成都中小企业设计网站有成百上千案例。做网站建设,选创新互联。

public static void main(String[] args) {
//bb x = new bb();
aa y = new aa();
y.abc();
}
}
其中aa为静态类,bb为其继承类,类aa能像aa y = new aa();实例化,但类bb却会出错,eclipse中提示为No enclosing instance of type staticTest is accessible. Must qualify the allocation with an enclosing instance of type staticTest (e.g. x.new A() where x is an instance of staticTest).
经过多次试验,均不知道如何实例化类bb,故我的结论是类bb不能实例化。因为静态类aa的继承类bb无法实例化,类bb名存实亡,故认为静态类aa不能被继承。
(若谁知道如何实例化以上代码中的类bb,请回复代码,感谢!)

[@more@]
文章名称:静态类不能被继承(其继承类不能被实例化,等于不能继承)-创新互联
转载注明:http://lszwz.com/article/idsoi.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款