案例1.接口之打印机

/**
* 纸张接口
* @author DELL
*
*/
public interface Paper {
/**
* 得到纸张大小
*/
public String GetSize();
}
/**
* 墨盒接口
* @author DELL
*
*/
public interface InkBox {
/**
* 得到纸张大小
*/
public String GetColor();
}
public class ColorInkBox implements InkBox {
public String GetColor(){
return "彩色";
}
}
public class GrayInkBox implements InkBox {
public String GetColor(){
return "黑白";
}
}
public class A4 implements Paper {
public String GetSize(){
return "A4";
}
}
public class A5 implements Paper {
public String GetSize(){
return "A5";
}
}
/**
* 打印机类
* @author DELL
*
*/
public class Printer {
/**
* 使用墨盒在纸张上打印
*/
public void print(InkBox inkBox,Paper paper){
System.out.println("使用"+inkBox.GetColor()+"墨盒在"+paper.GetSize()+"纸张上打印。");
}
public class Text {
/**
* @param args
*/
public static void main(String[]) {
// TODO Auto-generated method stub

//定义打印机
InkBox i = null;
Paper p = null;
Printer pt = new Printer();

//使用黑白墨盒在A4纸上打印
i = new GrayInkBox();
p = new A4();
pt.print(i, p);

//使用彩色墨盒在A5纸上打印
i = new ColorInkBox();
p = new A5();
pt.print(i, p);
}
}
结果:
使用黑白墨盒在A4纸张上打印。
使用彩色墨盒在A5纸张上打印。

案例2. 接口之写信

/**
* 家书接口
*/
public interface HomeLetter {

/**
* 书写称谓
*/
public void writetitle();
/**
* 书写问候
*/
public void writehello();
/**
* 书写内容
*/
public void writebody();
/**
* 书写祝福
*/
public void writegreeting();
/**
* 书写落款
*/
public void writeself();
}
/**
* 接口实现类
*
* @author DELL
*
*/

public class HomeLetterImp implements HomeLetter {

@Override
public void writetitle() {
// TODO Auto-generated method stub
System.out.println("亲爱的爸爸妈妈:");

}

@Override
public void writehello() {
// TODO Auto-generated method stub
System.out.println("\t你们好吗?");

}

@Override
public void writebody() {
// TODO Auto-generated method stub
System.out.println("\t我在这里挺好的。\n\t我会努力学习的,已经学到java OOP啦!\n\t您二老保重身体啊!");
}

@Override
public void writegreeting() {
// TODO Auto-generated method stub
System.out.println("\n\t此致\n敬礼");
}

@Override
public void writeself() {
// TODO Auto-generated method stub
System.out.println("\t\t\t周杰\n\t\t\t2010.06.01");
}

}
public class HomeLetterWriter {
/**
* 按照约定格式书写家书
*/
public static void write(HomeLetter letter){
letter.writetitle();
letter.writehello();
letter.writebody();
letter.writegreeting();
letter.writeself();
}
}
public class Test {
/**
* @param args
*/
public static void main(String[]) {
// TODO Auto-generated method stub

//创建家书对象
HomeLetter letter = new HomeLetterImp();
//书写家书
HomeLetterWriter.write(letter);
}
}

结果:

亲爱的爸爸妈妈:
你们好吗?
我在这里挺好的。
我会努力学习的,已经学到java OOP啦!
您二老保重身体啊!

此致
敬礼
周杰
2010.06.01

案例3.接口之应聘

public interface Person {

public String getName();
}
public interface BigAgent extends Person {

//具备讲解业务能力
void Speaking();

}
public interface Programmer extends Person {


//具备编码能力
void bianCode();

}
public  class SoftEngineer implements Programmer,BigAgent {

private String name ;
public SoftEngineer(String name) {
// TODO Auto-generated method stub
this.name = name;
}

@Override
public void Speaking() {
// TODO Auto-generated method stub
System.out.println("我会讲业务。");

}

@Override
public void bianCode() {
// TODO Auto-generated method stub
System.out.println("我会写代码。");
}

public String getName(){
return name;
}
}
public class Test {
/**
* @param args
*/
public static void main(String[]) {
// TODO Auto-generated method stub

SoftEngineer xiaoming = new SoftEngineer("小明");
System.out.println("我是一名软件工程师,名字叫"+xiaoming.getName()+"。");

xiaoming.bianCode();
xiaoming.Speaking();
}
}