java里面如何在函数的参数中返回结果,不通过返回值?(100分)

  • 主题发起人 wxb761014
  • 开始时间
W

wxb761014

Unregistered / Unconfirmed
GUEST, unregistred user!
这个问题和下面这个问题实际上是等价的。
下面这个函数是交换两个String类型变量的值,但我试了一下,没有用?
public void swap(String a,String b)
{
String c=null;
c=a;
a=b;
b=c;
}
大家给诊断一下是什么问题?
java中怎么实现这类问题,在c和c++里面可用用指针,可java里我是束手无策。
本人刚刚开始用java,希望大家指教。
 
public void swap(out String a,out String b)
{
String c=null;
c=a;
a=b;
b=c;
}
 
请大家积极发言,等着急用?
 
to 沙隆巴斯的主人:
编译通不过啊?
java 里面是这么用的吗?
 
大家帮帮忙吧,做不完我是回不了家了。
 
类似的问题,学生要交作业?
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1447779
 
是这样的
java里的方法都是按值传递的
只能通过返回值来返回
或者把类的引用传进来
 
public void swap(String a,String b)
{
String c=a;
a=b;
b=c;
}
如果不行就不行了!
Pass by Value
In Java methods, arguments are passed by value. When invoked, the method receives the value of the variable passed in. When the argument is of primitive type, pass-by-value means that the method cannot change its value. When the argument is of reference type, pass-by-value means that the method cannot change the object reference, but can invoke the object's methods and modify the accessible variables within the object.
This is often the source of confusion--a programmer writes a method that attempts to modify the value of one its arguments and the methoddo
esn't work as expected. Let's look at such method and then
investigate how to change it so that itdo
es what the programmer originally intended.
Consider this series of Java statements which attempts to retrieve the current color of a Pen object in a graphics application:
. . .
int r = -1, g = -1, b = -1;
pen.getRGBColor(r, g, b);
System.out.println("red = " + r +
", green = " + g +
", blue = " + b);
. . .
At the time when the getRGBColor method is called, the variables r, g, and b all have the value -1. The caller is expecting the getRGBColor method to pass back the red, green and blue values of the current color in the r, g, and b variables.
However, the Java runtime passes the variables' values (-1) into the getRGBColor method;
not a reference to the r, g, and b variables. So you could visualize the call to getRGBColor like this: getRGBColor(-1, -1, -1).
When control passes into the getRGBColor method, the arguments come into scope (get allocated) and are initialized to the value passed into the method:
class Pen {
int redValue, greenValue, blueValue;
void getRGBColor(int red, int green, int blue) {
// red, green, and blue have been created
// and their values are -1
. . .
}
}
So getRGBColor gets access to the values of r, g, and b in the caller through its arguments red, green, and blue, respectively. The method gets its own copy of the values to use within the scope of the method. Any changes made to those local copies are not reflected in the original variables from the caller.
Now, let's look at the implementation of getRGBColor within the Pen class that the method signature above implies:
class Pen {
int redValue, greenValue, blueValue;
. . .
// this methoddo
es not work as intended
void getRGBColor(int red, int green, int blue) {
red = redValue;
green = greenValue;
blue = blueValue;
}
}
This method will not work as intended. When control gets to the println statement in the following code, which was shown previously, getRGBColor's arguments, red, green, and blue, no longer exist. Therefore the assignments made to them within the method had no effect;
r, g, and b are all still equal to -1.
. . .
int r = -1, g = -1, b = -1;
pen.getRGBColor(r, g, b);
System.out.println("red = " + r +
", green = " + g +
", blue = " + b);
. . .
Passing variables by value affords the programmer some safety: Methods cannot unintentionally modify a variable that is outside of its scope. However, you often want a method to be able to modify one or more of its arguments. The getRGBColor method is a case in point. The caller wants the method to return three values through its arguments. However, the method cannot modify its arguments, and, furthermore, a method can only return one value through its return value. So, how can a method return more than one value, or have an effect (modify some value) outside of its scope?
For a method to modify an argument, it must be of a reference type such as an object or array. Objects and arrays are also passed by value, but the value of an object is a reference. So the effect is that arguments of reference types are passed in by reference. Hence the name. A reference to an object is the address of the object in memory. Now, the argument in the method is referring to the same memory location as the caller.
Let's rewrite the getRGBColor method so that it actuallydo
es what you want. First, you must introduce a new type of object, RGBColor, that can hold the red, green and blue values of a color in RGB space:
class RGBColor {
public int red, green, blue;
}
Now, we can rewrite getRGBColor so that it accepts an RGBColor object as an argument. The getRGBColor method returns the current color of the pen by setting the red, green and blue member variables of its RGBColor argument:
class Pen {
int redValue, greenValue, blueValue;
void getRGBColor(RGBColor aColor) {
aColor.red = redValue;
aColor.green = greenValue;
aColor.blue = blueValue;
}
}
And finally, let's rewrite the calling sequence:
. . .
RGBColor penColor = new RGBColor();
pen.getRGBColor(penColor);
System.out.println("red = " + penColor.red +
", green = " + penColor.green +
", blue = " + penColor.blue);
. . .
The modifications made to the RGBColor object within the getRGBColor method affect the object created in the calling sequence because the names penColor (in the calling sequence) and aColor (in the getRGBColor method) refer to the same object.
 
对象和数组是“引用”的,可以修改内容
基础类型是“传值”的,无法修改内容。
 
String 是恒对象,自然不能更改内容了。
 
String是恒对象!但StringBuffer不是恒对象。可以试一下。
对于之一问题,用合理的设计模式是可以解决的!
//-----------------------------------------
class the_string{
private Sstring;
the_string(String new_string){
Sstring=new_string;
}
public void set_string(String string){
Sstring=string;
}
public string get_string(){
return Sstring;
}
}
class change_string{
public void swap(the_string a,the_string b)
{the_string c=new the_string("");
c.set_string(a.get_string());
a.set_string(b.get_string());
b.set_string(c.get_string());
}
public static void main(String[] args){
the_string a=new the_string("boy");
the_string b=new the_string("girl");
swap(a,b);
System.out.println(a.get_string());
System.out.println(b.get_string());
}
}
 
用对象吧。
 
你们一堆人说了半天,我看没有一个人说道点子上面
最基本的概念是,在 method 里面定义的 a, b 和 swap 函数里面的 a,b虽然是指向同一个
字符串的内存引用,但确有不同的存储地址,
method 里面的a b 是指向字符串的内存引用,而swap方法里面的 a b 是指向相同地址的另
外一份内存应用
swap 函数想影响另外一份内存应用是不可能的,你可以修改引用指向的对象的值,绝对没有
办法修改其它的内存引用
public void method() {
String a = "aaaa";
String b = "bbbb";
swap(a, b);
}
public void swap(String a,String b) {
String c=null;
c=a;
a=b;
b=c;
}
 
传入对象,然后改变对象中的变量。
 
想通过函数的方法修改参数的值是绝对不可能的!
因为哪样你只是修改了参数的值,是传入的参数的一份拷贝,
他们的内存地址是不一样的。
 
就是这样,Java中取消了直接使用指针。所以除了对象和
数组传的是地址,其他基本类型传的都是值,你甭想把它
返回后对值的修改保留。
 
用对象是可以的,因为传入对象时实际是传递对象的引用。
 
这种想法本身就不可取
 

Similar threads

顶部