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

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

黄舟
Release: 2017-08-22 10:06:43
Original
1901 people have browsed it

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()
Copy after login

                                                                 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());  
  } 
}
Copy after login

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()); 
  } 
}
Copy after login

Test class:


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

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template