快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

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

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

其他资讯