package net.elaina.interface01;
public abstract class Animal {
private String name;
private int age;
public Animal() {
}
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "Animal{name = " + name + ", age = " + age + "}";
}
public abstract void eat();
}
package net.elaina.interface01;
public interface Swim {
public abstract void swim();
}
package net.elaina.interface01;
public class Rabbit extends Animal {
public Rabbit() {
}
public Rabbit(String name, int age) {
super(name, age);
}
@Override
public void eat() {
System.out.println("兔子在吃胡萝卜");
}
}
package net.elaina.interface01;
public class Frog extends Animal implements Swim{
public Frog() {
}
public Frog(String name, int age) {
super(name, age);
}
@Override
public void eat() {
System.out.println("青蛙在吃虫子");
}
@Override
public void swim() {
System.out.println("青蛙在蛙泳");
}
}
package net.elaina.interface01;
public class Dog extends Animal implements Swim{
public Dog() {
}
public Dog(String name, int age) {
super(name, age);
}
@Override
public void eat() {
System.out.println("狗吃骨头");
}
@Override
public void swim() {
System.out.println("狗刨");
}
}
package net.elaina.interface01;
public class Test {
public static void main(String[] args) {
Frog f = new Frog("小青",1);
System.out.println(f.getName() + ", " + f.getAge());
f.eat();
f.swim();
Rabbit r = new Rabbit("小白",2);
System.out.println(r.getName() + ", " + r.getAge());
r.eat();
}
}
打印:
小青, 1
青蛙在吃虫子
青蛙在蛙泳
小白, 2
兔子在吃胡萝卜
标签:java,name,int,void,接口,public,age,String
From: https://www.cnblogs.com/elaina520/p/17845051.html