【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】
**开源地址:https://docs.qq.com/doc/DSmxTbFJ1cmN1R2dB **
public void setColor(String color) {
this.color = color;
}
public abstract void eat();
public abstract void sleep();
public Animal() {
}
public Animal(String name, String color) {
this.name = name;
this.color = color;
}
}
interface IRunning {
void run();
}
interface Iwalking {
void walk();
}
interface ISwimming {
void swim();
}
interface IFlying {
void fly();
}
class fish extends Animal implements ISwimming {
public void eat() {
System.out.println(“fish eat little fish”);
}
public void sleep() {
System.out.println(“fish sleep in walter”);
}
public void swim() {
System.out.println(“fish can swim”);
}
public fish(String name, String color) {
super(name, color);
}
}
class Forg extends Animal implements Iwalking, IRunning, ISwimming {
@Override
public void eat() {
System.out.println(“forg eat small animals”);
}
@Override
public void sleep() {
System.out.println(“forg sleep on leaf”);
}
@Override
public void run() {
System.out.println(“forg can run”);
}
@Override
public void swim() {
System.out.println(“forg can swim”);
}
@Override
public void walk() {
System.out.println(“forg can walk”);
}
public Forg(String name, String color) {
super(name, color);
}
}
class Duck extends Animal implements ISwimming, IRunning, Iwalking, IFlying {
private String kind;
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
@Override
public void eat() {
System.out.println(“duck eat seed”);
}
@Override
public void sleep() {
System.out.println(“duck sleep on land”);
}
@Override
public void fly() {
System.out.println(“duck can fly”);
}
@Override
public void run() {
System.out.println(“duck can run”);
}
@Override
public void swim() {
System.out.println(“duck can swim”);
}
@Override
public void walk() {
System.out.println(“duck can walk”);
}
public Duck(String name, String color) {
super(name, color);
}
}
public class Test {
public static void swim(ISwimming iSwimming) {
iSwimming.swim();
}
public static void run(IRunning iRunning) {
iRunning.run();
}
public static void fly(IFlying iFlying) {
iFlying.fly();
}
public static void walk(Iwalking iwalking) {
iwalking.walk();
}
public static void main(String[] args) {
ISwimming forg = new Forg(“wawa”, “black”);
ISwimming duck = new Duck(“dudu”, “write”);
forg.swim();
duck.swim();
System.out.println(“======================”);
Forg forg1 = new Forg(“YY”, “red”);
fish fish = new fish(“XX”, “black”);
Duck duck2 = new Duck(“LLL”, “yellow”);
swim(forg1);
swim(fish);
swim(duck2);
run(duck2);
walk(forg1);
fly(duck2);
System.out.println(“==============”);
Animal animal = new fish(“anan”, “red”);
Animal animal1 = new Duck(“aaa”, “ccc”);
animal.eat();
animal1.eat();
System.out.println(animal.getColor());
Duck duck1 = new Duck(“ccc”, " sss");
duck1.setKind(“write sky duck”);
System.out.println(duck1.getKind());
}
}
运行结果:
forg can swim