1、添加第一个插件ADT之后出现eclipse原有的SDK管理问题。需要重新安装SDK
2、一种方式按照系统提示直接联网自动搜索安装,另一种就是下载好之后import。
1、用接口声明的变量称为接口变量。实现把接口类的实例的引用赋值给接口变量后,接口变量就可以回调类重写的接口方法,由接口产生的多态就是指在不同的类实现同一个接口时可能据用不同的实现方法。那么接口变量再回调接口方法的时候就可能具有多态。
public class JiKouHuiDiao {
public static void main(String[] args) {
// TODO Auto-generated method stub
ShowMessage sm;
sm=new TV();
sm.showMessage("changCheng");
sm=new PC();
sm.showMessage("lianXiang");
}
}
interface ShowMessage{
void showMessage(String s);
}
class TV implements ShowMessage{
public void showMessage(String s){
System.out.println(s);
}
}
class PC implements ShowMessage{
public void showMessage(String s){
System.out.println(s);
}
}
2、接口和多态,其实就是类似于类的上转型
public class JieKouDuoTai {
public static void main(String[] args) {
// TODO Auto-generated method stub
ComputerAverage computer;
double a=11.23;
double b=23.23;
computer=new A();
double result=computer.average(a, b);
System.out.printf("%5.2f和%5.2f的算术平均数的值: %5.2f",a,b,result);
computer=new B();
result=computer.average(a, b);
System.out.printf("%5.2f和%5.2f的算术平均数的值: %5.2f",a,b,result);
}
}
interface ComputerAverage{
public double average(double a,double b);
}
class A implements ComputerAverage{
public double average(double a,double b){
double aver=0;
aver=(a+b)/2;
return aver;
}
}
class B implements ComputerAverage{
public double average(double a,double b){
double aver=0;
aver=Math.sqrt(a*b);
return aver;
}
}
3、如果一个方法的参数类型是接口 ,就可以将任何实现这个接口的实例的引用传递该接口参数。那么接口参数就可以回调类实现的接口方法。
public class JieKouCanShu {
public static void main(String[] args) {
// TODO Auto-generated method stub
KIndHello kindehello=new KIndHello();
kindehello.speakHello(new Chinese());
kindehello.speakHello(new English());
}
}
interface SpeakHello{
void speakHello();
}
class Chinese implements SpeakHello{
public void speakHello(){
System.out.println("中国人习惯说 你吃饭了吗");
}
}
class English implements SpeakHello{
public void speakHello(){
System.out.println("天气真好");
}
}
public class JieKouCanShu {
public static void main(String[] args) {
// TODO Auto-generated method stub
KIndHello kindehello=new KIndHello();
kindehello.speakHello(new Chinese());
kindehello.speakHello(new English());
}
}
interface SpeakHello{
void speakHello();
}
class Chinese implements SpeakHello{
public void speakHello(){
System.out.println("中国人习惯说 你吃饭了吗");
}
}
class English implements SpeakHello{
public void speakHello(){
System.out.println("天气真好");
}
}public class JieKouCanShu {
public static void main(String[] args) {
// TODO Auto-generated method stub
KIndHello kindehello=new KIndHello();
kindehello.speakHello(new Chinese());
kindehello.speakHello(new English());
}
}
interface SpeakHello{
void speakHello();
}
class Chinese implements SpeakHello{
public void speakHello(){
System.out.println("中国人习惯说 你吃饭了吗");
}
}
class English implements SpeakHello{
public void speakHello(){
System.out.println("天气真好");
}
}public class JieKouCanShu {
public static void main(String[] args) {
// TODO Auto-generated method stub
KIndHello kindehello=new KIndHello();
kindehello.speakHello(new Chinese());
kindehello.speakHello(new English());
}
}
interface SpeakHello{
void speakHello();
}
class Chinese implements SpeakHello{
public void speakHello(){
System.out.println("中国人习惯说 你吃饭了吗");
}
}
class English implements SpeakHello{
public void speakHello(){
System.out.println("天气真好");
}
}
4、接口回调
public class Example6_1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
AdvertisementBoard tian=new AdvertisementBoard();
tian.show(new WhiteCloud());
tian.show(new BlackLandCorp());
}
}
interface Advertisement{
void showMessage();
String getCorpName();
}
class AdvertisementBoard{
public void show(Advertisement adver){
System.out.println(adver.getCorpName()+"的广告词");
adver.showMessage();
}
}
class WhiteCloud implements Advertisement{
public void showMessage(){
System.out.println("##########");
System.out.println("飞机中的战斗机 @@");
System.out.println("#@@@@@@@@@@@@@@@@@");
}
public String getCorpName(){
return "白玉公司";
}
}
class BlackLandCorp implements Advertisement{
public void showMessage(){
System.out.println("##########");
System.out.println("劳动关荣 @@");
System.out.println("#@@@@@@@@@@@@@@@@@");
}
public String getCorpName(){
return "黑土集团";
}
}