Skip to content

Instantly share code, notes, and snippets.

@SZooo
Created October 3, 2014 05:42
Show Gist options
  • Save SZooo/8c7c8111f2d2e5dc5738 to your computer and use it in GitHub Desktop.
Save SZooo/8c7c8111f2d2e5dc5738 to your computer and use it in GitHub Desktop.
向上转型实例
interface monitor{
public void displayText();
public void displayGraphics();
} //定义监视器接口
-----------------------------------------------------
class LCD implements monitor{
public void displayText(){
System.out.println("this is the lcd screen");
}
public void displayGraphics(){
System.out.println("nice");
}
} //实现接口
-----------------------------------------------------
class IPS implements monitor{
public void displayText(){
System.out.println("this is the ips screen");
}
public void displayGraphics(){
System.out.println("great");
}
}
------------------------------------------------------
public class myMonitor{
public static void main(String[] args){
run(new LCD);
run(new IPS);
}
public static void run(monitor m){
m.displayText();
m.displayGraphics();
} //这里的monitor并不是实例化,而是生成一个引用,在语法上是可以的,而后在参数里生成一个子接口以达到向上转型
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment