असल में वहाँ न तो है रेफरी और न ही बाहर में कीवर्ड बराबर जावा भाषा के रूप में तक मुझे पता है। हालाँकि मैंने अभी जावा में C # कोड को बदल दिया है जो पैरामीटर का उपयोग करता है और सलाह देगा कि मैंने अभी क्या किया है। आपको रैपर क्लास में किसी भी वस्तु को लपेटना चाहिए और निम्न प्रकार के रैपर ऑब्जेक्ट में लिपटे हुए मूल्यों को पास करना चाहिए;
रैपर का उपयोग करने के लिए एक सरल उदाहरण
यहाँ रैपर्स क्लास है ;
public class Wrapper {
public Object ref1; // use this as ref
public Object ref2; // use this as out
public Wrapper(Object ref1) {
this.ref1 = ref1;
}
}
और यहाँ परीक्षण कोड है;
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!";
}
}
एक वास्तविक विश्व उदाहरण
पैरामीटर का उपयोग करके एसी # .NET विधि
यहाँ एक C # .NET विधि है जो कीवर्ड का उपयोग कर रही है ;
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;
}
जावा सी # कोड के समकक्ष जो बाहर पैरामीटर का उपयोग कर रहा है
और रैपर क्लास की मदद से इस विधि के बराबर जावा निम्नानुसार है;
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;
}
रैपर क्लास
और इस जावा कोड में प्रयुक्त रैपर क्लास नीचे है;
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;
}
}