本人现在在看thinking in java,里面有个关于this作用的程序,不是很明白,望指教。
程序如下:
class Person{
public void eat(Apple apple){
Apple peeled = apple.getPeeled();
System.out.println("Yummy");
}
}class Peeler{
static Apple peel(Apple apple){
return apple;
}
}class Apple{
Apple getPeeled(){
return Peeler.peel(this); //this指当前对象
}
}public class PassingThis {
public static void main(String[] args){
new Person().eat(new Apple());
}
}
不太明白this的作用.
程序如下:
class Person{
public void eat(Apple apple){
Apple peeled = apple.getPeeled();
System.out.println("Yummy");
}
}class Peeler{
static Apple peel(Apple apple){
return apple;
}
}class Apple{
Apple getPeeled(){
return Peeler.peel(this); //this指当前对象
}
}public class PassingThis {
public static void main(String[] args){
new Person().eat(new Apple());
}
}
不太明白this的作用.
解决方案 »
- 菜鸟求助:关于I/O和排序
- Java基本数据类型boolean占内存多少?包装类Boolean又占多少?
- 谁能帮下,我SWING写了个双色球的抽奖介面,但不知道怎么处理数据?
- 为什么我安装了jdk1.5+netbeans6.1后,netbeans服务视图中没有服务器?
- 在一个frame上实现多个canvas切换的问题,急!
- 我想实现java连接数据库并且可以执行sql代码,怎么操作呢?
- 请大家帮忙看下这个Date的问题,解决马上给分~
- jar 文件的运行问题
- 高分请教,jTable中如何翻页显示记录
- 郁闷!
- 从数据库读取到的字段怎么输出
- 随机获得数组String s[] = {"gequ1","gequ2","gequ3","gequ4"};中的字符串,要不重复的,分别赋值给单选按钮的4个选项
static Apple peel(Apple apple){
return apple;
}
}里面的peel方法的参数是Apple对象,return Peeler.peel(this); 此处的this正是指向Apple对象,这有什么不明白的
Apple getPeeled(){
return Peeler.peel(this); //this指当前对象
}
}
这个还真找不出比你注释更好的解释了。
静态方法里面不能this但是普通方法里面可以this.静态
String name;
int age;
Parent(String n,int a){
this.name = n;
this.age = a;
}
public static void run(){
System.out.println(" run ");
}}class Child extends Parent{
String sex;
Child(String n,int a,String s){
super(n,a);//相当于执行 Parent(n,a)的{this.name = n;this.age = a;}
this.sex = s;//相当于Child对象.sex = s;
}
public static void main(String args[]){
Child c = new Child("黎明",23,"男");
run();
Parent.run();
}
}