作者APE36 (PT乡民)
看板java
标题[问题] interface系统产生问题
时间Tue Jul 22 14:50:47 2014
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
麻烦版上大大的指导!!感谢
--
◢
◣ ▊ ▊ ▊ ▊ ◢◣ ◢◣ ▊ ▊ ▊███ ◣ ◣
◢█
L I N ◣ ▊ ▊ █◣ ▊◢ ◥◣ ▊ ▊ █◣ ▊ ▊ ▊ ▊ ▊
◥◤
▊ ▇▇ ◥◤ ▊ ▊ ▊◥◣▊◥ ▊ ▊▊◥◣▊ ▊ ▊ ▊ ▊
▊ ▊▎ ▊ ▊ ▊ ◥▊ ◥◣ ▊ ▊▊ ◥▊ ▊ ▊ ▉ ▉
▊ ▎ ▊ ▊ ▊ ▊ ◥◣█▆▆▊▊ ▊ ▊ ▊ ◥█
◤
ψ █▇▇ ▊ ▊ ▊◣▅▇◤▊ ▊▊ ▊ ▊ ▊ ▊
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 114.39.11.48
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/java/M.1406011850.A.FAD.html
1F:推 PttTime:Actions没有price member当然不能a.price 07/22 21:15
2F:推 PttTime:故a.name为Some Actions也不足为奇 07/22 21:20
3F:→ PttTime:interface里的变数会自动变成static变数 07/22 21:21