案例一:
Usb:
package Interface1;
public interface Usb {
public abstract void work();
}
keymouse:
package Interface1;
public class keymouse implements Usb {
@Override
public void work() {
System.out.println("鼠标开始工作了!你可以移动鼠标");
}
}
Usbkeyboard :
package Interface1;
public class Usbkeyboard implements Usb {
@Override
public void work() {
System.out.println("键盘开始工作了!你可以输入了");
}
}
Test:
package Interface1;
import org.junit.Test;
public class jTest {
@Test
public void test(){
/*Usbkeyboard key=new Usbkeyboard();
key.work();
keymouse mouse=new keymouse();
mouse.work();*/
//或者
Usb mouse=new keymouse();//通过接口的实现类来实例化接口
mouse.work();
Usb key=new Usbkeyboard();//通过接口的实现类来实例化接口
key.work();
}
}
案例二:
Person:
package Interface2;
public abstract class Person {
private String name;
private String sex;
private int age;
public Person(String name, String sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public abstract void show();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Hearable:
package Interface2;
public interface Hearable {
public abstract void hear();
}
Eatable:
package Interface2;
public interface Eatable {
public abstract void eat();
}
Sayable:
package Interface2;
public interface Sayable {
public abstract void say();
}
Dumb:
package Interface2;
public class Dumb extends Person implements Sayable, Eatable {
public Dumb(String name, String sex, int age) {
super(name, sex, age);
}
@Override
public void say() {
System.out.println(super.getName()+" "+super.getSex()+" "+super.getAge()+"say say say");
}
@Override
public void eat() {
System.out.println(super.getName()+" "+super.getSex()+" "+super.getAge()+"eat eat eat");
}
@Override
public void show() {
System.out.println(super.getName()+" "+super.getSex()+" "+super.getAge()+"显示1");
eat();
say();
}
}
Deaf :
package Interface2;
public class Deaf extends Person implements Hearable, Eatable {
public Deaf(String name, String sex, int age) {
super(name, sex, age);
}
@Override
public void hear() {
System.out.println(super.getName()+" "+super.getSex()+" "+super.getAge()+"hear hear hear");
}
@Override
public void eat() {
System.out.println(super.getName()+" "+super.getSex()+" "+super.getAge()+"the deaf can eat");
}
@Override
public void show() {
System.out.println(super.getName()+" "+super.getSex()+" "+super.getAge()+"显示1");
eat();
hear();
}
}
Test:
package Interface2;
import org.junit.Test;
public class Jtest {
@Test
public void test(){
//Person d=new Dumb("fdsa","fsda",56);
//Dumb dumb=new Dumb("张三", "男", 25);
//Deaf deaf=new Deaf("李四", "男", 45);
//dumb.show();
//dumb.eat();
//dumb.say();
//System.out.println("**************");
//deaf.show();
//deaf.eat();
//deaf.hear();
Person dumb=new Dumb("张三", "男", 25); //多态性 向上转型
Person deaf=new Deaf("李四", "男", 45);
dumb.show();
System.out.println("**************");
deaf.show();
}
}