Инкапсуляция, Полиморфизм, Наследование

 
 
 
Сообщения:3
import java.io.*;
import java.util.*;

public class Nasledovanie {
    public static void main(String[] args) throws Exception{
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        ArrayList<Animal> animals = new ArrayList<>();
        animals.add(new Homyak());
        animals.add(new Pig());
        animals.add(new Dog());
        animals.add(new Bear());

        for (Animal animal : animals) {
            System.out.println("Введите данные " + animal.getClass());
            System.out.print("Имя: ");
            animal.setName(reader.readLine());
            System.out.print("Возраст: ");
            animal.setAge(Integer.parseInt(reader.readLine()));
            System.out.print("Цвет: ");
            animal.setColor(reader.readLine());
            System.out.print("Скорость: ");
            animal.setSpeed(Integer.parseInt(reader.readLine()));
        }
        for (Animal animal : animals){
            System.out.println(animal.toString());
        }

    }
}
abstract class Animal{
    private String name;
    private int age;
    private String color;
    private int speed;


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


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


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


    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public String toString() {
        return "Name: " + name + ", " + "Age: " + age + ", " + "Color: " + color + ", " + "Speed(км/ч): " + speed;
    }
}

abstract class lazy extends Animal{

}
class Homyak extends lazy{
    
}
class Pig extends lazy{

}

abstract class fast extends lazy{

}
class Dog extends fast{

}
class Bear extends fast {

}
 
Модераторы:Нет
Сейчас эту тему просматривают:Нет