一个JAVA小程序的问题

来源:百度知道 编辑:UC知道 时间:2024/05/31 10:27:23
public interface withpet {

public String breed();
public String play();
}

class farmer implements withpet {
String name;
farmer(String name) {
this.name = name;
}
public String breed() {
System.out.println(name+"随便给口吃的");
}
public String play() {
System.out.println(name+"哪有时间和它玩");
}
}

class worker implements withpet {
String sex;
worker(String sex) {
this.sex = sex;
}
public String breed() {
System.out.println(sex+"下班回家喂");
}
public String play() {
System.out.println(sex+"晚上玩");
}
}

class officer implements withpet {
String zhiwei;
officer(String zhiwei) {
this.zhiwei = zhiwei;
}
public String breed() {
System.out.println(zhiwei+"天天大鱼大肉");
}
public String play() {
System.out.println(zhiwei+"随时

错误的不算多:
1.public interface withpet {

public String breed();
public String play();
} 定义了公共的接口那么文件名要和类名一致,考虑到你的情况将public去掉即可。
2.public String breed() {
System.out.println(name+"随便给口吃的");
} 实现接口的子类中重写出错了。你的方法要返回String类型的值而不是输出。
将接口和子类中的方法的返回值String改为void。代码贴上:
——————————————test.java————————————————

interface withpet {

public void breed();
public void play();
}

class farmer implements withpet {
String name;
farmer(String name) {
this.name = name;
}
public void breed() {
System.out.println(name+"随便给口吃的");
}
public void play() {
System.out.println(name+"哪有时间和它玩");
}
}

class worker implements withpet {
String sex;
worker(String sex) {
this.sex = sex;
}
public void breed() {
System.out.println(sex+"下班回家喂");
}
public void