问个比较初级的问题,super与this的区别?

hellen_99010 2011-11-15 08:14:56
  java里super指向的是父类的实例,this指向的是当前的实例对象。

public class Test extends Date {
public static void main(String[] args) {
new Test().test();
}
public void test(){
System.out.println(super.getClass().getName());
// System.out.println(getClass().getSuperclass().getName());
}
}

我上面的程序打印出来的结果为什么是cn.com.Test.Test,而不是父类的名字。我知道Object中getClass()方法是final的。也就是不能继承重写的。可是出来的结果为什么是当前类的类名。。。求解。。。。
...全文
199 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
hellen_99010 2011-11-16
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 fyswords 的回复:]

从效果来看,getClass()返回的就是调用类的类型啊,你用Test类对象执行返回的就是Test类

getClass()又不是static的,只能在对象实体里调用,不管你super到哪层,就是super到了Object也是这个对象在调用
[/Quote]
我知道运行结果是这样,Api里也是这么说的。可是为什么是这样呢?能详细解析下嘛。VM中执行过程是怎样的?
fyswords 2011-11-16
  • 打赏
  • 举报
回复
从效果来看,getClass()返回的就是调用类的类型啊,你用Test类对象执行返回的就是Test类

getClass()又不是static的,只能在对象实体里调用,不管你super到哪层,就是super到了Object也是这个对象在调用
hellen_99010 2011-11-16
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 fyswords 的回复:]

在Object类源代码里,getClass()是final native的
Java code
public final native Class<?> getClass();


也就是说,所有类的getClass()都是一样的,返回值是调用getClass()方法对象实例的类型。具体实现代码不是java的,不知道sun给藏哪儿了。
[/Quote]
这问题纠结了我很久,期待有人能详细解析下。。。期待。。。。。。。。。。。
jtops 2011-11-16
  • 打赏
  • 举报
回复
父类和本来
Aquarius_T 2011-11-16
  • 打赏
  • 举报
回复
public class Test extends Date {
public static void main(String[] args) {
new Test().test();
}

你子类的方法把父类的方法重写了,当然调用的是子类的方法、。
Aquarius_T 2011-11-16
  • 打赏
  • 举报
回复
this
使用this关键字引用成员变量。
使用this关键字在自身构造方法内部引用其他构造方法。
使用this关键字代表自身类的对象。
使用this关键字引用成员方法



super
在子类的构造方法内部引用父类的构造方法。
在子类中调用父类中的成员方法。
在子类中调用父类中的成员变量。
Aquarius_T 2011-11-16
  • 打赏
  • 举报
回复
this表示当前对象,也就是当前类对象,super表示当前类的父类。


举例:你定义一个新的类:A,这个A继承了类B,也就是说B是A的父类。那么如果A中 有个方法:aa();B中也有个方法: aa();

那么在A 中用this.aa()调用的就是A中定义的方法,而super.aa()调用的就是A的父类B中定义的方法aa();
fyswords 2011-11-16
  • 打赏
  • 举报
回复
在Object类源代码里,getClass()是final native的
public final native Class<?> getClass();


也就是说,所有类的getClass()都是一样的,返回值是调用getClass()方法对象实例的类型。具体实现代码不是java的,不知道sun给藏哪儿了。
hellen_99010 2011-11-16
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 mr_iori 的回复:]

http://blog.csdn.net/tts_kevin/article/details/6717108
[/Quote]
结果是这个,我现在想弄明白为什么super.getClass().getName()得出来的是当前类的名字。而不是父类的名字。最好有人能解析出运行时super在VM中的状态是怎样的?
  • 打赏
  • 举报
回复
super是调用父类成员,this调用子类成员。。
fyswords 2011-11-16
  • 打赏
  • 举报
回复
super.getClass().getName()调用的是父类Date类中的getClass().getName()方法,你在Test类中没重写过getClass(),所以加不加super在这里执行效果都一样,都是找父类。

你执行这条语句的对象是Test类型的,也就是一个Test实例在getClass(),当然结果是Test。

只有父类对象执行getClass().getName()才是得到父类的类型,或者getClass()是static的可以Date.getClass().getName()
Pets_NarSha 2011-11-16
  • 打赏
  • 举报
回复
this:属性表示的是本类中的属性,如果在子类中找不到熟悉则会从父类中继续查找;this.方法名()表示调用本类中的方法,如果找不到则在父类中查找;可以调用本类中的其他构造方法,但是要求至少有一个构造方法是没有this(),作为调用的出,放在调用构造时的首行;this表示当前对象。

super:表示直接找到父类中的属性;从子类直接找到父类中被子类覆写过的方法;从子类调用父类的构造方法,子类不管如何编写代码,则肯定要先调用父类的构造;调用构造时,要放在子类构造方法首行。

hellen_99010 2011-11-16
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 zhangpeng9886 的回复:]

在网上看到有人这样说,super表示在子类中调用父类的方法或属性而已,楼主可以试一下这个输出:
public void test(){
System.out.println(this.getClass());
System.out.println(super.getClass());
}
输出的结果都是当前实体类Test
System.out.println(supe……
[/Quote]
this.getClass(),super.getClass()方法输出是当前的类Test.就你所说的,是多态的解析。可是多态是要方法重写。Object中getClass()是final的。是不允许重写的。就是这两方法调用的都是Object中的getClass()方法。你这解析还是解析不了为什么super.getClass()输出的是当前类
zhangpeng9886 2011-11-15
  • 打赏
  • 举报
回复
在网上看到有人这样说,super表示在子类中调用父类的方法或属性而已,楼主可以试一下这个输出:
public void test(){
System.out.println(this.getClass());
System.out.println(super.getClass());
}
输出的结果都是当前实体类Test
System.out.println(super.getClass());调用父类Date类中的getclass()方法,Date类Object的子类,Date中的getClass()是Object中的,运行时期的实例是Test类,所以输出的依然是Class Test
System.out.println(this.getClass()); //调用Test类中的getclass()方法,此方法从Date继承的,Date从Object继承的,运行时期的实例是Test类,所以输出的是Class Test
Mr_iori 2011-11-15
  • 打赏
  • 举报
回复
http://blog.csdn.net/tts_kevin/article/details/6717108
良才2015 2011-11-15
  • 打赏
  • 举报
回复
super调用的是父类的方法,属性等。
this是本类的属性,方法等。
就这样
hllfl 2011-11-15
  • 打赏
  • 举报
回复
调用父类,this 当前对象~
jx1246080675 2011-11-15
  • 打赏
  • 举报
回复
把所有的代码都贴出来看看、
jx1246080675 2011-11-15
  • 打赏
  • 举报
回复
super表示的是从子类调用父类中的指定操作:比如:调用属性、方法、构造等,
this、和super的区别是:
this super
属性访问: 调用本类中的属性,如果本 访问父类中的属性
类中没有此属性则在父类中继续
查找。
方法: 访问本类中的方法,如果本类 直接访问父类中的方法
中没有此方法,则在父类中继续查找

调用构造: 调用本类构造方法,必须放在构造 调用父类构造、必须放在子类构造方法的首行
方法首行

特殊: 表示当前对象 无此概念
ios初级代码 1.oc中的set和get方法 1>.首先NewFile创建类,选iOS中的Cocoa Touch,再点击Objective-C class,输入类名Student,作为NSobject(系统 自带)的子类 2>.在.h中做方法的声明 在Student.h中: //@interface代表声明一个类 // : 代表继承 #import @interface Student : NSObject{ //成员变量要定义在此{}中 int age; int no; } //age no 的get,set方法 //-代表调用动态方法,+代表静态方法 -(void)setAge:(int)newAge andNo:(int)newNo; //方法名是setage: andNo: 注:冒号也是方法名的一部分 -(int)age; //方法名是age -(int)no; @end 在Student.m中: #import "Student.h" @implementation Student - (int)age{ return age; } - (int)no{ return no; } - (void)setAge:(int)newAge andNo:(int)newNo { age=newAge; age=newNo; //self.age = newAge; //这是个错误的方法会导致死循环,相当于[self setAge] } @end 在main.m中: #import int main(int argc,const char * argv[]) { @autoreleasepool{ //创建对象,alloc方法,init方法都是继承NSlog Student *stu=[[student alloc] init]; //释放方法 [stu release] [stu setAge:20 andNo:3] NSLog(@"this age is:i% andNo:i%",[stu age],[stu no]) } return 0; } 2.点语法 以1为实例: stu.age = 10; //编译的时候调用的是:[stu setAge:10]; int a = stu.age; //编译的时候调用的是:[stu age]; 注:1>oc中的点不是成员变量的访问,是调用set,get方法 2>成员变量默认是@protected 3>当成员变量为@public时,外部直接访问用stu->name; 3.构造方法和description方法 1>.在student.h中 #import @interface student:NSObject{ //默认的是@protected int _age; int _no; } - (void)setAge:(int)age; - (void)setNo:(int)no; - (int)age; - (int)no; //自己写构造方法 - (id)initWithAge:(int)age andNo:(int)no; @end 2>.在student.m中 #import "student.h" @implementation student -(void)setAge:(int)age{ _age = age; } - (void)setNo:(int)no{ _no = no; } - (int)age{ return _age; } -(int)no{ return _no; } //实现构造方法 - (id)initWithAge:(int)age andNo:(int) no{ //首先要调用super的构造方法 //self = [super init]; //如果self不为nil if(self = [super init]){ _age = age; _no = no; } return self; } //重写父类的的description方法 //当使用%@带打印一个对象时候,会调用这个方法 - (NSString *)description{ NSString *str = [NSString stringWithFormat:@"age is %i and no is %i",_age,_no]; return str; } @end 3>.在main.m中: #import int main(int argc,const char * argv[]) { @autoleasepool{ student *stu = [[student alloc] initWithAge:100 andNo:20]; [stu release] //NSLog(@"this age is:i% this no is:i%",stu.age,stu.no); //%@代表打印一个oc对象 NSLog(@"%@",stu); } }

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧