package traffic;
public class traffictool {
public void go()
{
System.out.println(); ///子类将重写之,不能省去
}
///public abstract void go();
}
/**********************抽象类
package traffic;
abstract public class traffictool
{
public abstract void go();
}
*************************/
package traffic;
public class bike extends traffictool {
public void go()
{
System.out.println("骑自行车去火车站");
}
}
package traffic;
public class bus extends traffictool{
public void go()
{
System.out.println("坐公交车去火车站");
}
}
package traffic;
//接口文件
public interface enter //只能写抽象方法
{
public void go();///在接口中默认为抽象方法,不需要加abstract
}
package traffic;
public class highway extends traffictool {
public void go()
{
System.out.println("坐高铁去火车站");
}
}
package traffic;
public class taxi implements enter //"必须"要"重写"接口中"所有"的抽象方法
{
public void go()
{
System.out.println("打出租去火车站");
}
}
package traffic;
public class test2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
traffictool tt =new bus();
tt.go();
//用接口实现多态
enter e1=new taxi();
e1.go();
}
}
Java 多态,接口
最新推荐文章于 2022-11-05 00:21:18 发布