About the representation of this in the Java inheritance system

Source: Internet
Author: User

Java's inheritance system, because there is the concept of rewriting, so that the call between the child parent class is exactly who the method, or member properties, the problem is a question worth thinking about;

First of all, if you call a member property in the test class that has the same name as the child parent, the value of this member property is the value of this member property in the parent class because there is no overriding concept of member properties in Java

, if you call a member method with the same name, then you should call the overridden member method .... If there is no such member method in the subclass, look in the parent class.

The above code supports the argument:

 PackageTest_this; Public classDemo { Public Static voidMain (string[] args) {Fu F=NewZi (); System.out.println (f.num);//the parent class is defined here, and the member variable is not polymorphic, so even if your new subclass still points to the member variable of the parent class. System.out.println (F.FUN1 ());//no explanation, it's polymorphic. f.show (); }}classFu { PublicString num = "Parent class member variable";  Public voidShow () {System.out.println ( This. num);//because the member variable is not polymorphic, this is a pointer to the member variable of the current class object. System.out.println ( This. FUN1 ());//because the method is polymorphic, this is the method that points to the new object.     }         PublicString fun1 () {System.out.println ( This. num);//because the member variable is not polymorphic, this is a pointer to the member variable of the current class object.         return"Parent class invocation"; }}classZiextendsFu { PublicString num = "Child class member variable";  PublicString fun1 () {System.out.println ( This. num);//because the member variable is not polymorphic, this is a pointer to the member variable of the current class object.         return"Subclass Invocation"; }}

Operation Result:

The above code experiment results can prove its argumentation;

This feature can be used in web development, combining reflection to extract the Servlet method

On the code: This is the parent class

Importjava.io.IOException;ImportJava.lang.reflect.Method;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse; Public classBaseservletextendsHttpServlet {@Override Public voidDoget (httpservletrequest req, HttpServletResponse resp)throwsservletexception, IOException { This. DoPost (req, resp); } @Override Public voidDoPost (httpservletrequest req, HttpServletResponse resp)throwsservletexception, IOException {//localhost:8080/test/productservlet?method=addproductString method = Req.getparameter ("Method"); if(NULL= = Method | | "". Equals (method) | | Method.trim (). Equals ("") ) {method= "Execute"; }        //Note: This here represents the object of the subclassSystem.out.println ("Baseservlet This"); System.out.println ( This); //sub-class object byte code objectClass Clazz = This. GetClass (); Try {            //find the Subclass object that corresponds to the name of the method in the byte code. The parameter type for this method is: Httpservletrequest.class,httpservletresponse.classMethod 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 (); }        }    //Default Method     PublicString Execute (httpservletrequest req, HttpServletResponse resp)throwsservletexception, IOException {return NULL; }}

Sub-class:

Importjava.io.IOException;Importjavax.servlet.ServletException;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportCn.itcast.domain.User;ImportCn.itcast.service.IUserService;Importcn.itcast.service.serviceImp.UserServiceImp;Importcn.itcast.utils.MyBeanUtils;Importcn.itcast.utils.UUIDUtils;ImportCn.itcast.web.base.BaseServlet; Public classUserservletextendsBaseservlet {Private Static Final LongSerialversionuid = 1L;  PublicString Registui (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {return"/jsp/register.jsp"; }    //regist     PublicString regist (httpservletrequest request, httpservletresponse response)throwsException {//accept the user's form data and encapsulate it on an object userUser user=mybeanutils.populate (user.class, Request.getparametermap ());        User.setuid (Uuidutils.getid ());        User.setcode (Uuidutils.getuuid64 ());        SYSTEM.OUT.PRINTLN (user); //invoke the Sevice user Registration featureIuserservice userservice=NewUserserviceimp ();        Userservice.regist (user); //forward to a prompt page info.jspRequest.setattribute ("msg", "User registration successful, please activate"); return"/jsp/info.jsp"; }        //Active     PublicString Active (HttpServletRequest request, httpservletresponse response)throwsException {//get the user's activation codeString code = request.getparameter ("code"); //Call the service Layer activation feature to return the user objectIuserservice userservice=NewUserserviceimp (); User User=userservice.active (code);        SYSTEM.OUT.PRINTLN (user); if(NULL!=user) {            //If the user is not empty, you can activate, change the user's status, clear the user's activation code, put a prompt message to request, forward to the login pageUser.setstate (1); User.setcode ("");            Userservice.updateuser (user); Request.setattribute ("MSG", "User activation successful, please log in"); return"/jsp/login.jsp"; }Else {            //If the user is empty, can fail, put a prompt message to request, forward to the info.jsp pageRequest.setattribute ("msg", "User activation failed, please reactivate"); return"/jsp/info.jsp"; }    }    //Loginui     PublicString Loginui (httpservletrequest request, httpservletresponse response)throwsException {return"/jsp/login.jsp"; }    //User Login//Login     PublicString Login (httpservletrequest request, httpservletresponse response)throwsException {//Accept Form ParametersUser user=mybeanutils.populate (user.class, Request.getparametermap ()); //Call the Business Layer login feature to return the user objectIuserservice userservice=NewUserserviceimp (); User UU=userservice.login (user); if(NULL!=UU) {            //user name is not empty, login is successful, login successful user into session, redirect to Project home pageRequest.getsession (). SetAttribute ("User", UU); Response.sendredirect ("/store_v4/index.jsp"); return NULL; }Else {            //user name is empty, login failed, put a hint message in request, forward/jsp/info.jspRequest.setattribute ("msg", "User logon failed, please login again"); return"/jsp/info.jsp"; }    }    //LogOut     PublicString LogOut (httpservletrequest request, httpservletresponse response)throwsException {request.getsession (). invalidate (); Response.sendredirect ("/store_v4/index.jsp"); return NULL; }}

When requested to the servlet, the servlet

About the representation of this in the Java inheritance system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.