class Demo{
String str = "这是成员变量";
void fun(String str1){
System.out.println(str1);
System.out.println(str);
}
}
public class This{
public static void main(String args[]){
Demo demo = new Demo();
demo.fun("这是局部变量");
}
}
复制代码
2,this关键字把当前对象传递给其他方法
这里有个很经典的例子,就是java编程思想的85页的例子。我们拿出来仔细研究。
复制代码
class Person{
public void eat(Apple apple){
Apple peeled = apple.getPeeled();
System.out.println("Yummy");
}
}
class Peeler{
static Apple peel(Apple apple){
//....remove peel
return apple;
}
}
class Apple{
Apple getPeeled(){
return Peeler.peel(this);
}
}
public class This{
public static void main(String args[]){
new Person().eat(new Apple());
}
}
public class This{
int i = 0;
This increment(){
i += 2;
return this;
}
void print(){
System.out.println("i = " + i);
}
public static void main(String args[]){
This x = new This();
x.increment().increment().print();
}
}