父类和实现接口中同名的静态和非静态方法 [英] Static and non static methods with the same name in parent class and implementing interface

查看:0
本文介绍了父类和实现接口中同名的静态和非静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是在问接口和抽象类之间的区别。

这是单打独斗的成功,对吗?

interface Inter {
    public void fun();
}

abstract class Am {
    public static void fun() {
        System.out.println("Abc");
    }
}

public class Ov extends Am implements Inter {
    public static void main(String[] args) {
        Am.fun();
    }
}

为什么会发生冲突?

推荐答案

Astatic和非static方法在相同的class中不能有相同的签名。这是因为您可以使用引用访问static和非static方法,而编译器将无法决定是要调用static方法还是非static方法。

以下面的代码为例:

Ov ov = new Ov();
ov.fun(); //compiler doesn't know whether to call the static or the non static fun method.

Java之所以允许使用引用调用static方法,是为了让开发人员可以无缝地将static方法更改为非static方法。

这篇关于父类和实现接口中同名的静态和非静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆