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

后端 未结 7 1507
陌清茗
陌清茗 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:00

    Like many others, I needed to convert a C# project to Java. I did not find a complete solution on the web regarding out and ref modifiers. But, I was able to take the information I found, and expand upon it to create my own classes to fulfill the requirements. I wanted to make a distinction between ref and out parameters for code clarity. With the below classes, it is possible. May this information save others time and effort.

    An example is included in the code below.

    //*******************************************************************************************
    //XOUT CLASS
    //*******************************************************************************************
    public class XOUT
    {
        public XOBJ Obj = null;
    
        public XOUT(T value)
        {
            Obj = new XOBJ(value);
        }
    
        public XOUT()
        {
          Obj = new XOBJ();
        }
    
        public XOUT Out()
        {
            return(this);
        }
    
        public XREF Ref()
        {
            return(Obj.Ref());
        }
    };
    
    //*******************************************************************************************
    //XREF CLASS
    //*******************************************************************************************
    
    public class XREF
    {
        public XOBJ Obj = null;
    
        public XREF(T value)
        {
            Obj = new XOBJ(value);
        }
    
        public XREF()
        {
          Obj = new XOBJ();
        }
    
        public XOUT Out()
        {
            return(Obj.Out());
        }
    
        public XREF Ref()
        {
            return(this);
        }
    };
    
    //*******************************************************************************************
    //XOBJ CLASS
    //*******************************************************************************************
    /**
     *
     * @author jsimms
     */
    /*
        XOBJ is the base object that houses the value. XREF and XOUT are classes that
        internally use XOBJ. The classes XOBJ, XREF, and XOUT have methods that allow
        the object to be used as XREF or XOUT parameter; This is important, because
        objects of these types are interchangeable.
    
        See Method:
           XXX.Ref()
           XXX.Out()
    
        The below example shows how to use XOBJ, XREF, and XOUT;
        //
        // Reference parameter example
        //
        void AddToTotal(int a, XREF Total)
        {
           Total.Obj.Value += a;
        }
    
        //
        // out parameter example
        //
        void Add(int a, int b, XOUT ParmOut)
        {
           ParmOut.Obj.Value = a+b;
        }
    
        //
        // XOBJ example
        //
        int XObjTest()
        {
           XOBJ Total = new XOBJ<>(0);
           Add(1, 2, Total.Out());    // Example of using out parameter
           AddToTotal(1,Total.Ref()); // Example of using ref parameter
           return(Total.Value);
        }
    */
    
    
    public class XOBJ {
    
        public T Value;
    
        public  XOBJ() {
    
        }
    
        public XOBJ(T value) {
            this.Value = value;
        }
    
        //
        // Method: Ref()
        // Purpose: returns a Reference Parameter object using the XOBJ value
        //
        public XREF Ref()
        {
            XREF ref = new XREF();
            ref.Obj = this;
            return(ref);
        }
    
        //
        // Method: Out()
        // Purpose: returns an Out Parameter Object using the XOBJ value
        //
        public XOUT Out()
        {
            XOUT out = new XOUT();
            out.Obj = this;
            return(out);
        }
    
        //
        // Method get()
        // Purpose: returns the value
        // Note: Because this is combersome to edit in the code,
        // the Value object has been made public
        //
        public T get() {
            return Value;
        }
    
        //
        // Method get()
        // Purpose: sets the value
        // Note: Because this is combersome to edit in the code,
        // the Value object has been made public
        //
        public void set(T anotherValue) {
            Value = anotherValue;
        }
    
        @Override
        public String toString() {
            return Value.toString();
        }
    
        @Override
        public boolean equals(Object obj) {
            return Value.equals(obj);
        }
    
        @Override
        public int hashCode() {
            return Value.hashCode();
        }
    }
    

提交回复
热议问题