- class Test_Interface {
- public static void main(String[] args) {
- Cat c = new Cat("黑猫",2);
- c.eat();
- c.sleep();
- c.jump();
-
- JumpCat j = new JumpCat("跳高猫",5);
- j.eat();
- j.sleep();
- j.jump();
-
- }
- }
- /*
- A:案例演示
- 动物类:姓名,年龄,吃饭,睡觉。
- 猫
- 动物培训接口:跳高
- */
- abstract class Animal {
- private String name;
- private int age;
- public Animal() {}
- public Animal(String name,int age) {
- this.name = name;
- this.age = age;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getName() {
- return name;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public int getAge() {
- return age;
- }
- public abstract void eat();
- public abstract void sleep();
- }
- class Cat extends Animal {
- public Cat() {}
- public Cat(String name,int age) {
- super(name,age);
- }
- public void eat() {
- System.out.println("猫吃鱼");
- }
- public void sleep() {
- System.out.println("猫侧着睡");
- }
- public void jump() {
- System.out.println("猫跳高");
- }
- }
- interface Jumping {
- public abstract void jump();
- }
- class JumpCat extends Cat implements Jumping {
- //为什么要特意定义一个jumpCat类啊??上面Cat类extends Animal implements Jumping不就可以了吗?
- public JumpCat() {}
- public JumpCat(String name,int age) {
- super(name,age);
- }
- public void jump() {
- System.out.println("猫跳高");
- }
- }
复制代码
|
|