* 非静态方法属于类的实例,是可以被子类重写,从而达到多态的效果;
静态方法属于类,是不能被重写,故而也不能实现多态。 *
下面是具体的验证过程
首先,定义一个超类A,里边定义一个静态方法和一个非静态方法:
publicclassA{
publicvoidunstaticMethod(){
System.out.println("SuperClassunstaticMethod");
}
publicstaticvoidstaticMethod(){
System.out.println("SuperClassstaticMethod");
}
}
接下来,定义一个子类B,里边定义一个静态方法和一个非静态方法(形式上像是重写了超类的方法):
public class B extends A {
public void unstaticMethod() {
System.out.println("SunClass unstaticMethod");
}
public static void staticMethod() {
System.out.println("SunClass staticMethod");
}
}
在接下来,我们进行测试:
public class Test {
@SuppressWarnings("static-access")
public static void main(String[] args) {
A a = new A();
A a2 = new B();
a.unstaticMethod();
a2.unstaticMethod();
a.staticMethod();
a2.staticMethod();
}
}
运行结果:
SuperClass unstaticMethod
SunClass unstaticMethod
SuperClass staticMethod
SuperClass staticMethod
从运行结果,我们可以知道,对于非静态方法,实现了多态的效果,而静态方法却没有。
Java中的静态方法能否被重写?
发表于:2017-08-08
作者:王潘
来源:
 相关文章
前端到底是“技术深度”重要还是“技... 悟透一个小窍门,你就能举一反三掌握... 聊聊架构设计流程:设计备选方案 Java中的并发编程模型及其应对策略 掌握Java并发编程,避免无处不在的竞态条件 2024年,五个Java开发者应该关注的编程趋势- 周排行
- 月排行
-   一个牛逼的创业公司后台技术栈搭建方案
-   13个Python web框架比较
-   性能优化:量变引起质变的挑战
-   30道针对TypeScript面试的必须掌握的面试题
-   Python 模块 asyncio-异步IO,事件循环和并发
-   还有什么是Python不能干的?
-   Web开发与设计常用的17种Chrome扩展包
-   一个牛逼的创业公司后台技术栈搭建方案
-   我们一起聊聊如何提高API性能的综合策略
-   程序员开发利器?在线工具集锦
-   面试必备:如何将一个长URL转换为一个...
-   程序员如何成为代码调试高手?教你三...
-   业务开发做到零 bug 有多难?
-   性能优化:量变引起质变的挑战