public class TestThis { public static void main(String[] args) { Child c = new Child(); c.info(); } } class Parent { public void info() { System.out.println("info:" + this.toString()); } } class Child extends Parent { public Child() { System.out.println("construct:" + this.toString()); } }
结果:
construct: Child@2260e277
info: Child@2260e277
https://my.oschina.net/mlongbo/blog/90047
package com.if1; class Parent { public void print() { System.out.println("Parent."); } public void info() { print(); // == this.print(); } } class Child extends Parent { public void print() { System.out.println("Child."); } } public class TestThis { public static void main(String[] args) { Child c = new Child(); c.info(); } }
结果:Child.
《java编程思想》:编译器会“偷偷的”将this关键字传入进去,所以在设计继承时,要特别注意。
“this关键字只能在方法内部使用,表示对调用方法的那个对象的引用”