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指向最外面的对象