父类:
public class Father {
private int a = 123;
int b = 234;
protected int c = 345;
public int d = 456;
public void doGet(){
System.out.println("Father.doGet()");
}
public void doPost(){
System.out.println("Father.doPost()");
}
public void service(){
System.out.println("Father.service()");
doGet();
}
}
子类:
public class Child extends Father {
@Override
public void doGet() {
System.out.println("Child.doGet()");
}
@Override
public void doPost() {
System.out.println("Child.doPost()");
}
@Override
public void service() {
// TODO Auto-generated method stub
System.out.println("Child.service()");
super.service();
}
}
测试方法:
Child c = new Child();
c.service();
结果:
Child.service()
Father.service()
Child.doGet()
分析:
当调用子类的方法时,前两个值是Child.service和Father.service(),我们能够理解。
(首先执行子类的service,然后使用super调用父类的service方法。)。
但是最后调用doGet()方法时,却是调用了子类的doGet方法。
这其实是和this指针有关。在调用doGet()方法时,其实是省略了this指针。
如果加上this,那我们再去分析,就会明白此时的this是指向Child的。
那么调用的时候也就是调用了子类的doGet()方法。
分享到:
相关推荐
js中this相关的测试代码
C++面向对象,封装,继承,多态,虚函数,this指针相关知识内容,欢迎下载
《教师用书》则包含录音的书面材料,练习答案和相关文化背景知识的补充读物。 和国内现有的听力教程相比,Listen to This 有以下几个特点: 1.教程的第三册适合英语专业高年级学生使用,以达到教委大纲中第六级...
This demonstration version of this control is a wrapper around the FileSystemObject - and makes using the FSO much easier.
比较齐全的this的用法.this在不同的情况下用法的说明和一些相关的总结。
JavaScript中this指向问题 记得初学 JavaScript 时,其中 this 的指向问题曾让我头疼不已,我还曾私自将其与闭包、原型(原型链)并称 JS 武林中的三大魔头。如果你要想在 JS 武林中称霸一方,必须将这三大魔头击倒...
This is an example of how to use the Common Dialog. This demonstrates most of the event procedures of the CM Dialog control.
再啰嗦一下,js的话用FF调试,在fireBug中能看到很多东西,很方便,当然,偶尔也会见浏览器兼容相关的问题。
内容概要: 本文介绍了Java中的this关键字的用法和作用。读者将学习什么是this关键字以及它在实例方法、构造方法和内部类中的具体应用。...如有疑问,可以参考Java官方文档或搜索相关资料,进一步扩展学习。
This ocx allows you to encrypt strings while utilizing a password shift method.
This is a good example to add your programs’ icon to the Systray. This Zip also includes a helpful example.
This is an example of how to retrieve the system colors and display them on your form.
This is an example of how to return all of the files on a users system. This can be easily edited to be close to the normal Windows’ Find File or Folder Dialog.
This is the source to my OCX. This demontrates how to create a basic OCX. This will aid anyone who wants to create an OCX.
在JavaScript中,this 的概念比较复杂。除了在面向对象编程中,this 还是随处可用的。这篇文章介绍了javascript中的this相关知识,对javascript this相关知识感兴趣的朋友一起学习吧
This is the best example of Visual Basic you can find on the internet about monitoring a folder.
C++中this指针用法详解及实例 概要: 本文简单介绍this指针的基本概念,并通过一个实际例子...this指针的具体类型与具体对象的类型以及对象是否被const关键字修饰 有关。例如,在类Employee的非常量函数中,this指针类
主要介绍了 Android this与Activity.this的区别的相关资料,需要的朋友可以参考下
This is a first for this author. Its an OCX which represents a characters in the form of led bulbs.
JavaScript中this的使用 在JavaScript中this变量是一个令人难以摸清的关键字,this可谓是非常强大,充分了解this的相关知识有助于我们在编写面向对象的JavaScript程序时能够游刃有余。 对于this变量最要的是能够理清...