java编程:(二)类的继承(二)类的继承1.修改(一)中的Animal类,使它的成员变量name和age能在它的子类中

1个回答

  • public class DogTest {

    public static void main(String[] args) {

    Dog dog = new Dog("Mimi ", "3");

    dog.setNickname("Baby");

    System.out.println(dog.name + "is" + dog.age + " years old");

    System.out.println("It’s nickname is " + dog.getNickname());

    Animal animal = new Animal("Tom ", "3");

    System.out.println(animal.name + animal.run());

    }

    }

    /**

    *

    */

    package test;

    /**

    * @author lilin

    *

    */

    public class Dog extends Animal {

    public Dog(String name, String age) {

    super(name, age);

    }

    private String nickname;

    public String getNickname() {

    return nickname;

    }

    public void setNickname(String nickname) {

    this.nickname = nickname;

    }

    public String run() {

    System.out.println("run by four feet");

    return null;

    }

    }

    /**

    *

    */

    package test;

    /**

    * @author lilin

    *

    */

    public class Animal {

    protected String name;

    protected String age;

    public Animal(String name, String age) {

    this.name = name;

    this.age = age;

    }

    public String run() {

    return "can run";

    }

    }