Does Java have something like C#'s ref and out keywords?

后端 未结 7 1530
陌清茗
陌清茗 2020-11-27 03:31

Something like the following:

ref example:

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

void main() {
    String abc = \"abc\";
    chan         


        
7条回答
  •  臣服心动
    2020-11-27 04:01

    Actually there is neither ref nor out keyword equivalent in Java language as far as I know. However I've just transformed a C# code into Java that uses out parameter and will advise what I've just done. You should wrap whatever object into a wrapper class and pass the values wrapped in wrapper object instance as follows;

    A Simple Example For Using Wrapper

    Here is the Wrapper Class;

    public class Wrapper {
        public Object ref1; // use this as ref
        public Object ref2; // use this as out
    
        public Wrapper(Object ref1) {
            this.ref1 = ref1;
        }
    }
    

    And here is the test code;

    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!";
        }
    
    }
    

    A Real World Example

    A C#.NET method using out parameter

    Here there is a C#.NET method that is using out keyword;

    public bool Contains(T value)
    {
        BinaryTreeNode parent;
        return FindWithParent(value, out parent) != null;
    }
    
    private BinaryTreeNode FindWithParent(T value, out BinaryTreeNode parent)
    {
        BinaryTreeNode 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 Equivalent of the C# code that is using the out parameter

    And the Java equivalent of this method with the help of wrapper class is as follows;

    public boolean contains(T value) {
        BinaryTreeNodeGeneration result = findWithParent(value);
    
        return (result != null);
    }
    
    private BinaryTreeNodeGeneration findWithParent(T value) {
        BinaryTreeNode current = head;
        BinaryTreeNode parent = null;
        BinaryTreeNodeGeneration resultGeneration = new BinaryTreeNodeGeneration();
        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 Class

    And the wrapper class used in this Java code is as below;

    public class BinaryTreeNodeGeneration>  {
    
        private BinaryTreeNode   parentNode;
        private BinaryTreeNode   childNode;
    
        public BinaryTreeNodeGeneration() {
            this.parentNode = null;
            this.childNode = null;
        }
    
        public BinaryTreeNode getParentNode() {
            return parentNode;
        }
    
        public void setParentNode(BinaryTreeNode parentNode) {
            this.parentNode = parentNode;
        }
    
        public BinaryTreeNode getChildNode() {
            return childNode;
        }
    
        public void setChildNode(BinaryTreeNode childNode) {
            this.childNode = childNode;
        }
    
    }
    

提交回复
热议问题