在方法内实例化两个对象以用作“返回值”;

发布于 2024-09-25 09:09:08 字数 464 浏览 5 评论 0 原文

我有以下类:

public class Matriz
{
  ...
  static public void create(Matriz a, Matriz b)
  {
     ...
     a=new Matriz(someValue,anotherValue);
     b=new Matriz(someValue,anotherValue);    
     ...
  }
}

在我的主要方法中:

public static void main(String[] args)
{
  Matriz a=null,b=null;
  Matriz.create(a,b);
  //these are null
  a.print();
  b.print();
}

我的方法 create() 的要点是“返回”两个 Matriz 对象。我怎么能这样做呢?

I've got the following class:

public class Matriz
{
  ...
  static public void create(Matriz a, Matriz b)
  {
     ...
     a=new Matriz(someValue,anotherValue);
     b=new Matriz(someValue,anotherValue);    
     ...
  }
}

And in my main method:

public static void main(String[] args)
{
  Matriz a=null,b=null;
  Matriz.create(a,b);
  //these are null
  a.print();
  b.print();
}

The point of my method create() is to "return" two Matriz objects. How could I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

赤濁 2024-10-02 09:09:08

这里有一些建议:

1)返回它们的列表:

public List<Matriz> create(..);
...
List<Matriz> matrizList = Matriz.create(...);
a = matrizList.get(0);
b = matrizList.get(1);

2)返回一个方法对象

public MatrizHolder create(...);
...
MatrizHolder holder = Matriz.create(...);
a = holder.getA();
b = holder.getB();

3)一次创建一个它们

public Matriz create(...);
...
a = Matriz.create(..);
b = Matriz.create(..);

顺便说一句,你不能将空引用传递给方法,而是在方法中对其进行初始化并在方法完成时保留引用。因此,将 a 和 b 传递给上面代码中的 create 方法是没有意义的。

Here are a few suggestions:

1) Return a list of them:

public List<Matriz> create(..);
...
List<Matriz> matrizList = Matriz.create(...);
a = matrizList.get(0);
b = matrizList.get(1);

2) Return a method Object

public MatrizHolder create(...);
...
MatrizHolder holder = Matriz.create(...);
a = holder.getA();
b = holder.getB();

3) Create them one at a time

public Matriz create(...);
...
a = Matriz.create(..);
b = Matriz.create(..);

As an aside, you can't pass a null reference to a method, have it initialized in a method and retain the reference when the method completes. Hence, passing a and b to the create method in your code above doesn't make sense.

倦话 2024-10-02 09:09:08

最简单的方法是返回一个数组:

static public Matriz[] create()
{
    ...
    Matriz[] m = new Matriz[2];
    m[0] = new Matriz(someValue, anotherValue);
    m[1] = new Matriz(someValue, anotherValue); 
    ...
    return m;
}

The simplest way is to just return an array:

static public Matriz[] create()
{
    ...
    Matriz[] m = new Matriz[2];
    m[0] = new Matriz(someValue, anotherValue);
    m[1] = new Matriz(someValue, anotherValue); 
    ...
    return m;
}
鸢与 2024-10-02 09:09:08

要么返回两个数组:

public static Matriz[] create(..) {

   ...
   return new Matriz[] {a, b};
}

要么引入一个新类 -

class MatrizPair {
   private Matriz a;
   private Matriz b;

   // setter and getter for each
}

重要的是要注意您的示例不起作用,因为 java 是“按值传递”,而不是“按引用传递”(阅读 这个

Either return an array of the two:

public static Matriz[] create(..) {

   ...
   return new Matriz[] {a, b};
}

or introduce a new class -

class MatrizPair {
   private Matriz a;
   private Matriz b;

   // setter and getter for each
}

It's important to note that your example does not work because java is "pass-by-value", rather than "pass-by-reference" (read this)

千纸鹤带着心事 2024-10-02 09:09:08

这种事情在Java中行不通,因为Java是按值调用的,而不是按引用调用的。

如果您想要此功能,您可以做的是将任何类型的容器传递给该方法(数组、集合,一个 AtomicReference 等,但是仅仅使用变量是不行的。变量在传入方法时会被复制,因此无法更改原始值如果您确实传递了一个容器,则可以从该方法插入一个值,并且该值从原始上下文中可见,因为外部方法指向同一个容器对象(引用被复制,但值未被复制) t)。

This kind of thing won't work in Java, because Java calls by value, not by reference.

What you can do if you want this functionality is to pass a container of any kind to the method (an array, a Collection, an AtomicReference etc., but you can't do it just by using a variable. The variable is copied when passed into the method, so the original value can't be changed by the method. If you do pass a container, you can insert a value from the method, and that will be visible from the original context because the outer method points to the same container object (the references are copied, but the values aren't).

场罚期间 2024-10-02 09:09:08

首先,您不应该传入要实例化的对象。只需返回一个数组即可。

所以你的代码看起来像这样

public class Matriz
{
  ...
  static public Matriz[] create()
  {
     ...
     a=new Matriz(someValue,anotherValue);
     b=new Matriz(someValue,anotherValue);    
     ...
     return new  Matriz[] {a, b};
  }
}

你的主要方法:

public static void main(String[] args)
{
  Matriz[] array = Matriz.create();
  array[0].print();
  array[1].print();
}

Firstly, you should not be passing in the objects you want to instantiate. Just return an array.

So your code would look like this

public class Matriz
{
  ...
  static public Matriz[] create()
  {
     ...
     a=new Matriz(someValue,anotherValue);
     b=new Matriz(someValue,anotherValue);    
     ...
     return new  Matriz[] {a, b};
  }
}

And your main method:

public static void main(String[] args)
{
  Matriz[] array = Matriz.create();
  array[0].print();
  array[1].print();
}
只有一腔孤勇 2024-10-02 09:09:08

你不能返回两个对象。您只能返回对象的数组或集合。
在您的情况下,您可以将变量作为类字段并在“create”方法中为它们分配一些内容

You can't return two object. You can return only array or collection of objects.
In your case you can make variables as class fields and assign something to them in 'create' method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文