匿名类问题
interface Machine{void run();
}
class Person{
void run(){
System.out.println("run");
}
}
class Robot extends Person{
Machine getMachine(){
return new Machine(){
void run(){
System.out.println("herat run");
}
}; //不知道匿名类是不是这样写的
}
}
class Text
{
public static void main(String [] args){
Robot robot=new Robot();
Machine m=robot.getMachine();
m.run();
robot.run();
}
}
这错哪了啊
搜索更多相关的解决方案:
匿名
----------------解决方案--------------------------------------------------------
以下是引用心动音符在2006-11-17 22:22:54的发言:
interface Machine{
void run();
}
class Person{
void run(){
System.out.println("run");
}
}
class Robot extends Person{
Machine getMachine(){
return new Machine(){
void run(){ //我这里改成public void run()就没问题了,但为什么呢 我没有降低它的权限啊 原来是default现在还是default啊
System.out.println("herat run");
}
}; //不知道匿名类是不是这样写的
}
}
class Text
{
public static void main(String [] args){
Robot robot=new Robot();
Machine m=robot.getMachine();
m.run();
robot.run();
}
}
这错哪了啊
interface Machine{
void run();
}
class Person{
void run(){
System.out.println("run");
}
}
class Robot extends Person{
Machine getMachine(){
return new Machine(){
void run(){ //我这里改成public void run()就没问题了,但为什么呢 我没有降低它的权限啊 原来是default现在还是default啊
System.out.println("herat run");
}
}; //不知道匿名类是不是这样写的
}
}
class Text
{
public static void main(String [] args){
Robot robot=new Robot();
Machine m=robot.getMachine();
m.run();
robot.run();
}
}
这错哪了啊
----------------解决方案--------------------------------------------------------
哦 接口默认是 public final static 我忘记了不好意思
----------------解决方案--------------------------------------------------------
晕倒 我自问自答 有意思!
----------------解决方案--------------------------------------------------------