dongzituo5530 2015-01-13 09:31
浏览 23
已采纳

界面:php vs java [关闭]

I notice something very different between java and php on interface, when you create the same methods in their interfaces.

PHP:

interface Visitor 
{
    public function visit(Visitable $Visitable);
    public function visit(Visitable2 $Visitable2); // this is wrong in php.
}

Java:

interface Visitor 
{
    public double visit(Visitable Visitable);
    public double visit(Visitable2 Visitable2); // this is ok in java
}

you can look at the check it out on this video at 3:35 for java.

how come java allows that? what does double do anything? and no double in php?

  • 写回答

1条回答 默认 最新

  • donglu1913 2015-01-13 10:09
    关注

    Java actually allows overloading: this means that you can have a method with the same name but with two different signature.

    In the case of Java there are two methods with the same name visit in overloading:

    1. one accepts a param of type Visitable;
    2. the other Visitable2.

    You cannot do that in PHP because PHP doens't support overloading.

    Also please notice that double (the method return type) in that case does nothing. The return type alone cannot be used to achieve overloading as the compiler/interpreter has no way to know which implementation to bind in some circumstance.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部