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();        //打乒乓球    
    } 
}
相关推荐
努力变厉害的小超超25 分钟前
ArkTS中的组件基础、状态管理、样式处理、class语法以及界面渲染
笔记·鸿蒙
aloha_7895 小时前
从零记录搭建一个干净的mybatis环境
java·笔记·spring·spring cloud·maven·mybatis·springboot
dsywws6 小时前
Linux学习笔记之vim入门
linux·笔记·学习
A-超8 小时前
vue3展示pag格式动态图
笔记
u0101526589 小时前
STM32F103C8T6学习笔记2--LED流水灯与蜂鸣器
笔记·stm32·学习
weixin_518285059 小时前
深度学习笔记10-多分类
人工智能·笔记·深度学习
丘狸尾9 小时前
ubuntu【桌面】 配置NAT模式固定IP
笔记
王俊山IT9 小时前
C++学习笔记----10、模块、头文件及各种主题(二)---- 预处理指令
开发语言·c++·笔记·学习
慕卿扬9 小时前
基于python的机器学习(二)—— 使用Scikit-learn库
笔记·python·学习·机器学习·scikit-learn
齐 飞11 小时前
MongoDB笔记02-MongoDB基本常用命令
前端·数据库·笔记·后端·mongodb