引用传递
class Ref1{
String temp="hello";//这个叫做属性,区别之后添加的方法
}
public class refDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Ref1 r1 = new Ref1();
r1.temp = "sef";
System.out.println(r1.temp);
tell(r1);
System.out.println(r1.temp);
}
/**
*
* @param r2
*/
public static void tell(Ref1 r2){
//r2也只是个名字,方法的含义是对源数据进行修改(方法)-因为Ref1并未new一个新对象,
//r2的修改就是对r1的修改,最后r1发生的改变
//---相对应的,复制传入值进行处理可称为(加工)
r2.temp = "Changed";
}
}
引用的传递,相当于使用方法对对源数据修饰,改变原数据的值。
this关键字的使用
表示类中的属性和调用方法
强调本类中的,区别引入的相同的参数名。
调用本类中的构造方法
表示当前对象
class Ref1{
String temp="hello";
public Ref1(String temp) {
this();//放在首行,代表调用Ref1()无参数的默认构造函数
this.temp = temp;//this代表调用本类的属性-hello
System.out.println("Ref1(int temp)");
}
public Ref1(){
System.out.println("Call Ref1()");
}
public String print(){
return "Call print()";
}
public String finalPrint(){
return ("Call FinalPrint() also Call " + this.print());//强调类中的方法
}
}
static关键字的使用
static声明全局属性,达到属性公用的目的;
直接通过类名调用(比较好);
只能访问static声明的属性和方法,作用域的问题;