Java多态内存分析

HttpServlet.java

package cn.bjsxt.oop.polymorphism.myServlet;

public class HttpServlet {
	public void service(){
		System.out.println("HttpServlet.service()");
		this.doGet();
	}
	
	public void doGet(){
		System.out.println("HttpServlet.doGet()");
	}
	
}

MyServlet.java

package cn.bjsxt.oop.polymorphism.myServlet;

public class MyServlet extends HttpServlet {
	
	public void doGet(){
		System.out.println("MyServlet.doGet()");
	}
	
}

Test.java

package cn.bjsxt.oop.polymorphism.myServlet;

public class Test {
	public static void main(String[] args) {
		HttpServlet s = new MyServlet();
		s.service();
	}
}

输出结果:

HttpServlet.service()

MyServlet.doGet()

内存分析如下:

图片

其中this指向最外面的对象

posted @ 2018-04-30 16:48  pallcard  阅读(211)  评论(0编辑  收藏  举报