[英]How do I display my Object's parameters using a method
public class Vector
{
private double x;
private double y;
public Vector (double x_comp, double y_comp)
{
x= x_comp;
y= y_comp;
}
public double getX ()
{
}
public double getY ()
{
}
public double magnitude ()
{
}
public double dotProduct (Vector vec)
{
}
public Vector addVectors (Vector vec)
{
// return new Vector (add parameters);
}
public Vector unitVector ()
{
// return new Vector (add parameters);
}
public double angleBetween (Vector vec)
{
}
public String formatAsString ()
{
}
public static void main(String[]args){
Vector v1 = new Vector (-3, 2);
}
}
I'm in the midst of building a program that deals with vectors. 我正在构建一个处理向量的程序。 However, for the first two methods, I'm wondering how can I display the x coordinate and y coordinate of my Vector objects.
但是,对于前两种方法,我想知道如何显示Vector对象的x坐标和y坐标。 Is there anyway for me to use the methods to display the coordinates of the Vector objects I create?
无论如何,我是否可以使用这些方法来显示我创建的Vector对象的坐标?
In Java, all classes implicitly extend the Object
type. 在Java中,所有类都隐式扩展
Object
类型。 The Object
class specifies a method, toString()
. Object
类指定方法toString()
。 This method is designed to output a meaningful String
representing the class. 此方法旨在输出表示该类的有意义的
String
。 This can be overridden in subclasses. 可以在子类中覆盖它。
@Override
public String toString()
{
return "Meaningful String";
}
In your example, it can be something like: 在您的示例中,它可能类似于:
public String toString()
{
return "X: " + x + ", Y: " + y;
}
NOTE : For objects with a lot of parameters, it's better to use a StringBuilder
. 注意 :对于具有很多参数的对象,最好使用
StringBuilder
。 This provides a mutable version of String
, so you don't need to concatenate several values and create several instances of type String
. 这提供了一个可变的
String
版本,因此您不需要连接多个值并创建一些String
类型的实例。
For example, if you have a more complex Vector, that describes a three dimensional movement with respect to time, then you might have x
, y
, z
and t
. 例如,如果您有一个更复杂的Vector,它描述了相对于时间的三维运动,那么您可能具有
x
, y
, z
和t
。 In this case, a toString()
that looks like the following will be more efficient: 在这种情况下,如下所示的
toString()
会更有效:
public String toString()
{
StringBuilder builder = new StringBuilder();
builder.append("X: ");
builder.append(x);
builder.append("Y: ");
builder.append(y);
builder.append("Z: ");
builder.append(z);
builder.append("T: ");
builder.append(t);
return builder.toString();
}
And this can be called by calling something like. 这可以通过调用类似的东西来调用。
Vector vec = new Vector(3,-1);
System.out.println(vec);
// Outputs: X: 3, Y: -1
Displaying the values is part of your user interface . 显示值是用户界面的一部分。 The actual implementation of
Vector
is part of the application logic. Vector
的实际实现是应用程序逻辑的一部分。 These distinctions apply both to simple programs and very complex applications. 这些区别适用于简单程序和非常复杂的应用程序。
Usually, it's beneficial to keep UI separate from application logic / implementation. 通常,将UI与应用程序逻辑/实现分开是有益的。 In your case, all this really translates to is that you simply want to do this at a higher level.
就您而言,这实际上意味着您只是想在更高层次上做到这一点。 So:
所以:
Implement your getters to return the correct values. 实现您的吸气剂以返回正确的值。
Higher up in your application, a simple System.out.println("The X coord is " + myVector.getX())
will do. 在您的应用程序的更高处,一个简单的
System.out.println("The X coord is " + myVector.getX())
。
By not adding print-out code directly to Vector
, you are now free to display (or not display) the values in any format you wish. 通过不将打印输出代码直接添加到
Vector
,您现在可以自由显示(或不显示)任何所需格式的值。
You could also override toString()
, but generally this is just done for debugging (that may be your goal) rather than meaningful formatting. 您也可以重写
toString()
,但是通常这只是为了调试(可能是您的目标)而已,而不是有意义的格式化。 The other answers here describe that option well (eg christopher's answer ). 这里的其他答案很好地描述了该选项(例如christopher的答案 )。 Then you can simply
System.out.println(myVector)
. 然后,您可以简单地
System.out.println(myVector)
。
Note that the above concepts still apply to toString()
: You're still printing at a higher level, rather than from within Vector
itself, which is the take-home point here. 请注意,以上概念仍然适用于
toString()
:您仍在更高级别上进行打印,而不是从Vector
本身进行打印,这是这里的重点。
Override the toString method and print both x and y 覆盖toString方法并打印x和y
@Override
public String toString() {
return "x value is "+ x ", y value is " + y;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.