java 抽象类与抽象方法
建立鱼的抽象类,鱼类Fish,可以分为淡水鱼(Freshwater_fish)和热带鱼(Tropical_fish),这里以淡水鱼为例,淡水鱼包含鲤鱼Carp、鲈鱼Wee...
建立鱼的抽象类,鱼类Fish,可以分为淡水鱼(Freshwater_fish )和热带鱼(Tropical_fish),这里以淡水鱼为例,淡水鱼包含鲤鱼Carp、鲈鱼Weever、鲶鱼Catfish等,每种都可以游动( void swimming() )。
展开
展开全部
不知道合不合适
package com.kidd.baiduzhidao;
public class Test {
public static void main(String[] args) {
Fish fish = new Freshwater_fish();
fish.swimming();
fish = new Tropical_fish();
fish.swimming();
fish = new Carp();
fish.swimming();
}
}
class Fish implements Swimable {
@Override
public void swimming() {
System.out.println("I'm Fish,I can Swimming.");
}
}
class Freshwater_fish extends Fish {
@Override
public void swimming() {
System.out.println("I'm Freshwater_fish,I can Swimming.");
}
}
class Tropical_fish extends Fish {
@Override
public void swimming() {
System.out.println("I'm Tropical_fish,I can Tropical_fish.");
}
}
class Carp extends Freshwater_fish {
@Override
public void swimming() {
System.out.println("I'm Carp,I'm a Freshwater_fish,I can Swimming.");
}
}
class Weever extends Freshwater_fish {
@Override
public void swimming() {
System.out.println("I'm Weever,I'm a Freshwater_fish,I can Swimming.");
}
}
class Catfish extends Freshwater_fish {
@Override
public void swimming() {
System.out.println("I'm Catfish,I'm a Freshwater_fish,I can Swimming.");
}
}
package com.kidd.baiduzhidao;
public interface Swimable {
public void swimming();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询