相关疑难解决方法(0)

Java有像C#的ref和out关键字吗?

类似于以下内容:

参考示例:

void changeString(ref String str) {
    str = "def";
}

void main() {
    String abc = "abc";
    changeString(ref abc);
    System.out.println(abc); //prints "def"
}
Run Code Online (Sandbox Code Playgroud)

例子:

void changeString(out String str) {
    str = "def";
}

void main() {
    String abc;
    changeString(out abc);
    System.out.println(abc); //prints "def"
}
Run Code Online (Sandbox Code Playgroud)

c# java

106
推荐指数
5
解决办法
10万
查看次数

Java Practice:返回作为参数传递的相同对象

在下面的代码中,updateWithContex返回作为参数的相同对象是否真的是不好的做法?

class SomeClass{
   Foo updateWithContex(Foo foo){
       foo.setAppId(i);
       foo.setXId(index);
       //.....
       return foo;
   }
}

class Foo{

    public void setAppId(int appId)
    {
       //
    }
    public void setXId(int appId)
    {
        //
    }
    public void changeState(X x)
    {
       //
    }
}
Run Code Online (Sandbox Code Playgroud)

在C++中,我看到过这样的代码:

 BigObject&
   fastTransform( BigObject& myBO )
   {
      // When entering fastTransform(), myBO is the same object as the function
      // argument provided by the user. -> No copy-constructor is executed.
      // Transform myBO in some way
      return myBO;   // Transformed myBO …
Run Code Online (Sandbox Code Playgroud)

java

8
推荐指数
1
解决办法
6530
查看次数

标签 统计

java ×2

c# ×1