静态类不能被继承(其继承类不能被实例化,等于不能继承)

发布时间:2020-08-19 08:35:14 作者:guoyj520
来源:ITPUB博客 阅读:333

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

推荐阅读:
  1. Kotlin类与继承
  2. 继承抽象类

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

不能 实例 等于

上一篇:Java注解详解

下一篇:Service Mesh 初体验

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》