JAVA题目笔记(十一)多态+带有抽象类/接口的JavaBean类

一、多态定义方法并调用子类特有方法

java 复制代码
public class Animal {
    private String colour;
    private int age;

    public Animal(){

    }

    public Animal(int age,String colour){
        this.age=age;
        this.colour=colour;
    }

    public String getColour() {
        return colour;
    }


    public void setColour(String colour) {
        this.colour = colour;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    public void eat(String something){
        System.out.println("吃"+something);
    }

}

public class Cat extends Animal {
    public Cat(){
        super();
    }

    public Cat(int age,String colour){
        super(age,colour);
    }


    public void catchMouse(){
        System.out.println("抓老鼠");
    }
}

public class Dog extends Animal {

    public Dog(){
        super();
    }

    public Dog(int age,String colour){
        super(age,colour);
    }


    public void lookHome(){
        System.out.println("看家");
    }

}

public class Person {
    private String name;
    private int age;


    public Person() {
    }

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

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

    public void KeepPet(Animal animal,String something) {
        if(animal instanceof Dog d) {
            System.out.println(this.age + "岁的" + this.name + "养了一只" + animal.getAge() + "的" + animal.getColour() + "的狗抱着" + something + "猛吃。");
            d.lookHome();
        }
        else if(animal instanceof Cat d) {
            System.out.println(this.age + "岁的" + this.name + "养了一只" + animal.getAge() + "的" + animal.getColour() + "的猫抱着" + something + "猛吃。");
            d.catchMouse();
        }
    }

}

public class Test {
    public static void main(String[] args){
        Animal dog=new Dog(2,"黑");
        Person man=new Person("张三",30);
        Person woman=new Person("张四",39);
        Cat cat=new Cat(1,"白色");
        man.KeepPet(dog,"鸡腿");
        woman.KeepPet(cat,"猫粮");
    }
}

知识点:

对象名 instanceof 类名 d :

返回 true 或 false,若为true 则将该对象赋值为d,d为对应类。可以通过d调用该类的特有方法。

二、编写带有抽象类的标准JavaBean类

java 复制代码
package test;

public abstract class Animal {
    private String name;

    public Animal(){

    }

    public Animal(String name){
        this.name=name;
    }

    public String getName() {
        return name;
    }

    public void setAge(String name) {
        this.name = name;
    }
    public abstract void eat();

}


package test;

public class Dog extends Animal{
    @Override
    public void eat(){
        System.out.println("吃骨头");
    }
}

package test;

public class Frog extends Animal{
    @Override
    public void eat(){
        System.out.println("吃虫子");
    }

    public Frog(){
        super();
    }
    public Frog(String name){
        super(name);
    }
}

package test;

public class Sheep extends Animal{
    @Override
    public void eat(){
        System.out.println("吃草");
    }
}

package test;

import com.test1.Teacher;

public class Test {
    public static void main(String[] args){
        Frog frog=new Frog("青蛙王子");
        frog.eat();  //吃虫子
    }
}

三、带有接口和抽象类的标准JavaBean类

java 复制代码
package test;

public abstract class Person {
    private int age;
    private String name;

    public Person() {
    }

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

    public abstract void work();



    public int getAge() {
        return age;
    }


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


    public String getName() {
        return name;
    }


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

}

package test;

public abstract class Coach extends Person{
    public Coach(){
        super();
    }
    public Coach(String name,int age){
        super(name,age);
    }
}


package test;

public abstract class Player extends Person{

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

package test;

public class BasketBallCoach extends Coach{
    public BasketBallCoach(){
        super();
    }
    public BasketBallCoach(String name,int age){
        super(name,age);
    }

    public void work(){
        System.out.println("教打篮球");
    }
}


package test;

public class BasketBallPlayer extends Player{

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

    @Override
    public void work() {
        System.out.println("打篮球");
    }
}


package test;

public class TennisCoach extends Coach implements SpeakEnglish{
    public TennisCoach(){
        super();
    }
    public TennisCoach(String name,int age){
        super(name,age);
    }

    public void work(){
        System.out.println("教打乒乓球");
    }

    @Override
    public void speakEnglish() {
        System.out.println("教练学英语");
    }
}


package test;

public class TennisPlayer extends Player implements SpeakEnglish{

    public TennisPlayer(){
        super();
    }
    public TennisPlayer(String name,int age){
        super(name,age);
    }
    @Override
    public void work() {
        System.out.println("打乒乓球");
    }

    @Override
    public void speakEnglish() {
        System.out.println("队员学英语");
    }
}




package test;

import com.test1.Teacher;

import java.sql.SQLOutput;

public class Test{
    public static void main(String[] args){
        BasketBallCoach coach1=new BasketBallCoach("张三教练",40);
        TennisCoach coach2=new TennisCoach("李四教练",45);
        BasketBallPlayer player1=new BasketBallPlayer("易建联",20);
        TennisPlayer player2=new TennisPlayer("张继科",30);
        player2.speakEnglish();  //教练学英语
        coach2.speakEnglish();   //队员学英语
        coach2.work();         //教打乒乓球
        coach1.work();         //教打篮球
        player1.work();        //打篮球
        player2.work();        //打乒乓球    
    } 
}
相关推荐
Octopus20777 小时前
【Linux】vim的使用
linux·笔记·学习·vim
谢白羽11 小时前
CUDA补充笔记
笔记
醉陌离11 小时前
渗透测试笔记—shodan(7完结)
笔记
咔叽布吉12 小时前
【前端学习笔记】ES6 新特性
前端·笔记·学习
超越✔12 小时前
学习内容分享
笔记·学习·面试
Lostgreen12 小时前
SQL on Hadoop
数据库·hadoop·笔记·分布式·sql·学习
坚硬果壳_13 小时前
《硬件架构的艺术》笔记(八):消抖技术
笔记·硬件架构
八年。。13 小时前
MATLAB 中有关figure图表绘制函数设计(论文中常用)
开发语言·笔记·学习·matlab
THRUSTER1111114 小时前
Java学习笔记--继承的介绍,基本使用,成员变量和成员方法访问特点
java·开发语言·笔记·学习·学习方法·继承·intellij idea
DDDiccc14 小时前
JAVA题目笔记(二十)Stream流综合练习+方法引用
笔记