java 函数覆盖,Java中的函数覆盖 - 重载

What is the difference between override and overload?

解决方案

Overloading: picking a method signature at compile time based on the number and type of the arguments specified

Overriding: picking a method implementation at execution time based on the actual type of the target object (as opposed to the compile-time type of the expression)

For example:

class Base

{

void foo(int x)

{

System.out.println("Base.foo(int)");

}

void foo(double d)

{

System.out.println("Base.foo(double)");

}

}

class Child extends Base

{

@Override void foo (int x)

{

System.out.println("Child.foo(int)");

}

}

...

Base b = new Child();

b.foo(10); // Prints Child.foo(int)

b.foo(5.0); // Prints Base.foo(double)

Both calls are examples of overloading. There are two methods called foo, and the compiler determines which signature to call.

The first call is an example of overriding. The compiler picks the signature "foo(int)" but then at execution time, the type of the target object determines that the implementation to use should be the one in Child.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值