package my;
public class StudentTest {
public static void main(String[] args) {
//接口或抽象类指向其子对象,只能调用自己已有的方法;
Student p=new Student("谷谷");
Swimmer s=new Student("风清");//接口
Singer sg=new Student("古昔");//接口
Person r=new Student("蛮风");//抽象类
p.swime();p.rest();p.sing();p.work();
s.swime();s.rest();
sg.sing();
r.sing();r.work();
//接口->(抽象类->对象)
Swimmer test1=s;
s.swime();
s.rest();
/*接下来的举例都是不可用的,重在思考问题的方式,多重角度,很多时候,你不试试是不知道正确答案的*/
//接口->抽象类
//Person e=new Person();//Error,抽象类不可实例化
//抽象类->接口
//Singer t=new Singer();//error,事实上,有了抽象类的概念后才有接口的出现,所以自然接口也是不能实例化;
//子类->接口
//Student r=new Singer();//error,借口不可实例化
//子类->抽象类
//Student y=new Person();
}
}
interface Swimmer
{
public void swime();
public void rest();
}
interface Singer
{
public void sing();
}
abstract class Person implements Singer
{
private String name;
Person(String name)
{
this.name = name;
}
public abstract void sing();
public void work()
{
System.out.println(this.name + " is working");
}
}
class Student extends Person implements Swimmer{
Student(String name){
super(name);
}
public void sing() {
System.out.println("song of the person!");
}
public void swime() {
System.out.println("the person is swimming");
}
public void rest() {
System.out.println("the person is sleeping");
}
}