一、接口的目的
接口的目的是规范代码,在写代码之前,先通过接口规定要写哪些方法
二、定义接口
package com.guoyasoft.implement;
public interface IStudy {
public boolean doHomeWork(String renwu);
public int learn(String course);
}
实现接口
public class StudentCY extends Student implements IStudy{
@Override
public boolean doHomeWork(String renwu) {
System.out.println("开始做作业");
System.out.println("开始看手机");
System.out.println("开始玩游戏");
System.out.println("不做了");
return false;
}
@Override
public int learn(String course) {
System.out.println("不想学");
return 0;
}
}
public class StudentWL extends Student implements IStudy{
@Override
public boolean doHomeWork(String renwu) {
System.out.println("开始做作业");
System.out.println("开始看手机");
System.out.println("开始玩游戏");
System.out.println("不做了");
return false;
}
@Override
public int learn(String course) {
System.out.println("不想学");
return 0;
}
}
3. 测试
package com.guoyasoft.implement;
public class TestStudent {
public static void main(String[] args) {
StudentWL wl3=new StudentWL();
wl3.doHomeWork("家庭作业");
wl3.learn("法语");
//向上转型成接口类型
IStudy wl4=new StudentWL();
wl4.doHomeWork("家庭作业");
wl4.learn("法语");
}
}