package com.cn1; public class AnimalDemo { public static void main(String[] args) { jumpping j = new Cat(); j.jumpping(); System.out.println("------------------"); Animal a = new Cat(); a.setAge(14); a.setName("xiu mao~"); a.eat(); System.out.println(a.getAge()+".."+a.getName()); jumpping j1 = new Dog(); ((Cat) a).jumpping(); System.out.println("----------------"); a = new Cat("jiafei",1); System.out.println(a.getAge()+".."+a.getName()); Animal a1 = new Dog(); a1.setName("xiu gou~~"); a1.setAge(12); a1.eat(); ((Cat) a1).jumpping(); System.out.println(a1.getAge()+".."+a1.getName()); System.out.println("----------------"); Cat cat = new Cat(); cat.setAge(13); cat.setName("jiajiafeifeifei"); cat.eat(); cat.jumpping(); System.out.println(cat.getAge()+".."+cat.getName()); } }
package com.cn1; public abstract 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 abstract void eat(); }
package com.cn1; public interface jumpping { public abstract void jumpping(); }
package com.cn1; public class Cat extends Animal implements jumpping{ public Cat() { } public Cat(String name, int age) { super(name, age); } @Override public void eat() { System.out.println("cat eat fish"); } @Override public void jumpping() { System.out.println("jumping catcat"); } }
package com.cn1; public class Dog extends Animal implements jumpping{ public Dog() { } public Dog(String name, int age) { super(name, age); } @Override public void eat() { System.out.println("dog eat meat"); } @Override public void jumpping() { System.out.println("jumpping dogdog"); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)