分析: ①猫: 成员变量:姓名,年龄 构造方法:无参,带参 成员方法:get/set方法,抓老鼠() ②狗: 成员变量:姓名,年龄 构造方法:无参,带参 成员方法:get/set方法,看门() ③共性: 成员变量:姓名,年龄;构造方法:无参,带参;成员方法:get/set方法 步骤: 1、定义动物类(Animal) 【成员变量:姓名,年龄】【 构造方法:无参,带参】【成员方法:get/set方法】 2、定义猫类(Cat),继承动物类 【构造方法:无参,带参】【成员方法:抓老鼠() 】 3、定义狗类(Dog),继承动物类【构造方法:无参,带参】【成员方法:看门() 】 4、定义测试类(AnimalDemo),写代码测试
动物类
public class Animal{ private String name; private int age; public Animal() { } public Animal(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }猫类
class Cat extends Animal{ public Cat() { } public Cat(String name, int age) { super(name, age); } public void catchMouse(){ System.out.println("猫抓老鼠"); } } class Dog extends Animal{ public Dog() { } public Dog(String name, int age) { super(name, age); } public void lookdoor(){ System.out.println("狗看门"); } }测试类
public class AnimalDemo{ public static void main(String[] args) { //创建猫类对象并进行测试 Cat c1 = new Cat(); c1.setName("加菲猫"); c1.setAge(5); System.out.println(c1.getName() + "," + c1.getAge()); c1.catchMouse(); Dog c2 = new Dog("二哈狗", 6); System.out.println(c2.getName() + "," + c2.getAge()); c2.lookdoor(); } }动物类
public class Animal { private String name; private int age; public Animal() { } public Animal(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void eat(){ System.out.println("动物吃东西"); } }猫类
public class Cat extends Animal { public Cat() { } public Cat(String name, int age) { super(name, age); } @Override public void eat() { System.out.println("猫吃鱼"); } }狗类
public class Dog extends Animal { public Dog() { } public Dog(String name, int age) { super(name, age); } @Override public void eat() { System.out.println("狗吃骨头"); } }测试类
public class Demo { public static void main(String[] args) { Animal cat=new Cat(); cat.setName("加菲猫"); cat.setAge(5); System.out.println(cat.getName() + "," + cat.getAge()); cat.eat(); Animal dog=new Dog(); dog.setName("二哈狗"); dog.setAge(4); System.out.println(dog.getName() + "," + dog.getAge()); dog.eat(); } }动物类
public abstract class Animal01 { private String name; private int age; public Animal01() { } public Animal01(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public abstract void eat(); }猫类
public class Cat01 extends Animal01{ public Cat01() { } public Cat01(String name, int age) { super(name, age); } @Override public void eat() { System.out.println("猫吃鱼"); } }狗类
public class Dog01 extends Animal01 { public Dog01() { } public Dog01(String name, int age) { super(name, age); } @Override public void eat() { System.out.println("狗吃骨头"); } }测试类
public class Demo01 { public static void main(String[] args) { Animal01 cat=new Cat01(); cat.setName("加菲猫"); cat.setAge(6); System.out.println(cat.getName() + "," + cat.getAge()); cat.eat(); Animal01 dog=new Dog01(); dog.setName("二哈狗"); dog.setAge(6); System.out.println(dog.getName() + "," + dog.getAge()); dog.eat(); } }案例需求 对猫和狗进行训练,他们就可以跳高了,这里加入跳高功能。 请采用抽象类和接口来实现猫狗案例,并在测试类中进行测试。
动物类
public abstract class Animal02 { private String name; private int age; public Animal02() { } public Animal02(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public abstract void eat(); }跳高接口
public interface Jumpping { public abstract void jump(); }猫类
public class Cat02 extends Animal02 implements Jumpping { public Cat02() { } public Cat02(String name, int age) { super(name, age); } @Override public void eat() { System.out.println("猫吃鱼"); } @Override public void jump() { System.out.println("猫可以跳高"); } }测试类
public class Demo02 { public static void main(String[] args) { //创建对象,调用方法 Jumpping j = new Cat02(); j.jump(); System.out.println(""); Animal02 a = new Cat02(); a.setName("加菲猫"); a.setAge(5); System.out.println(a.getName()+","+a.getAge()); a.eat(); ((Cat02) a).jump(); System.out.println(""); Cat02 c = new Cat02(); c.setName("加菲猫"); c.setAge(5); System.out.println(c.getName()+","+c.getAge()); c.eat(); c.jump(); } }