Created
October 3, 2014 05:42
-
-
Save SZooo/8c7c8111f2d2e5dc5738 to your computer and use it in GitHub Desktop.
向上转型实例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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