在java中使用toString可以';找不到符号错误?
我一直在尝试将getter和setter与toString一起使用,但我很难看到我的代码有什么问题。我不确定问题到底出在哪里。 那么,我应该有一个主管道吗?我不确定在这种情况下是否需要这样做 还有,有没有什么方法可以更好地格式化“矩形(x,x)”它现在看起来有点奇怪在java中使用toString可以';找不到符号错误?,java,Java,我一直在尝试将getter和setter与toString一起使用,但我很难看到我的代码有什么问题。我不确定问题到底出在哪里。 那么,我应该有一个主管道吗?我不确定在这种情况下是否需要这样做 还有,有没有什么方法可以更好地格式化“矩形(x,x)”它现在看起来有点奇怪 public class Rectangle { // DO NOT MODIFY THE INSTANCE VARIABLES // begin instance variables private int width; priv
public class Rectangle {
// DO NOT MODIFY THE INSTANCE VARIABLES
// begin instance variables
private int width;
private int height;
// end instance variables
// TODO - write your code below this comment.
// You need to do the following:
//
// 1.) Define a constructor which takes two ints
// representing the width and height, respectively.
// The constructor should set its instance variables
// equal to these values.
//
// 2.) Define a "getter" named getWidth, which returns
// the width of the rectangle.
//
// 3.) Define a "getter" named getHeight, which returns
// the height of the rectangle.
//
// 4.) Define a "setter" named setWidth, which takes
// the new width of the rectangle and sets the
// rectangle's width to that value.
//
// 5.) Define a "setter" named setHeight, which
// takes the new height of the rectangle and sets
// the rectangle's height to that value
//
// 6.) Define a toString method, which returns
// a String representation of the rectangle.
// As an example, if the width of the rectangle is
// 3 and the height of the rectangle is 4, this should
// return the String:
//
// "Rectangle(3, 4)"
//
public Rectangle(int rectWidth, int rectHeight) {
rectWidth = width;
rectHeight = height;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public void setWidth(int rectWidth) {
width = rectWidth;
}
public void setheight(int rectHeight) {
height = rectHeight;
}
public String toString() {
return s;
}
public static void main(String[] args) {
Rectangle s = new Rectangle("Rectangle"+"("+rectWidth+",
"+rectHeight+")");
System.out.println(s);
}
}
有两点:您的构造函数不正确: 您需要为正在创建的“this”对象设置实例变量,并且必须使用正确的参数调用构造函数。 应该使用ToString来获取矩形的这个特定实例的字符串表示形式。试试这个:
public class Rectangle {
// DO NOT MODIFY THE INSTANCE VARIABLES
// begin instance variables
private int width;
private int height;
// end instance variables
// TODO - write your code below this comment.
// You need to do the following:
//
// 1.) Define a constructor which takes two ints
// representing the width and height, respectively.
// The constructor should set its instance variables
// equal to these values.
//
// 2.) Define a "getter" named getWidth, which returns
// the width of the rectangle.
//
// 3.) Define a "getter" named getHeight, which returns
// the height of the rectangle.
//
// 4.) Define a "setter" named setWidth, which takes
// the new width of the rectangle and sets the
// rectangle's width to that value.
//
// 5.) Define a "setter" named setHeight, which
// takes the new height of the rectangle and sets
// the rectangle's height to that value
//
// 6.) Define a toString method, which returns
// a String representation of the rectangle.
// As an example, if the width of the rectangle is
// 3 and the height of the rectangle is 4, this should
// return the String:
//
// "Rectangle(3, 4)"
//
public Rectangle(int rectWidth, int rectHeight) {
// "this" refers to the instance of Rectangle you are creating
// so this objects width and height are set to the values passed into the constructor...
this.width = rectWidth;
this.height = rectHeight;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public void setWidth(int rectWidth) {
width = rectWidth;
}
public void setheight(int rectHeight) {
height = rectHeight;
}
public String toString() {
// the toString returns a string representation for "this" object
return "Rectangle(" + this.width + "," + this.height + ")";
}
public static void main(String[] args) {
Rectangle s = new Rectangle(5,4);
System.out.println(s);
}
}
但请把它作为一个最小的例子。评论等不相关。现在,您似乎正在尝试调用一个不存在的
矩形(字符串)
构造函数,使用的变量rectWidth
和rectHeight
不在范围内。现在还不清楚您希望它如何工作。我怀疑main
方法的第一行应该是rectangles=newrectangle(3,4)例如,你的toString()
方法试图返回s
,它不是类中的字段……此外,我认为你的构造函数没有做它应该做的事情。您希望将参数指定给属性,而不是反过来。使用此选项进行更改。宽度=矩形宽度;this.height=矩形高度;最好将主函数放在一个单独的类中。此外,您的toString应该返回您当前在主函数中拥有的“矩形”+(“+rectWidth+”,“+rectHeight+”)部分