(一)
接口
package test;
public interface Fruit {
public void fruit();
}
实现接口的两个实现类
package test;
public class Apple implements Fruit {
@Override
public void fruit() {
// TODO Auto-generated method stub
System.out.println("苹果");
}
}
package test;
public class Banana implements Fruit {
@Override
public void fruit() {
// TODO Auto-generated method stub
System.out.println("香蕉");
}
}
测试类
package test;
public class Main {
Fruit fruit = null;
public Main(Fruit fruit) {
this.fruit = fruit;
}
public void fruit() {
fruit.fruit();
}
public static void main(String[] args) {
Fruit fruit = new Banana();
Main m = new Main(fruit);
m.fruit();
}
}
(二)
接口
package test;
public interface View {
public void clikButton();
}
实现接口的一个实现类
package test;
public class Action implements View{
Button button = null;
public void onStart(){
button = new Button();
button.clickButton(this);
}
public static void main(String[] args) {
Action a = new Action();
a.onStart();
}
// class viewTest implements View{
// @Override
// public void clikButton() {
// System.out.println("Action");
// }
// }
@Override
public void clikButton() {
// TODO Auto-generated method stub
System.out.println("Action");
}
}
package test;
public class Button {
public void clickButton(View view){
System.out.println("Button");
view.clikButton();
}
}