本文介绍了是超载还是压倒一切?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码,但我不明白为什么输出将是长轮胎".有人可以帮助我理解此代码吗?

I have a code and I am not able to get why the output will be "Radial Tire with long".Can somebody help me to understand this code?

class Tyre {
    public void front() throws RuntimeException {
        System.out.println("Tire");
    }

    public void front(long a) {
        System.out.println("Radial Tire with long");
    }
}

class TestSolution extends Tyre {
    public void front() {
        System.out.println("Radial Tire");
    }
    public void front(int a) throws RuntimeException {
        System.out.println("Radial Tire with int");
    }


public static void main(String... args) {
        Tyre t = new TestSolution();
        int a = 10;
        t.front(a);

}

}

推荐答案

frontTestSolution中没有被覆盖,它已被重载.

front is not overridden in TestSolution, it is overloaded.

您可以将重载函数视为完全不同的函数,例如名称不同的函数.

You can regard an overloaded function as a completely different function, like one with a different name.

所以t.front(a)会调用Tyre中的那个,而a会隐式转换为long.

So t.front(a) will call the one in Tyre, with an a implicitly converted to long.

这篇关于是超载还是压倒一切?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 13:12