静态类不能被继承(其继承类不能被实例化,等于不能继承)-创新互联-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

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

开始听同事在那讨论,有人问静态类是否能被继承,我觉得是不能,但没有找到有说服力的例子,于是写了一段简单的代码来测试,如下:
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://kswsj.cn/article/idsoi.html

其他资讯