lnmp搭建后怎么做网站,南京网站建设雷仁网络,产品开发的基本流程,百度快照查询入口目录
猫#x1f431;和狗#x1f415;#xff08;接口版本#xff09;
画图分析 案例代码 猫#x1f431;和狗#x1f415;#xff08;接口版本#xff09; 需求#xff1a;对猫和狗进行训练#xff0c;它们就可以跳高了#xff0c;这里加入了跳高功能#xff0…目录
猫和狗接口版本
画图分析 案例代码 猫和狗接口版本 需求对猫和狗进行训练它们就可以跳高了这里加入了跳高功能请采用抽象类和接口来实现猫和狗的案例。 1. 定义跳高接口JumpInterface 成员方法跳高——jump(); 2. 定义动物类抽象类Animal实现跳高接口 成员变量姓名、年龄构造方法无参、带参 成员方法get/set方法吃饭——eat() 3. 定义猫类Cat,继承动物类Animal 构造方法无参、带参成员方法重写eat()重写跳高jump() 4. 定义狗类Dog,继承动物类Animal 构造方法无参、带参成员方法重写eat()重写跳高jump() 画图分析 案例代码
JumpInterface.java
package com.面向对象.Demo30;public interface JumpInterface {//跳高方法jump()void jump(); // public abstract void jump();
}Animal.java
package com.面向对象.Demo30;public abstract class Animal implements JumpInterface {private String name; // 姓名public int age; // 年龄public Animal() {}public Animal(String name, int age) {this.name name;this.age age;}/*** 在 Animal 动物类中 实际已经有一个 jump()跳高的 抽象方法* p* 抽象类 实现接口中的jump() 抽象方法* 相当于在Animal(抽象)类 实现了接口* 意味这在Animal类中存在jump()抽象方法* 正常情况是不需要写的让抽象类的子类进行重写*/public abstract void eat();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;}
}Cat.java
package com.面向对象.Demo30;public class Cat extends Animal {public Cat(String name, int age) {//super(); 默认第一行 调用父类中的无参构造方法super(name, age);}Overridepublic void jump() {System.out.println(Cat-jump());}Overridepublic void eat() {System.out.println(Cat-eat());}
}Dog.java
package com.面向对象.Demo30;public class Dog extends Animal {
// public Dog(String name, int age) {
// super(name, age);
// }Overridepublic void jump() {System.out.println(Dog-jump());}Overridepublic void eat() {System.out.println(Dog-eat());}
}AnimalDemo.java
package com.面向对象.Demo30;public class AnimalDemo {public static void main(String[] args) {JumpInterface cat1 new Cat(小猫1, 1);
// System.out.println(cat1.getName() , cat1.getAge());//报错编译看左边JumpInterface中没有get/set方法cat1.jump();
// cat.eat(); //报错编译看左边JumpInterface中没有eat方法System.out.println();Animal cat2 new Cat(小猫2, 2);System.out.println(name:cat2.getName() ,age: cat2.getAge());cat2.jump();cat2.eat();System.out.println();Animal dog new Dog();dog.setName(小狗1);dog.setAge(1);System.out.println(name:dog.getName(),age:dog.getAge());dog.jump();dog.eat();}
}下一篇文章