程序中对象传递与普通变量的区别

在Javascript PHP Java程序开发语言中,对象传递都是传其引用,而并非像普通类型的变量传递其值。 在Javascript中,如下:
<script type="text/javascript">
var student1 = {
		name : 123
};

var student2 = student1;
student1.name = 234;
alert(student2.name);  //alert 234 
</script>
在PHP中,如下:
<?php
// example one
class Student{
	public $age = 0;
}
$student1 = new Student();
$student1->age = 2;
fun($student1);
echo $student1->age;  //echo 3;

function fun($obj){
	$obj->age = 3;
}

//example two
$student2 = $student1;
$student2->age = 10;
echo $student1->age;    //echo 10
?>
在Java中,如下:
public class  Student{
	public int age = 0;
	public static void main(String[] args) {
		Student student1 = new Student();
		Student student2 = student1;
		student1.age = 10;
		System.out.print(student2.age);  //print 10
	}
}
4
Mar 2012
AUTHOR WiFeng
CATEGORY Web
COMMENTS 1 Comment

仅有一条评论 »

  1. Ander Ander

    - -! C语言所谓的指针罢了。。。其实叫什么不重要,重要的是理解。

添加新评论 »

   点击刷新验证码