Java 中关于 toString()方法的使用? 怎么把 toString 打印出来
packageHomework;publicclassAnimal{publicstaticvoidmain(String[]args){Animalal=newAnim...
package Homework;
public class Animal {
public static void main(String[] args) {
Animal al = new Animal(10, "张三", 8.0);
}
public Animal(int age, String name, double weight) {
}
void move() {
System.out.println("move");
}// move方法
int age;
String name;
double weight;
void setAge(int age) {
this.age = age;
}
int getAge() {
return age;
}
void setName(String name) {
this.name = name;
}
String getName() {
return name;
}
void setWeight(double weight) {
this.weight = weight;
}
double getWeight() {
return weight;
}
public String toString() {
return "Animal [age=" + age + ", name=" + name + ", weight=" + weight
+ "]";
}
} 展开
public class Animal {
public static void main(String[] args) {
Animal al = new Animal(10, "张三", 8.0);
}
public Animal(int age, String name, double weight) {
}
void move() {
System.out.println("move");
}// move方法
int age;
String name;
double weight;
void setAge(int age) {
this.age = age;
}
int getAge() {
return age;
}
void setName(String name) {
this.name = name;
}
String getName() {
return name;
}
void setWeight(double weight) {
this.weight = weight;
}
double getWeight() {
return weight;
}
public String toString() {
return "Animal [age=" + age + ", name=" + name + ", weight=" + weight
+ "]";
}
} 展开
3个回答
展开全部
public static void main(String[] args) {
Animal al = new Animal(10, "张三", 8.0);
System.out.println(al.toString());
}
这样就行了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
xxx.toString() 方法是将 xxx 转成String对象,不知道你想问的什么意思?
你自己写了一个toString() 方法, 那就直接调用就行了
你自己写了一个toString() 方法, 那就直接调用就行了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询