一个JAVA小程序的问题
publicinterfacewithpet{publicStringbreed();publicStringplay();}classfarmerimplementsw...
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+"随时随地可以玩");
}
}
class test {
public static void main(String[]args) {
withpet x1 = new farmer("狗剩");
withpet x2 = new worker("男");
withpet x3 = new officer("局长");
x1.breed();
x1.play();
x2.breed();
x2.play();
x3.breed();
x3.play();
}
}
怎么就运行不对呢
请帮忙下
我初学者
谢谢 展开
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+"随时随地可以玩");
}
}
class test {
public static void main(String[]args) {
withpet x1 = new farmer("狗剩");
withpet x2 = new worker("男");
withpet x3 = new officer("局长");
x1.breed();
x1.play();
x2.breed();
x2.play();
x3.breed();
x3.play();
}
}
怎么就运行不对呢
请帮忙下
我初学者
谢谢 展开
3个回答
展开全部
错误的不算多:
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 play() {
System.out.println(sex+"晚上玩");
}
}
class officer implements withpet {
String zhiwei;
officer(String zhiwei) {
this.zhiwei = zhiwei;
}
public void breed() {
System.out.println(zhiwei+"天天大鱼大肉");
}
public void play() {
System.out.println(zhiwei+"随时随地可以玩");
}
}
class test2 {
public static void main(String[]args) {
withpet x1 = new farmer("狗剩");
withpet x2 = new worker("男");
withpet x3 = new officer("局长");
x1.breed();
x1.play();
x2.breed();
x2.play();
x3.breed();
x3.play();
}
}
————————————————执行结果——————————————
狗剩随便给口吃的
狗剩哪有时间和它玩
男下班回家喂
男晚上玩
局长天天大鱼大肉
局长随时随地可以玩
————————————有问题百度空间留言或hi我————————
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 play() {
System.out.println(sex+"晚上玩");
}
}
class officer implements withpet {
String zhiwei;
officer(String zhiwei) {
this.zhiwei = zhiwei;
}
public void breed() {
System.out.println(zhiwei+"天天大鱼大肉");
}
public void play() {
System.out.println(zhiwei+"随时随地可以玩");
}
}
class test2 {
public static void main(String[]args) {
withpet x1 = new farmer("狗剩");
withpet x2 = new worker("男");
withpet x3 = new officer("局长");
x1.breed();
x1.play();
x2.breed();
x2.play();
x3.breed();
x3.play();
}
}
————————————————执行结果——————————————
狗剩随便给口吃的
狗剩哪有时间和它玩
男下班回家喂
男晚上玩
局长天天大鱼大肉
局长随时随地可以玩
————————————有问题百度空间留言或hi我————————
展开全部
你的描述太简单,我按自己的理解改了改。你看看对不对吧:
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 play() {
System.out.println(sex+"晚上玩");
}
}
class officer implements withpet {
String zhiwei;
officer(String zhiwei) {
this.zhiwei = zhiwei;
}
public void breed() {
System.out.println(zhiwei+"天天大鱼大肉");
}
public void play() {
System.out.println(zhiwei+"随时随地可以玩");
}
}
public class testt {
public static void main(String[]args) {
withpet x1 = new farmer("狗剩");
withpet x2 = new worker("男");
withpet x3 = new officer("局长");
x1.breed();
x1.play();
x2.breed();
x2.play();
x3.breed();
x3.play();
}
}
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 play() {
System.out.println(sex+"晚上玩");
}
}
class officer implements withpet {
String zhiwei;
officer(String zhiwei) {
this.zhiwei = zhiwei;
}
public void breed() {
System.out.println(zhiwei+"天天大鱼大肉");
}
public void play() {
System.out.println(zhiwei+"随时随地可以玩");
}
}
public class testt {
public static void main(String[]args) {
withpet x1 = new farmer("狗剩");
withpet x2 = new worker("男");
withpet x3 = new officer("局长");
x1.breed();
x1.play();
x2.breed();
x2.play();
x3.breed();
x3.play();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你觉得你的表述含金量多少?几个人能看懂你在说什么!就给一堆程序,就帮忙,帮什么?怎么就运行不对了?学好编程语言之前最好把汉语学好。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询