首页 > 技术文章 > java 动物类 继承 Bird Dog Fish

skprimin 2021-07-11 00:20 原文

java 动物类 继承 Bird Dog Fish

classDiagram示意图

Animal -byte legs -byte hands -byte age -String color -String name +setter() +getter() Bird -String wing +String getWing +void setWing +void fly Fish -String surface +String getSurface +void setSurface +void swimming Dog -byte voice +byte getVoice +void setVoice +void run

Animals

public class Animals {
    // property :

    private byte legs;
    private byte hands;
    private byte age;
    private String color;
    private String name;

    // setter getter method

    public byte getLegs() {
        return legs;
    }

    public void setLegs(byte legs) {
        this.legs = legs;
    }

    public byte getHands() {
        return hands;
    }

    public void setHands(byte hands) {
        this.hands = hands;
    }

    public byte getAge() {
        return age;
    }

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

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getName() {
        return name;
    }

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

    // method

    public void eat(){
    	System.out.print("It can eat food, ");
    }

    public void sleep(){
    	System.out.println("can sleep.");
    }

}

Bird

public class Bird extends Animals {
    private String wing;

    public String getWing() {
        return wing;
    }

    public void setWing(String wing) {
        this.wing = wing;
    }

    public void fly(){
    	System.out.println("It can fly in sky.");
    }

}

Dog

public class Dog extends Animals {

    private byte voice;

    public byte getVoice() {
        return voice;
    }

    public void setVoice(byte voice) {
        this.voice = voice;
    }

    public void run() {
        System.out.println("It can run fast");
    }
}

Fish

public class Fish extends Animals {

    private String surface;

    public String getSurface() {
        return surface;
    }

    public void setSurface(String surface) {
        this.surface = surface;
    }

    public void swimming() {
        System.out.println("It can swimming.");
    }
}

Main

import static javax.swing.JRootPane.NONE;

public class Main {
    public static void main(String[] args) {
        
        Bird bird = new Bird();
        bird.setName("Melanocy phamongollca 百灵鸟 Lark Bird");
        bird.setAge((byte) 3);
        bird.setLegs((byte) 2);
        bird.setHands((byte) 2);
        bird.setColor("blue");
        bird.setWing("have swing");

        System.out.println("Name: " + bird.getName() + ", Age: " + bird.getAge() +
                ", Legs: " + bird.getLegs() + ", Hands: " + bird.getHands() +
                ", Speciality: " + bird.getWing());
        bird.eat();
        bird.sleep();
        bird.fly();


        Dog dog = new Dog();
        dog.setName("Chinese rural dog 中华田园犬");
        dog.setAge((byte) 2);
        dog.setLegs((byte) 4);
        dog.setHands((byte) NONE);
        dog.setColor("white");
        dog.setVoice((byte) 70);

        System.out.println("Name: " + dog.getName() + ", Age: " + dog.getAge() +
                ", Legs: " + dog.getLegs() + ", Hands: " + dog.getHands() +
                ", Speciality: howl " + dog.getVoice() + "dB");
        dog.eat();
        dog.sleep();
        dog.run();


        Fish fish = new Fish();
        fish.setName("Carassius auratus 金鱼 Gold Fish");
        fish.setAge((byte) 1);
        fish.setLegs((byte) NONE);
        fish.setHands((byte) 2);
        fish.setColor("Gold");
        fish.setSurface(" Colorful and Beautiful");

        System.out.println("Name: " + fish.getName() + ", Age: " + fish.getAge() +
                ", Legs: " + fish.getLegs() + ", Hands: " + fish.getHands() +
                ", Speciality: Looks" + fish.getSurface());
        fish.eat();
        fish.sleep();
        fish.swimming();

    }
}

运行结果

Name: Melanocy phamongollca 百灵鸟 Lark Bird, Age: 3, Legs: 2, Hands: 2, Speciality: have swing
It can eat food, can sleep.
It can fly in sky.
Name: Chinese rural dog 中华田园犬, Age: 2, Legs: 4, Hands: 0, Speciality: howl 70dB
It can eat food, can sleep.
It can run fast
Name: Carassius auratus 金鱼 Gold Fish, Age: 1, Legs: 0, Hands: 2, Speciality: Looks Colorful and Beautiful
It can eat food, can sleep.
It can swimming.

推荐阅读