接口
1作为参数
interface Showmessage{
public abstract void show();
}
class Student implements Showmessage{
public void show(){
System.out.println("我爱学习");
}
}
class Teacher implements Showmessage{
public void show(){
System.out.println("教学");
}
}
class demo{
public void method(Showmessage p){
p.show();
}
}
//
public class Test{
public static void main(String[] args) {
Student stu=new Student();
Teacher t=new Teacher();
demo de=new demo();
de.method(stu);
de.method(t);
}
}
如上,接口作为参数时候,实参必须是实现接口的类的对象
2接口作为多态,例如:Showmessage show=new Student();
3接口做返回值,还是用实现接口的类做返回值
例如:
**
class t{
public void tt(){
//Showmessage sh=new Student();
//return sh;
//相当于
return new Student();
}