如何从 toString twin toString 的所有方法中访问 class 字段变量?
How to access class field variable inside all methods from toString twin toStrung?
一旦用 static 修饰符修改了上面的内容,第 16 行需要以下语法:
getLegs();toStrung();
//我认为这本质上是打印最后调用的方法更新 class 字段变量 toString。例如,执行 setLegs();toStrung();打印 setLegs() 的 toString。
问题:应该如何访问方法中的共享字段?如果我将它包含在构造函数中怎么办?理想情况下,我希望代码看起来像 getLegs().toStrung() 并且让 toString 成为每个方法的一个干净的平板。
我的回答:我认为每个方法中的 String toString 的一个单独实例都可以得到一个干净的石板吸引力,但语法没有意义。我知道这是关于我的设计。我认为一个解决方案是一个新的 class,但是这个 returns 与与 class 字段变量相关的相同冲突。
public class Dog{
public String toString;
public Dog(String name){
this.name = name;
}
public int getLegs(){
toString = "Dog has " + legs + " legs.";
return legs;
}
public int setLegs(int legs){
toString = getName() + "'s legs have changed from "
+ getLegs() + " to " + legs + ".";
this.legs = legs;
return this.legs;
}
public void toStrung(){
System.out.println(Dog.toString);
}
public static void main(String[] args){
Dog Dundt = new Dog("Dundt");
Dundt.getLegs();
Dundt.toStrung();
}
1) toString() 不应该是静态成员。
2) getLegs() 不应该有将成员 String 更改为 String 的副作用。
3) 不应该有成员变量String toString.
4) toString() 应该 return 一个字符串。
5) 姓名必须是会员。
6) 腿部需要成为会员。
7) toString() 应该在 运行 时间从成员中生成字符串。
8) 您不需要在 main 中显式调用 toString()。只需将 Dog 的实例传递给 println 即可为您调用它。
9) 使用@Override 注释来注释要覆盖的方法是一种很好的做法。 toString() 是 Object 的成员,而您正在覆盖 Object。
public class Dog{
private String name;
private int legs = 4;
public Dog(String name){
this.name = name;
}
public int getLegs(){
return legs;
}
public int setLegs(int legs){
this.legs = legs;
return this.legs;
}
@Override
public String toString(){
return "Dog is called " + name + " it has " + legs + " legs.";
}
public static void main(String[] args){
Dog dundt = new Dog("Dundt");
System.out.println(dundt);
}
}
一旦用 static 修饰符修改了上面的内容,第 16 行需要以下语法: getLegs();toStrung();
//我认为这本质上是打印最后调用的方法更新 class 字段变量 toString。例如,执行 setLegs();toStrung();打印 setLegs() 的 toString。
问题:应该如何访问方法中的共享字段?如果我将它包含在构造函数中怎么办?理想情况下,我希望代码看起来像 getLegs().toStrung() 并且让 toString 成为每个方法的一个干净的平板。
我的回答:我认为每个方法中的 String toString 的一个单独实例都可以得到一个干净的石板吸引力,但语法没有意义。我知道这是关于我的设计。我认为一个解决方案是一个新的 class,但是这个 returns 与与 class 字段变量相关的相同冲突。
public class Dog{
public String toString;
public Dog(String name){
this.name = name;
}
public int getLegs(){
toString = "Dog has " + legs + " legs.";
return legs;
}
public int setLegs(int legs){
toString = getName() + "'s legs have changed from "
+ getLegs() + " to " + legs + ".";
this.legs = legs;
return this.legs;
}
public void toStrung(){
System.out.println(Dog.toString);
}
public static void main(String[] args){
Dog Dundt = new Dog("Dundt");
Dundt.getLegs();
Dundt.toStrung();
}
1) toString() 不应该是静态成员。
2) getLegs() 不应该有将成员 String 更改为 String 的副作用。
3) 不应该有成员变量String toString.
4) toString() 应该 return 一个字符串。
5) 姓名必须是会员。
6) 腿部需要成为会员。
7) toString() 应该在 运行 时间从成员中生成字符串。
8) 您不需要在 main 中显式调用 toString()。只需将 Dog 的实例传递给 println 即可为您调用它。
9) 使用@Override 注释来注释要覆盖的方法是一种很好的做法。 toString() 是 Object 的成员,而您正在覆盖 Object。
public class Dog{
private String name;
private int legs = 4;
public Dog(String name){
this.name = name;
}
public int getLegs(){
return legs;
}
public int setLegs(int legs){
this.legs = legs;
return this.legs;
}
@Override
public String toString(){
return "Dog is called " + name + " it has " + legs + " legs.";
}
public static void main(String[] args){
Dog dundt = new Dog("Dundt");
System.out.println(dundt);
}
}