java--繼承&介面
阿新 • • 發佈:2020-12-09
package test_1027;
import java.util.Scanner;
public class test {
public static void main(String[] args) {
cat c =new cat(8,"cat");
c.eat();
c.sleep();
jumpcat jc = new jumpcat(9,"jumpcat");
jc.jump();
}
}
abstract class animal {
private int age;
private String name;
public animal() {
super();
// TODO Auto-generated constructor stub
}
public animal(int age, String name) {
super();
this.age = age;
this.name = name;
System.out.println(name+"---"+age);
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public abstract void sleep() ;
public abstract void eat() ;
}
interface jump{
public void jump();
}
class cat extends animal {
public cat(int age, String name) {
super(age,name);
}
@Override
public void sleep() {
System.out.println("cat sleep");
}
@Override
public void eat() {
System.out.println("cat eat fish");
}
}
class jumpcat extends cat implements jump{
public jumpcat(int age, String name) { super(age, name); // TODO Auto-generated constructor stub } @Override public void jump() { System.out.println("cat jumping"); }
}