Home  >  Article  >  Java  >  Cases about this.getClass() and super.getClass() in Java

Cases about this.getClass() and super.getClass() in Java

黄舟
黄舟Original
2017-08-22 10:06:431848browse

This article mainly introduces the relevant information about the instances of this.getClass() and super.getClass() in java. Friends in need can refer to

Detailed explanation of this.getClass in java () and examples of super.getClass()

Preface:

Feelings when encountering the return values ​​​​of this.getClass() and super.getClass() Doubts, suddenly became clear after exploration.

getClass() is a method of the Object class in java, and its prototype is:        


##

 public final Class<?> getClass()

                                                                 The return value is the Class object of the current runtime class.


So I wrote a piece of code to illustrate:


getClass() is not affected by this and super, but is determined by the current running class.


The code is as follows:


Parent class:


##

package com.cyou.lijiang_hw; 
 
import java.lang.reflect.ParameterizedType; 
 
public class BaseDAO { 
  public BaseDAO() { 
    System.out.println("==============BaseDAO initialization============="); 
    System.out.println("this.getClass() ---->" + this.getClass()); 
    System.out.println("super.getClass()---->" + super.getClass());  
  } 
}

Subclass:


package com.cyou.lijiang_hw; 
 
public class UserDAO extends BaseDAO<User> { 
  public UserDAO() { 
    System.out.println("\n==============UserDAO initialization============="); 
    System.out.println("this.getClass() ---->" + this.getClass()); 
    System.out.println("super.getClass()---->" + super.getClass()); 
  } 
}

Test class:


package com.cyou.lijiang_hw; 
 
public class Test { 
  public static void main(String[] args) { 
    UserDAO userDAO = new UserDAO(); 
  } 
}

The output result is:


==============BaseDAO<T> initialization============= 
this.getClass() ---->class com.cyou.lijiang_hw.UserDAO 
super.getClass()---->class com.cyou.lijiang_hw.UserDAO 
 
==============UserDAO initialization============= 
this.getClass() ---->class com.cyou.lijiang_hw.UserDAO 
super.getClass()---->class com.cyou.lijiang_hw.UserDAO

The above is the detailed content of Cases about this.getClass() and super.getClass() in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn