第03章_面向对象_43_interface_接口
public interface 接口名 {
int id 等价于 ( public static final int id = 1)
public void 方法名(){
}
}
使用implements 实现接口
public class Test20180429{
public static void main(String args[]){
Singe s = new Student("哈"); //创建唱歌接口引用 s 指向 学生对象
Run d = new Student("呵"); //创建跑接口引用 d 实现 学生对象
s.sing(); //调用s的唱歌方法
d.run1(); //调用d的跑方法
}
}
interface Singe{ //定义唱歌接口类
public void sing (); //唱歌方法
}
interface Run{ //定义跑接口类
public void run1 (); //跑方法
}
class Student implements Singe,Run{ //定义学生类 同时实现 唱歌 跑 两个接口类
String name;
Student(String name){
this.name = name;
}
public void sing (){ //实现 唱歌方法
System.out.println("学生唱歌");
}
public void run1 (){
System.out.println("学生跑"); //实现 跑方法
}
}