dongzituo5530 2015-01-13 17: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 18: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.

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

报告相同问题?

悬赏问题

  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动