简体   繁体   English

如何使用方法显示对象的参数

[英]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,它描述了相对于时间的三维运动,那么您可能具有xyzt 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: 所以:

  1. Implement your getters to return the correct values. 实现您的吸气剂以返回正确的值。

  2. 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.

相关问题 Spring MVC - 如何使用控制器方法更新模型对象的属性? - Spring MVC - How do I update a model object's attributes using a controller method? 如何从Country类中调用对象方法? - How do I call an object method from my Country class? 如何在Android上将自己的方法与HttpURLConnection对象一起使用? - How do I use my own method with an HttpURLConnection object on Android? 我的对象正在使用另一个对象的参数返回答案 - My object is returning an answer using another object's parameters 如何在 android studio 中使用 recyclerview 显示我的数据库? - How do i display my database using recyclerview in android studio? 如何使用 Postman 测试控制器的方法,该方法在 Spring 应用程序中将一个(或多个)对象作为参数? - How do I test with Postman a Controller's method which has one (or multiple) objects as parameters in a Spring Application? 如何确定我的树方法需要哪些参数? - How do I figure out what parameters my tree method needs? 如何从另一个方法访问在主方法中实例化的对象? - How do I access an object I've instantiated in my main method from another method? 使用Mockito,如何拦截void方法上的回调对象? - Using Mockito, how do I intercept a callback object on a void method? 在我的 contains 和 equals 方法中使用 Object 对象之前,是否必须将其强制转换为特定类型? - Do I have to cast an Object object to a specific type before using it in my contains and equals method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM