Soon after learning Java, I ran into a problem today. I needed to modify the value of the incoming object in the method. To be precise, I needed to use a method to create an object and return its reference. My
first reaction was familiar with C #. It is the ref / out keyword in C #. It turns out that there is no similar keyword in Java, so I can only think about how to solve this problem.
Parameter passing:
There are two types of parameter passing in the method, one is value passing and the other is Reference transfer, but in fact copy transfer.
Value transfer: It is to copy the copy of the data itself, and operate it in the incoming method, and the value is copied.
Reference transfer: It is to copy a copy of a reference to an object, and the object can be operated by this reference in the incoming method.
So as far as my current problem is concerned, I need a method. I pass in a reference and create the required object in the method.
Then in Java, I can only encapsulate the current object in a layer (define a new class), Make the object that needs to be created a member of the new class, and assign a value to the member in the method
Example:
C #:
/// <summary>
/// model
/// </ summary>
public class Student
{
public string Name;
public int Age;
}
/// Method:
/// <summary>
/// Error Demonstration
///
/// The reference of student is copied and created to create a new corresponding Student
/// Assign the reference to the student. When the method returns to the stack, the student reference returns to the front of the stack.
/// Then the student object reference number is 0, waiting for the GC, the reference student is still the original value (logical address)
/// < / summary>
/// <param name = "student"> </ param>
static void createStudent (Student student)
{
student = new Student {Name = "Stephen Lee", Age = 1};
}
/// <summary>
/// Correct practice
////
/// Transfer the operation right to the method through the ref keyword
/// </ summary>
/// <param name = "student"> </ param>
static void createStudent (ref Student student)
{
student = new Student {Name = "Stephen Lee", Age = 1};
}
// Client
static void Main (string [] args)
{
Console.WriteLine ("error demonstration");
Student student1 = null;
createStudent (student1);
if (student1 == null)
Console.WriteLine ("Failed to create object");
else
Console.WriteLine ("Successfully created object, Name:" + student1.Name);
Console.WriteLine ("Correct Practice");
Student student2 = null;
createStudent (ref student2);
if (student2 == null)
Console.WriteLine ("Failed to create object");
else
Console.WriteLine ("Successfully created object, Name:" + student2.Name);
}
Java:
/ ** Model
* @author Stephen
*
* /
public class Student
{
public String Name;
public int Age;
public Student (String name, int age)
{
this.Name = name;
this.Age = age;
}
}
/** 错误示范,原因同C#
* @param student
*/
private void createStudent(Student student)
{
student = new Student("Stephen Lee",1);
}
/** 正确做法
* @param studentPack
*/
private void createStudent(StudentPack studentPack)
{
studentPack.student = new Student("Stephen Lee",1);
}
/** 包装器
* @author Stephen
*
*/
public class StudentPack
{
public Student student;
}
// Client
StudentPack studentPack = new StudentPack();
createStudent(studentPack);
System.out.println(studentPack.student.Name);
Alternative C # ref / out keyword in Java
Guess you like
Origin www.cnblogs.com/aquariusunny/p/12729805.html
Recommended
Ranking