[问题] interface系统产生问题

楼主: APE36 (PT乡民)   2014-07-22 14:50:47
public class Interface1{
public static void main(String[] args) {
Bird b = new Bird();
b.canFly();
System.out.println(b.name);
/***从这里开始以下四行,不知道为何能跑出Answer的结果
Actions a = new Airplane();
a.canFly();
System.out.println(a.name); //资料成员用的是Actions中的name
//System.out.println(a.price); //这行会产生错误,不能用Airplane中定义的成员
} ***/
}
interface Actions { //接口的宣告
public String name = "Some Actions";
public void canFly(); //定义方法
public void canRun(); //定义方法
}
class Bird implements Actions{ //Bird类别实作接口
public String name = "Bird";
public void canFly(){
System.out.println("Bird Flying...");
}
public void canRun() {} //这样也算实作了
}
class Airplane implements Actions{ //Airplane类别实作接口
public String name = "Airplane";
public int price =100;
public void canFly(){ //一定要实作接口中的方法
System.out.println("Airplane Flying...");
}
public void canRun() {}; //这样也算实作了
}
ANSWER:
Bird Flying...
Bird
Airplane Flying...
Some Actions
麻烦版上大大的指导!!感谢
作者: PttTime   2014-07-22 21:15:00
Actions没有price member当然不能a.price故a.name为Some Actions也不足为奇interface里的变量会自动变成static变量

Links booklink

Contact Us: admin [ a t ] ucptt.com