在android中将引用作为参数传递

发布于 2025-01-06 06:53:10 字数 313 浏览 2 评论 0原文

我是java/android新手。我是 ac/c++ 开发人员。我可以知道如何在 android 中传递引用作为参数吗?下面显示的说明性 C 示例代码

void main()
{
  int no1 = 3, no2 = 2, sum = 0;
  findsum( no1, no2, sum );
  printf("sum=%d", sum );
}

void findsum( int no1, int no2, int& sum )
{
  sum = no1 + no2;
}

请建议我一个解决方案,

谢谢

I am newbie in java/android. I am a c/c++ developer. May i know how to pass a reference as parameter in android. An illustrative c sample code shown below

void main()
{
  int no1 = 3, no2 = 2, sum = 0;
  findsum( no1, no2, sum );
  printf("sum=%d", sum );
}

void findsum( int no1, int no2, int& sum )
{
  sum = no1 + no2;
}

please suggest me a solution

thanks

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

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

发布评论

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

评论(3

就像说晚安 2025-01-13 06:53:10

您不能在 Java 中传递 int 作为引用。 int 是主要类型,它只能按值传递。

如果您仍然需要传递 int 变量作为引用,您可以将其包装在可变类中,例如 int 数组:

void findsum( int no1, int no2, int[] sum )
{
  sum[0] = no1 + no2;
}

无论如何,我强烈建议您重构代码以更加面向对象,例如:

class SumOperation {
   private int value;

   public SumOperation(int no1, int no2) {
      this.value = no1 + no2;
   }

   public int getReturnValue() { return this.value; }
}

You cannot pass an int as reference in Java. int is a primary type, it can be passed only by value.

If you still need to pass an int variable as reference you can wrap it in a mutable class, for example an int array:

void findsum( int no1, int no2, int[] sum )
{
  sum[0] = no1 + no2;
}

Anyway, I strongly suggest you to refactor your code to be more object oriented, for example:

class SumOperation {
   private int value;

   public SumOperation(int no1, int no2) {
      this.value = no1 + no2;
   }

   public int getReturnValue() { return this.value; }
}
通知家属抬走 2025-01-13 06:53:10

Java 中没有引用传递。

There is no pass by reference in Java.

等待圉鍢 2025-01-13 06:53:10

这就是我解决这个问题的方法:

// By using a wrapper class.
// This also works with Class objects.
class IntReturn {
    public int val;
}

// For example:
class StringReturn {
    public String val;
}

class Main {
    public static void main (){
        IntReturn iRtn = new IntReturn();
        
        if(tryAdd(2, 3, iRtn)){
            System.out.println("tryAdd(2, 3): " + iRtn.val);
        }
    }

    public static boolean tryAdd(final int a, final int b, final IntReturn iRtn){
        iRtn.val = a + b;

        return true;  // Just something to use return
    }
}

我有一个用于此目的的此类类型的个人库。 Lambda 是这些类的另一个原因,因为您需要一个“最终”变量才能从 Lambda 中获取值,而不是通过 return 语句。

更新:
我已经替换了所有特定的包装类
使用单个泛型类:

public final class Ref<T>
{
    /**
     * The object being held.
     */
    public T val;

    /**
     * This constructor is <b>private</b> to prevent instantiation by external
     * clients.
     * <p>
     * This class should <i>only</i> be instantiated through one of the factory
     * methods:
     * <ul>
     * <li>{@link  #val()}</li>
     * <li>{@link  #val(Object) val(T val)}</li>
     * </ul>
     */
    private Ref()
    {
        this.val = null;
    }

    /**
     * This factory method instantiates an empty Ref object.
     *
     * @param <T> type of object.
     *
     * @return a new instance of the Ref class.
     */
    public static <T> Ref<T> val()
    {
        return new Ref<>();
    }

    /**
     * This factory method instantiates a Ref object containing {@code val}.
     *
     * @param <T> type of object.
     * @param val the object to be held
     *
     * @return a new instance of the Ref class, initialized with the 'val'
     *         object.
     */
    public static <T> Ref<T> val(T val)
    {
        Ref<T> rtn = new Ref<>();
        rtn.val = val;
        return rtn;
    }

    /**
     * Reset 'val' to {@code null}.
     */
    public void clear()
    {
        this.val = null;
    }

    /**
     * Check to see if this Ref object has not yet been set to a value.
     *
     * @return {@code true} if it hasn't been set to a value, {@code false}
     *         otherwise.
     */
    public boolean isEmpty()
    {
        return this.val == null;
    }

    /**
     * Check to see if this Ref object has been set to a value.
     *
     * @return {@code true} if it has been set to a value, {@code false}
     *         otherwise.
     */
    public boolean isPresent()
    {
        return this.val != null;
    }

    /**
     * Returns a string representation of the object.
     *
     * @implSpec
     * This implementation returns a string consisting of the default conversion
     * to a string, of the object held in {@code val}. This is achieved by
     * calling its {@code toString()} method. If {@code val} is empty, then the
     * empty string is returned: "".
     *
     * @return a string representation of the object.
     */
    @Override
    public String toString()
    {
        return "" + this.val;
    }
}

以下是它的使用方法:

 ...
     Ref<Integer> iRtn = Ref.val();

     if(add(2, 3, iRtn)){
         System.out.println("2 + 3 = " + iRtn.val);
     }
 ...

 public boolean add(final int a, final int b, final Ref<Integer> iRtn){
     iRtn.val = a + b;

     return iRtn.val > 0;  // just something to use up return
 }

这似乎是多余的,因为我拥有所有以前开发的包装器类。然而,现在我只有一个可以容纳任何东西的泛型类!

This is how I solved this problem:

// By using a wrapper class.
// This also works with Class objects.
class IntReturn {
    public int val;
}

// For example:
class StringReturn {
    public String val;
}

class Main {
    public static void main (){
        IntReturn iRtn = new IntReturn();
        
        if(tryAdd(2, 3, iRtn)){
            System.out.println("tryAdd(2, 3): " + iRtn.val);
        }
    }

    public static boolean tryAdd(final int a, final int b, final IntReturn iRtn){
        iRtn.val = a + b;

        return true;  // Just something to use return
    }
}

I have a personal library of these types of classes for this purpose. Lambdas are another reason for these classes, as you need to have a 'final' variable to get a value out of a Lambda, other than by return statement.

Update:
I have replaced all of my specific wrapper classes
with a single Generic class:

public final class Ref<T>
{
    /**
     * The object being held.
     */
    public T val;

    /**
     * This constructor is <b>private</b> to prevent instantiation by external
     * clients.
     * <p>
     * This class should <i>only</i> be instantiated through one of the factory
     * methods:
     * <ul>
     * <li>{@link  #val()}</li>
     * <li>{@link  #val(Object) val(T val)}</li>
     * </ul>
     */
    private Ref()
    {
        this.val = null;
    }

    /**
     * This factory method instantiates an empty Ref object.
     *
     * @param <T> type of object.
     *
     * @return a new instance of the Ref class.
     */
    public static <T> Ref<T> val()
    {
        return new Ref<>();
    }

    /**
     * This factory method instantiates a Ref object containing {@code val}.
     *
     * @param <T> type of object.
     * @param val the object to be held
     *
     * @return a new instance of the Ref class, initialized with the 'val'
     *         object.
     */
    public static <T> Ref<T> val(T val)
    {
        Ref<T> rtn = new Ref<>();
        rtn.val = val;
        return rtn;
    }

    /**
     * Reset 'val' to {@code null}.
     */
    public void clear()
    {
        this.val = null;
    }

    /**
     * Check to see if this Ref object has not yet been set to a value.
     *
     * @return {@code true} if it hasn't been set to a value, {@code false}
     *         otherwise.
     */
    public boolean isEmpty()
    {
        return this.val == null;
    }

    /**
     * Check to see if this Ref object has been set to a value.
     *
     * @return {@code true} if it has been set to a value, {@code false}
     *         otherwise.
     */
    public boolean isPresent()
    {
        return this.val != null;
    }

    /**
     * Returns a string representation of the object.
     *
     * @implSpec
     * This implementation returns a string consisting of the default conversion
     * to a string, of the object held in {@code val}. This is achieved by
     * calling its {@code toString()} method. If {@code val} is empty, then the
     * empty string is returned: "".
     *
     * @return a string representation of the object.
     */
    @Override
    public String toString()
    {
        return "" + this.val;
    }
}

Here is how it can be used:

 ...
     Ref<Integer> iRtn = Ref.val();

     if(add(2, 3, iRtn)){
         System.out.println("2 + 3 = " + iRtn.val);
     }
 ...

 public boolean add(final int a, final int b, final Ref<Integer> iRtn){
     iRtn.val = a + b;

     return iRtn.val > 0;  // just something to use up return
 }

It may seem as though this would be redundant since I had all of the previously developed wrapper classes. However, now I have only one generic class that can hold anything!

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