小弟需要大神帮助。哪位大神可以帮小弟编写Java程序,谢谢了,
3个回答
展开全部
package com.work.demo1;
public class Rabbit extends Animal{
@Override
public void swiming() {
System.out.println("那只"+animalColour+"的,名叫"+animalName+"的兔子正在叽叽的叫");
}
@Override
protected void eat() {
System.out.println("兔子是"+animalClass+",爱吃胡萝卜");
}
@Override
protected void cry() {
System.out.println("兔八哥和兔斯基也是会游泳的!");
}
}
package com.work.demo1;
public class Frog extends Animal{
@Override
public void swiming() {
System.out.println("那只"+animalColour+"的,名叫"+animalName+"的青蛙正在呱呱的叫");
}
@Override
protected void eat() {
System.out.println("青蛙是"+animalClass+",爱吃昆虫");
}
@Override
protected void cry() {
System.out.println("虽然不是鱼,但青蛙也是泳坛高手!");
}
}
package com.work.demo1;
public interface ISwiming {
void swiming();
}
package com.work.demo1;
public abstract class Animal implements ISwiming{
protected String animalName;
protected String animalColour;
protected String animalClass;
protected abstract void eat();
protected abstract void cry();
}
package com.work.demo1;
public class AnimalTest {
static Animal rabbit = new Rabbit();
static Animal frog = new Frog();
public static void init(){
rabbit.animalColour = "黑色";
rabbit.animalName="美人";
rabbit.animalClass="哺乳类";
frog.animalClass="绿色";
frog.animalColour="大兵";
frog.animalName="非哺乳类";
}
public static void main(String[] args) {
rabbit.eat();
rabbit.cry();
System.out.println("-----------------------");
frog.eat();
frog.cry();
frog.swiming();
}
}
public class Rabbit extends Animal{
@Override
public void swiming() {
System.out.println("那只"+animalColour+"的,名叫"+animalName+"的兔子正在叽叽的叫");
}
@Override
protected void eat() {
System.out.println("兔子是"+animalClass+",爱吃胡萝卜");
}
@Override
protected void cry() {
System.out.println("兔八哥和兔斯基也是会游泳的!");
}
}
package com.work.demo1;
public class Frog extends Animal{
@Override
public void swiming() {
System.out.println("那只"+animalColour+"的,名叫"+animalName+"的青蛙正在呱呱的叫");
}
@Override
protected void eat() {
System.out.println("青蛙是"+animalClass+",爱吃昆虫");
}
@Override
protected void cry() {
System.out.println("虽然不是鱼,但青蛙也是泳坛高手!");
}
}
package com.work.demo1;
public interface ISwiming {
void swiming();
}
package com.work.demo1;
public abstract class Animal implements ISwiming{
protected String animalName;
protected String animalColour;
protected String animalClass;
protected abstract void eat();
protected abstract void cry();
}
package com.work.demo1;
public class AnimalTest {
static Animal rabbit = new Rabbit();
static Animal frog = new Frog();
public static void init(){
rabbit.animalColour = "黑色";
rabbit.animalName="美人";
rabbit.animalClass="哺乳类";
frog.animalClass="绿色";
frog.animalColour="大兵";
frog.animalName="非哺乳类";
}
public static void main(String[] args) {
rabbit.eat();
rabbit.cry();
System.out.println("-----------------------");
frog.eat();
frog.cry();
frog.swiming();
}
}
追问
谢谢了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
给的这么显而易见,定义接口和父类然后去继承跟实现啊;比如他们都是动物,吧动物做父类,然后都继承动物,动物油两个方法叫 和 吃;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |