接口提问

yangyanwm 2005-12-28 01:23:21
interface Act{
void act();
}
class Actor1 implements Act{
public void act(){
System.out.println("To be,or not to be");
}
}
class Actor2 implements Act{
public void act(){
System.out.println("Wherefore art thou Romeo?");
}
}
public class TryOut{
public static void main(String []args){
Actor1 hamlet=new Actor1();
Actor2 juliet=new Actor2();
tryout(hamlet);
tryout(juliet);
}
private static void tryout(Act actor){
actor.act();
}
}
Actor1,Actor2不是实现接口的方法了吗?语句
tryout(hamlet);
tryout(juliet);
private static void tryout(Act actor)
actor.act();不要了不行吗?


...全文
191 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
jara 2006-05-19
  • 打赏
  • 举报
回复
1楼是正解
schol 2006-05-19
  • 打赏
  • 举报
回复
funcreal(为中华之崛起而编程) ( 一星(中级)) 信誉:100
#1才是正解

yuzl32(Hello!有酒醉)这样做有什么意义。根本体现不了接口的特点。
数据娃掘 2006-05-19
  • 打赏
  • 举报
回复
yuzl32(Hello!有酒醉)说得是正解
ccnu425 2006-05-17
  • 打赏
  • 举报
回复
这里就是体现了接口与实现了接口的类之间的关系。例子很短小,但很经典,顶一下。
zorro09 2006-02-27
  • 打赏
  • 举报
回复
。。。。yuzl32(Hello!有酒醉) 有何意义?
linanbox21 2006-02-26
  • 打赏
  • 举报
回复
同意搂上的,不过这个例子是有点别扭定义了一个私有的静态方法就是为了调用hamlet.act()和juliet.act(),不过,我猜想可能还是有别的原因的,顶上去,希望高手能给个准确的答复
yuzl32 2005-12-28
  • 打赏
  • 举报
回复
不要也可以,改成下面:
public class TryOut{
public static void main(String []args){
Actor1 hamlet=new Actor1();
Actor2 juliet=new Actor2();

hamlet.act();
juliet.act();
}
}
funcreal 2005-12-28
  • 打赏
  • 举报
回复
当然不可以,你这两个类根本没有关系。

直接调用
Actor1 hamlet=new Actor1(); hamlet.act();
Actor2 juliet=new Actor2(); juliet.act();
这样当然可以。

但是我觉得这个例子是为了说明hamlet和juliet都是Act类型。

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧