关于Java继承体系中this的表示关系

Java的继承体系中,因为有重写的概念,所以说this在子父类之间的调用到底是谁的方法,或者成员属性,的问题是一个值得思考的问题;

先说结论:如果在测试类中调用的是子父类同名的成员属性,这个this.成员  属性的值就是父类中这个成员属性的值,因为在Java中没有重写成员属性这一概念

,如果调用的是同名的成员方法的话,那就应该调用的是重写后的成员方法.....如果子类中没有这个成员方法,则向父类中寻找.

上代码支持论证:

package Test_This;

public class Demo {
    public static void main(String[] args) {
        Fu f = new Zi();
        
        System.out.println(f.num);//这里定义的是父类,而成员变量没有多态,所以即使你new的子类,依然指向父类的成员变量。
        System.out.println(f.fun1());//不解释了,就是多态。
        f.show();
    }
}

class Fu {
    public String num = "父类成员变量";
    public void show() {
        System.out.println(this.num);//因为成员变量没有多态,所以this指向当前类对象的成员变量。
        System.out.println(this.fun1());//因为方法有多态,所以this指向new对象的方法。
    }
    
    public String fun1() {
        System.out.println(this.num);//因为成员变量没有多态,所以this指向当前类对象的成员变量。
        return "父类调用";
    }
}

class Zi extends Fu {
    public String num = "子类成员变量";
    
    public String fun1() {
        System.out.println(this.num);//因为成员变量没有多态,所以this指向当前类对象的成员变量。
        return "子类调用";
    }
}

运行结果:

以上代码实验结果即可证明其论证;

 

在web开发中可以用这一特性,结合反射来抽取servlet方法

上代码:这个是父类

import java.io.IOException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BaseServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // localhost:8080/Test/productServlet?method=addProduct
        String method = req.getParameter("method");

        if (null == method || "".equals(method) || method.trim().equals("")) {
            method = "execute";
        }

        // 注意:此处的this代表的是子类的对象
        System.out.println("baseservlet this");
        System.out.println(this);
        // 子类对象字节码对象
        Class clazz = this.getClass();

        try {
            // 查找子类对象对应的字节码中的名称为method的方法.这个方法的参数类型是:HttpServletRequest.class,HttpServletResponse.class
            Method md = clazz.getMethod(method, HttpServletRequest.class, HttpServletResponse.class);
            if (null != md) {
                String jspPath = (String) md.invoke(this, req, resp);
                if (null != jspPath) {
                    req.getRequestDispatcher(jspPath).forward(req, resp);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }    
    }

    // 默认方法
    public String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        return null;
    }

}

 

子类:

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.itcast.domain.User;
import cn.itcast.service.IUserService;
import cn.itcast.service.serviceImp.UserServiceImp;
import cn.itcast.utils.MyBeanUtils;
import cn.itcast.utils.UUIDUtils;
import cn.itcast.web.base.BaseServlet;

public class UserServlet extends BaseServlet {
    private static final long serialVersionUID = 1L;
    public String registUI(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        return "/jsp/register.jsp";
    }
    //regist
    public String regist(HttpServletRequest request, HttpServletResponse response) throws Exception {
        
        //接受用户的表单数据,将其封装到一个对象User上
        User user=MyBeanUtils.populate(User.class, request.getParameterMap());
        user.setUid(UUIDUtils.getId());
        user.setCode(UUIDUtils.getUUID64());
        System.out.println(user);
        //调用sevice用户注册功能
        IUserService userService=new UserServiceImp();
        userService.regist(user);
        //转发到个提示页面info.jsp
        request.setAttribute("msg", "用户注册成功,请激活");
        return "/jsp/info.jsp";
        
    }
    
    //active
    public String active(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取用户的激活码
        String code = request.getParameter("code");
        //调用SERVICE层激活功能,返回用户对象
        IUserService userService=new UserServiceImp();
        User user=userService.active(code);
        System.out.println(user);
        if(null!=user) {
            //如果用户不为空,可以激活,更改用户的状态,清空用户的激活码,向request放入提示消息,转发到登录页面
            user.setState(1);
            user.setCode("");
            userService.updateUser(user);
            request.setAttribute("msg", "用户激活成功,请登录");
            return "/jsp/login.jsp";
        }else {
            //如果用户为空,可以失败,向request放入提示消息,转发到info.jsp页面
            request.setAttribute("msg", "用户激活失败,请重新激活");
            return "/jsp/info.jsp";
        }
    }
    //loginUI
    public String loginUI(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return "/jsp/login.jsp";
    }
    //用户登录
    //login
    public String login(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //接受表单参数
        User user=MyBeanUtils.populate(User.class, request.getParameterMap());
        //调用业务层登录功能,返回用户对象
        IUserService userService=new UserServiceImp();
        User uu=userService.login(user);
        if(null!=uu) {
            //用户名不为空,登录成功,将登录成功的用户放入session,重定向到项目首页
            request.getSession().setAttribute("user", uu);
            response.sendRedirect("/store_v4/index.jsp");
            return null;
        }else {
            //用户名为空,登录失败,向request中放入提示消息,转发/jsp/info.jsp
            request.setAttribute("msg", "用户登录失败,请重新登录");
            return "/jsp/info.jsp";
        }
    }
    //logOut
    public String logOut(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.getSession().invalidate();
        response.sendRedirect("/store_v4/index.jsp");
        return null;
    }
}

 

当请求到servlet后,在servlet

 

posted @ 2018-05-11 18:36  cheney-yang  阅读(1516)  评论(0编辑  收藏  举报