D:\>javac cat.java cat.java:12: 错误: 需要'{' public class Main(String[] args) ^ 1 个错误
publicclasscat{publicstringname;publicintage;publicintweight;publicstringcolor;public...
public class cat
{
public string name;
public int age;
public int weight;
public string color;
public string tostring()
{
return"名字:"+name+"\t年龄:"+age+"\t重量:"+weight+"\t颜色:"+color+"\n";
}
}
public class Main(String[] args)
{
private Cat cat1;
private Cat cat2;
private Cat cat3;
private String tostring()
{
return cat1.tostring()+cat1.tostring()+cat1.tostring();
}
public static void main(String[] args)
{
cat1=new Cat();
cat2=new Cat();
cat3=new Cat();
cat1.name="龙,4,2";
main a=new main();
a.tostring();
}
} 展开
{
public string name;
public int age;
public int weight;
public string color;
public string tostring()
{
return"名字:"+name+"\t年龄:"+age+"\t重量:"+weight+"\t颜色:"+color+"\n";
}
}
public class Main(String[] args)
{
private Cat cat1;
private Cat cat2;
private Cat cat3;
private String tostring()
{
return cat1.tostring()+cat1.tostring()+cat1.tostring();
}
public static void main(String[] args)
{
cat1=new Cat();
cat2=new Cat();
cat3=new Cat();
cat1.name="龙,4,2";
main a=new main();
a.tostring();
}
} 展开
2个回答
展开全部
您的代码问题比较多的,您用命令行编译的,还是IDE工具(IDE工具会标注语法错误)?
下面是修改过的代码(有些地方猜测不出意图,有问题请追问)
保存为Main.java
class Cat {
public String name;
public int age;
public int weight;
public String color;
public String tostring() {
return "名字:" + name + "\t年龄:" + age + "\t重量:" + weight + "\t颜色:" + color + "\n";
}
}
public class Main {
private Cat cat1;
private Cat cat2;
private Cat cat3;
private String tostring() {
return cat1.tostring() + cat2.tostring() + cat3.tostring();
}
public static void main(String[] args) {
Main a = new Main();
a.cat1 = new Cat();
a.cat2 = new Cat();
a.cat3 = new Cat();
//如果你想表示name="龙";age=4;weight=2;建议使用构造函数或赋值方法
a.cat1.name = "龙,4,2";
System.out.println(a.tostring());
}
}
追问
运用重写toString()方法来使用字符串输出对象内容,对象内容就是3只宠物猫的名字、年龄、重量、颜色 我是参照别人的代码能把你弄好的代码发过来下吗我是用控制台命令的
追答
您先运行一下啊;我发的已经是改过的了;有问题再问;帮你改
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询