引用作为参数传递时容易搞错的两个例子
2005-09-07 21:42
281 查看
//例子1:
class Value
{
public int i = 15;
}
public class PassRefrence
{
public static void main(String argv[])
{
PassRefrence t = new PassRefrence();
t.first();
}
public void first()
{
int i = 5;
Value v = new Value();
v.i = 25;
second(v, i);
System.out.println(v.i);
}
public void second(Value v, int i)
{
i = 0;
v.i = 20;
Value val = new Value();
v = val;
System.out.println(v.i + " " + i);
}
}
class Value
{
public int i = 15;
}
public class PassRefrence
{
public static void main(String argv[])
{
PassRefrence t = new PassRefrence();
t.first();
}
public void first()
{
int i = 5;
Value v = new Value();
v.i = 25;
second(v, i);
System.out.println(v.i);
}
public void second(Value v, int i)
{
i = 0;
v.i = 20;
Value val = new Value();
v = val;
System.out.println(v.i + " " + i);
}
}
class Value
{
public int i = 15;
}
public class PassRefrence
{
public static void main(String argv[])
{
PassRefrence t = new PassRefrence();
t.first();
}
public void first()
{
int i = 5;
Value v = new Value();
v.i = 25;
second(v, i);
System.out.println(v.i);
}
public void second(Value v, int i)
{
i = 0;
v.i = 20;
Value val = new Value();
v = val;
System.out.println(v.i + " " + i);
}
}
输出结果为:
15 0
20
输出结果为:
15 0
20参数传到一个方法中后,在方法中对参数值的改变不会影响原来的值,对primitive和object refrence 都一样。对引用来说就是,在方法中又new 了一个对象并把该对象的引用赋给传递进来的参数,这样并不影响方法外的引用所指向的地址。
另外:
//例子2:
class Test
{
public static void main(String [] args)
{
Base b = new Subclass();
System.out.println(b.x);
System.out.println(b.method());
}
}
class Base
{
int x = 2;
int method()
{
return x;
}
}
class Subclass extends Base
{
int x = 3;
int method()
{
return x;
}
}
输出结果为:
2
3
输出结果为:
2
3 The rule for accessing methods and variables in a class hierarchy is as follows:
-the object reference is used when accessing variables (so the value of b.x is therefore 2).
-the underlying object is used when accessing methods (so the method called is actually the method defined in Subclass, and in Subclass, x = 3).
用父类或接口调用子类的覆盖方法时,根据动态绑定确定调用哪个子类的方法,但调用成员变量时,直接访问父类或接口的成员变量
class Value
{
public int i = 15;
}
public class PassRefrence
{
public static void main(String argv[])
{
PassRefrence t = new PassRefrence();
t.first();
}
public void first()
{
int i = 5;
Value v = new Value();
v.i = 25;
second(v, i);
System.out.println(v.i);
}
public void second(Value v, int i)
{
i = 0;
v.i = 20;
Value val = new Value();
v = val;
System.out.println(v.i + " " + i);
}
}
class Value
{
public int i = 15;
}
public class PassRefrence
{
public static void main(String argv[])
{
PassRefrence t = new PassRefrence();
t.first();
}
public void first()
{
int i = 5;
Value v = new Value();
v.i = 25;
second(v, i);
System.out.println(v.i);
}
public void second(Value v, int i)
{
i = 0;
v.i = 20;
Value val = new Value();
v = val;
System.out.println(v.i + " " + i);
}
}
class Value
{
public int i = 15;
}
public class PassRefrence
{
public static void main(String argv[])
{
PassRefrence t = new PassRefrence();
t.first();
}
public void first()
{
int i = 5;
Value v = new Value();
v.i = 25;
second(v, i);
System.out.println(v.i);
}
public void second(Value v, int i)
{
i = 0;
v.i = 20;
Value val = new Value();
v = val;
System.out.println(v.i + " " + i);
}
}
输出结果为:
15 0
20
输出结果为:
15 0
20参数传到一个方法中后,在方法中对参数值的改变不会影响原来的值,对primitive和object refrence 都一样。对引用来说就是,在方法中又new 了一个对象并把该对象的引用赋给传递进来的参数,这样并不影响方法外的引用所指向的地址。
另外:
//例子2:
class Test
{
public static void main(String [] args)
{
Base b = new Subclass();
System.out.println(b.x);
System.out.println(b.method());
}
}
class Base
{
int x = 2;
int method()
{
return x;
}
}
class Subclass extends Base
{
int x = 3;
int method()
{
return x;
}
}
输出结果为:
2
3
输出结果为:
2
3 The rule for accessing methods and variables in a class hierarchy is as follows:
-the object reference is used when accessing variables (so the value of b.x is therefore 2).
-the underlying object is used when accessing methods (so the method called is actually the method defined in Subclass, and in Subclass, x = 3).
用父类或接口调用子类的覆盖方法时,根据动态绑定确定调用哪个子类的方法,但调用成员变量时,直接访问父类或接口的成员变量
相关文章推荐
- 《Advanced Bash-scripting Guide》学习(十三):引用变量的两个例子
- 基本类型和引用类型作为参数传递
- C++ 引用和指针作为函数参数的例子。请不要拍砖
- C++ 引用作为参数传递
- 引用作为函数参数的例子,有很多知识点。
- java笔试陷阱:对象引用作为参数传递时陷阱
- 基本类型和引用类型作为参数传递
- 引用两个jQuery 容易冲突导致失效
- 【Java基础】基本类型的包装类作为参数传递是值传递还是引用传递
- Comparable与Comparator的两个小例子,简单容易理解
- 基本数据类型或者引用数据类型作为方法参数传递
- java (值传递和引用传递) String 和StringBuffer 作为参数传递
- C++常量引用作为函数参数的例子
- java引用类型参数传值的两个很好的例子
- [Topic]指针的引用作为参数传递
- 【漏洞分析】两个例子-数组溢出修改返回函数与strcpy覆盖周边内存地址
- 引用作为函数返回值的情况
- 【C#错误】未能添加对“LoginBLL”的引用,将此项目作为引用添加将导致循环依赖项
- 三个引用两个对象的内存图
- c++中的引用和指针的区别和相关例子