java面向对象:引用传递

来源:互联网 发布:无线优化工程师 编辑:程序博客网 时间:2024/05/06 17:52
引用传递:package bao;////////引用传递案例:
class Ref{       //名称 最好大写开头
int temp=10;//声明变量,并初始化
}
public class refdemo {
  public static void main(String[] args) {
 Ref ref;         //主方法调用temp使用
 ref=new Ref(); //声明,并创建Ref对象,再对其对象进行实例化操作
             //或者合为一步:Ref ref=new  Ref();
  ref.temp=15;//通过对象调用属性执行,并重新给变量赋值
  System.out.println(ref.temp);
  tell(ref );//调用tell执行
  System.out.println(ref.temp);
  //  System.out.println(r2.temp);此语句出现错误,
}                                     
public static void tell(Ref r2) {//声明一个tell方法
r2.temp=30;//通r2把变量值修改为30 ,就是在此步奏之前r2为30

} }

例子2:package bao;


public class refdemo01 {


/**
* @param args
*/
public static void main(String[] args) {
String str1="hello";
System.out.println(str1);
tell(str1); 
System.out.println(str1);
}
   public static void  tell(String str2) {
str2="jike";
}--------------------------------显示:hello hello----------为何?                            ------------------------string类型数据不可更改!

0 0
原创粉丝点击