关于接口和接口中多肽问题的实例

/**
 * 关于接口和接口中多肽问题的实例
 * 
 * @author iflytek
 * 
 */
interface Singer {
	public void sing();

	public void sleep();
}

interface Painter {
	public void paint();

	public void eat();
}

class Student implements Singer {
	private String name;

	Student(String name) {
		this.name = name;
	}

	public void study() {
		System.out.println("studying......");
	}

	@Override
	public void sing() {
		System.out.println("student sing......");

	}

	@Override
	public void sleep() {
		System.out.println("student sleep......");

	}
}

class Teacher implements Singer, Painter {
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	Teacher(String name) {
		this.name = name;
	}

	public void teach() {
		System.out.println("teacher teach......");
	}

	@Override
	public void paint() {
		System.out.println("teacher paint......");

	}

	@Override
	public void eat() {
		System.out.println("teacher eat......");

	}

	@Override
	public void sing() {
		System.out.println("teacher sing......");

	}

	@Override
	public void sleep() {
		System.out.println("teacher sleep......");

	}

}

public class TestSinger {
	public static void main(String[] args) {
		Singer s1 = new Student("le");
		s1.sing();
		Singer s2 = new Teacher("steven");
		s2.sing();
		s2.sleep();
		Painter p1 = (Painter) s2;
		p1.paint();
		p1.eat();
		System.out.println((Painter) s2 instanceof Teacher);
		System.out.println(s2 instanceof Teacher);
		System.out.println(p1 instanceof Teacher);
	}
}

你可能感兴趣的:(接口)