我可以将属性作为"out"或"ref"参数传递,如果不是,为什么不呢?
例如
Person p = new Person();
Run Code Online (Sandbox Code Playgroud)
...
public void Test(out p.Name);
Run Code Online (Sandbox Code Playgroud) package myintergertest;
/**
*
* @author Engineering
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//this one does not increment
Integer n = new Integer(0);
System.out.println("n=" + n);
Increment(n);
System.out.println("n=" + n);
Increment(n);
System.out.println("n=" + n);
Increment(n);
System.out.println("n=" + n);
Increment(n);
//this one will increment
MyIntegerObj myInt = new MyIntegerObj(1);
Increment(myInt);
System.out.println("myint = " + myInt.get());
Increment(myInt);
System.out.println("myint = " + myInt.get());
Increment(myInt);
System.out.println("myint = " + myInt.get()); …Run Code Online (Sandbox Code Playgroud) 可能重复: Java是否通过引用传递?
public class myClass{
public static void main(String[] args){
myObject obj = new myObject("myName");
changeName(obj);
System.out.print(obj.getName()); // This prints "anotherName"
}
public static void changeName(myObject obj){
obj.setName("anotherName");
}
}
Run Code Online (Sandbox Code Playgroud)
我知道Java通过值传递,但为什么它obj在前面的示例中通过引用传递并更改它?
java terminology parameter-passing pass-by-reference pass-by-value
在测试我的代码时,我发现了以下内容:如果我将data.table分配DT1给DT并在DT之后进行DT1更改,则使用它进行更改.所以DT,DT1似乎是内部联系的.这是预期的行为吗?虽然我不是编程专家,但这对我来说是错误的,并且用简单的R变量或a来测试它data.frame,我无法重现这种行为.这里发生了什么事?
DF <- data.frame(ID=letters[1:5],
value=1:5)
DF1 <- DF
all.equal(DF1, DF)
[1] TRUE
DF[1, "value"] <- DF[1, "value"]*2
all.equal(DF1, DF)
[1] "Component 2: Mean relative difference: 1"
library(data.table)
data.table 1.7.1 For help type: help("data.table")
DT <- data.table(ID=letters[1:5],
value=1:5)
DT1 <- DT
all.equal(DT1, DT)
[1] TRUE
DT[, value:=value*2]
ID value
[1,] a 2
[2,] b 4
[3,] c 6
[4,] d 8
[5,] e 10
all.equal(DT1, DT)
[1] …Run Code Online (Sandbox Code Playgroud) Java是按值传递的.你如何修改语言以引入通过引用传递(或一些等效的行为)?
举个例子
public static void main(String[] args) {
String variable = "'previous String reference'";
passByReference(ref variable);
System.out.println(variable); // I want this to print 'new String reference'
}
public static void passByReference(ref String someString) {
someString = "'new String reference'";
}
Run Code Online (Sandbox Code Playgroud)
哪个(没有ref)编译成以下字节码
public static void main(java.lang.String[]);
Code:
0: ldc #2 // String 'previous String reference'
2: astore_1
3: aload_1
4: invokestatic #3 // Method passByReference:(Ljava/lang/String;)V
7: return
public static void passByReference(java.lang.String);
Code:
0: ldc #4 // String …Run Code Online (Sandbox Code Playgroud) java bytecode language-design pass-by-reference bytecode-manipulation
在Ruby中,是否可以通过引用传递带有值类型语义的参数(例如Fixnum)?我正在寻找类似于C#的' ref '关键字的东西.
例:
def func(x)
x += 1
end
a = 5
func(a) #this should be something like func(ref a)
puts a #should read '6'
Run Code Online (Sandbox Code Playgroud)
顺便说一句.我知道我可以使用:
a = func(a)
Run Code Online (Sandbox Code Playgroud) 有没有一种很好的方法可以在不使用磁盘的情况下在两个python子进程之间传递大量数据?这是我希望完成的动画示例:
import sys, subprocess, numpy
cmdString = """
import sys, numpy
done = False
while not done:
cmd = raw_input()
if cmd == 'done':
done = True
elif cmd == 'data':
##Fake data. In real life, get data from hardware.
data = numpy.zeros(1000000, dtype=numpy.uint8)
data.dump('data.pkl')
sys.stdout.write('data.pkl' + '\\n')
sys.stdout.flush()"""
proc = subprocess.Popen( #python vs. pythonw on Windows?
[sys.executable, '-c %s'%cmdString],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
for i in range(3):
proc.stdin.write('data\n')
print proc.stdout.readline().rstrip()
a = numpy.load('data.pkl')
print a.shape
proc.stdin.write('done\n')
Run Code Online (Sandbox Code Playgroud)
这将创建一个子进程,该子进程生成numpy数组并将数组保存到磁盘.然后父进程从磁盘加载数组.有用!
问题是,我们的硬件可以生成比磁盘可读/写快10倍的数据.有没有办法将数据从一个python进程传输到另一个纯内存中,甚至可能没有复制数据?我可以做一些像传递参考的东西吗?
我第一次尝试纯粹在内存中传输数据是非常糟糕的:
import …Run Code Online (Sandbox Code Playgroud) 我决定查看是否为成员分配引用会使成员成为引用.我编写了以下代码片段来测试它.有一个Wrapper带有std::string成员变量的简单类.我const string&在构造函数中取一个并将其分配给public成员变量.后来在main()方法中我修改了成员变量但是string我传给构造函数的方法保持不变,怎么样?我认为在Java中变量会发生变化,为什么不在这段代码中呢?在这种情况下,引用究竟是如何工作的?
#include <iostream>
#include <string>
using namespace std;
class Wrapper
{
public:
string str;
Wrapper(const string& newStr)
{
str = newStr;
}
};
int main (int argc, char * const argv[])
{
string str = "hello";
cout << str << endl;
Wrapper wrapper(str);
wrapper.str[0] = 'j'; // should change 'hello' to 'jello'
cout << str << endl;
}
Run Code Online (Sandbox Code Playgroud) C和C++开发人员使用短语"通过引用传递",但它们似乎用于表示不同的东西.每种语言中这个模棱两可的短语究竟有什么区别?
让我们考虑一个对象foo(可能是一个int,一个double,一个习惯struct,一个class,等等).我的理解是,通过foo引用函数(或只是传递指针foo)来传递更高的性能,因为我们避免制作本地副本(如果foo很大则可能很昂贵).
但是,从这里的答案来看,似乎64位系统上的指针实际上可以预期大小为8字节,无论指向什么.在我的系统上,a float是4个字节.这是否意味着如果foo是类型float,那么仅通过值传递而不是给它指向它是更有效的foo(假设没有其他约束可以使得使用一个比函数内的另一个更有效)?
c++ pointers pass-by-reference pass-by-value pass-by-pointer
c++ ×3
java ×3
terminology ×2
arguments ×1
bytecode ×1
c ×1
c# ×1
constructor ×1
ctypes ×1
data.table ×1
numpy ×1
pointers ×1
python ×1
r ×1
ruby ×1
subprocess ×1