自学内容网 自学内容网

java:接口,抽象,多态的综合小练习

package 综合抽象接口练习;

public class person {
    protected String name;
    protected int age;

    person(){}

    person(String name,int age){
        this.name=name;
        this.age=age;
    }

    public void setName(String name)
    {
        this.name=name;
    }

    public String getName()
    {
        return name;
    }

    public void setAge(int age)
    {
        this.age=age;
    }

    public int getAge()
    {
        return age;
    }
}
package 综合抽象接口练习;

public abstract class coach extends person{
    public coach(){};
    public coach(String name,int age)
    {
        super(name, age);
    }

    public abstract void teach();
}
package 综合抽象接口练习;

public abstract class  player  extends person {
    public player(){};

    public player(String name,int age){
        super(name, age);
    }

    public abstract void study();
}

 接口:

package 综合抽象接口练习;

public interface speaking_English {
    public abstract void speakEnglish();
}
package 综合抽象接口练习;

public class 乒乓player extends player implements speaking_English {
    public 乒乓player(){};

    public 乒乓player(String name,int age){
        super(name, age);
    }

    @Override
    public void speakEnglish(){
        System.out.println("乒乓球运动员在说英语");
    }

    @Override
    public void study(){
        System.out.println("乒乓球运动员会打篮球哦");
    }

}
package 综合抽象接口练习;

public abstract class 乒乓球coach extends coach implements speaking_English{
    public 乒乓球coach(){};

    public 乒乓球coach(String name,int age){
        super(name, age);
    }

    @Override
    public void teach()
    {
        System.out.println("教练在教我打乒乓球");
    }

    @Override
    public void speakEnglish()
    {
        System.out.println("乒乓球教练在学习说英语");
    }

}
package 综合抽象接口练习;

public abstract class 篮球coach extends coach{
    public 篮球coach(){};

    public 篮球coach(String name,int age){
        super(name, age);
    }

    @Override
    public void teach()
    {
        System.out.println("篮球教练正在教如何打篮球");
    }
}
package 综合抽象接口练习;

public class 篮球player extends player{
    public 篮球player(){};

    public 篮球player(String name,int age){
        super(name, age);
    }

    @Override
    public void study()
    {
        System.out.println("篮球运动员会打篮球哦");
    }
}
package 综合抽象接口练习;

public class test {
    public static void main(String[] args) {
        //创建运动员或者教练对象
        乒乓player ps1=new 乒乓player("王楚钦",24);
        System.out.println(ps1.getName()+","+ps1.getAge());
        ps1.study();
        ps1.speakEnglish();
    }
}

原文地址:https://blog.csdn.net/2303_81073778/article/details/143810053

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!