Soweit ich weiß, gibt es in der Java- Sprache weder ein ref noch ein out- Schlüsselwortäquivalent . Ich habe jedoch gerade einen C # -Code in Java umgewandelt , der out- Parameter verwendet und berät, was ich gerade getan habe. Sie sollten jedes Objekt in eine Wrapper-Klasse einschließen und die in der Wrapper-Objektinstanz eingeschlossenen Werte wie folgt übergeben.
Ein einfaches Beispiel für die Verwendung von Wrapper
Hier ist die Wrapper-Klasse ;
public class Wrapper {
public Object ref1; // use this as ref
public Object ref2; // use this as out
public Wrapper(Object ref1) {
this.ref1 = ref1;
}
}
Und hier ist der Testcode;
public class Test {
public static void main(String[] args) {
String abc = "abc";
changeString(abc);
System.out.println("Initial object: " + abc); //wont print "def"
Wrapper w = new Wrapper(abc);
changeStringWithWrapper(w);
System.out.println("Updated object: " + w.ref1);
System.out.println("Out object: " + w.ref2);
}
// This won't work
public static void changeString(String str) {
str = "def";
}
// This will work
public static void changeStringWithWrapper(Wrapper w) {
w.ref1 = "def";
w.ref2 = "And this should be used as out!";
}
}
Ein Beispiel aus der realen Welt
AC # .NET-Methode mit out-Parameter
Hier gibt es eine C # .NET- Methode, die das Schlüsselwort out verwendet .
public bool Contains(T value)
{
BinaryTreeNode<T> parent;
return FindWithParent(value, out parent) != null;
}
private BinaryTreeNode<T> FindWithParent(T value, out BinaryTreeNode<T> parent)
{
BinaryTreeNode<T> current = _head;
parent = null;
while(current != null)
{
int result = current.CompareTo(value);
if (result > 0)
{
parent = current;
current = current.Left;
}
else if (result < 0)
{
parent = current;
current = current.Right;
}
else
{
break;
}
}
return current;
}
Java Entspricht dem C # -Code, der den Parameter out verwendet
Das Java- Äquivalent dieser Methode mit Hilfe der Wrapper-Klasse lautet wie folgt:
public boolean contains(T value) {
BinaryTreeNodeGeneration<T> result = findWithParent(value);
return (result != null);
}
private BinaryTreeNodeGeneration<T> findWithParent(T value) {
BinaryTreeNode<T> current = head;
BinaryTreeNode<T> parent = null;
BinaryTreeNodeGeneration<T> resultGeneration = new BinaryTreeNodeGeneration<T>();
resultGeneration.setParentNode(null);
while(current != null) {
int result = current.compareTo(value);
if(result >0) {
parent = current;
current = current.left;
} else if(result < 0) {
parent = current;
current = current.right;
} else {
break;
}
}
resultGeneration.setChildNode(current);
resultGeneration.setParentNode(parent);
return resultGeneration;
}
Wrapper-Klasse
Die in diesem Java-Code verwendete Wrapper-Klasse lautet wie folgt.
public class BinaryTreeNodeGeneration<TNode extends Comparable<TNode>> {
private BinaryTreeNode<TNode> parentNode;
private BinaryTreeNode<TNode> childNode;
public BinaryTreeNodeGeneration() {
this.parentNode = null;
this.childNode = null;
}
public BinaryTreeNode<TNode> getParentNode() {
return parentNode;
}
public void setParentNode(BinaryTreeNode<TNode> parentNode) {
this.parentNode = parentNode;
}
public BinaryTreeNode<TNode> getChildNode() {
return childNode;
}
public void setChildNode(BinaryTreeNode<TNode> childNode) {
this.childNode = childNode;
}
}