getclass与getsuperclass (1)

本文深入探讨了Java中super关键字的使用误区,解释了super.getClass()的实际含义及其背后的原理,并提供了获取父类名称的正确方法。

Java代码 
import Java.util.Date;    
public  class Test extends Date{    
    public static void main(String[] args) {    
        new Test().test();    
    }    
        
    public void test(){    
        System.out.println(super.getClass().getName());    
    }    
  

import java.util.Date; 
public  class Test extends Date{ 
    public static void main(String[] args) { 
        new Test().test(); 
    } 
     
    public void test(){ 
        System.out.println(super.getClass().getName()); 
    } 
 

你可能会认为是Date,但是实际的结果是Test。没错,你没有看错,super.getClass()并不会返回超类的引用。我们再做一个实验,在test方法中直接调用getClass().getName()方法,则结果返回的是Test。为什么super没有起作用呢?简单来说,super并不能代表一个超类的引用。 

 

因为super并没有代表超类的一个引用的能力,只是代表调用父类的方法而已。所以,在子类的方法中,不能这样用System.out.println(super);也不能使用super.super.mathod();

 

事实上,super.getClass()是表示调用父类的方法。getClass方法来自Object类,它返回对象在运行时的类型。因为在运行时的对象类型是Test,所以this.getClass()和super.getClass()都是返回Test。

 

此外,由于getClass()在Object类中定义成了final,子类不能覆盖该方法,所以,在test方法中调用getClass().getName()方法,其实就是在调用从父类继承的getClass()方法,等效于调用super.getClass().getName()方法,所以,super.getClass().getName()方法返回的也应该是Test。 

 

如果想得到父类的名称,应该用如下代码: 

 

Java代码 
getClass().getSuperClass().getName();

package com.app.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.app.dao.DictionaryDao; import com.app.entity.DictionaryEntity; import com.app.service.DictionaryService; import jakarta.servlet.ServletContext; import jakarta.servlet.http.HttpServletRequest; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; import java.util.Map; /** * 字典表服务实现类 */ @Service("dictionaryService") @Transactional public class DictionaryServiceImpl extends ServiceImpl<DictionaryDao, DictionaryEntity> implements DictionaryService { @Override public Page<DictionaryEntity> queryPage(Map<String, Object> params, QueryWrapper<DictionaryEntity> wrapper) { // 分页参数处理 long pageNum = 1; long pageSize = 10; if (params.get("page") != null) { pageNum = Long.parseLong((String) params.get("page")); } if (params.get("limit") != null) { pageSize = Long.parseLong((String) params.get("limit")); } // 创建分页对象 Page<DictionaryEntity> page = new Page<>(pageNum, pageSize); return baseMapper.selectPage(page, wrapper); } @Override public void dictionaryConvert(Object obj, HttpServletRequest request) { try { if (obj == null) return; // 获取所有以"Types"结尾的字段 List<String> fieldNameList = new ArrayList<>(); Class<?> tempClass = obj.getClass(); while (tempClass != null) { for (Field f : tempClass.getDeclaredFields()) { f.setAccessible(true); if (f.getType().equals(Integer.class) && f.getName().endsWith("Types")) { fieldNameList.add(f.getName()); } } tempClass = tempClass.getSuperclass(); } // 获取ServletContext中的字典数据 ServletContext servletContext = request.getServletContext(); Map<String, Map<Integer, String>> dictionaryMap = (Map<String, Map<Integer, String>>) servletContext.getAttribute("dictionaryMap"); // 为每个字段赋值 for (String fieldName : fieldNameList) { Field typesField = null; Field valueField = obj.getClass().getDeclaredField(fieldName.replace("Types", "Value")); valueField.setAccessible(true); // 获取Types字段 if (hasField(obj.getClass(), fieldName)) { typesField = obj.getClass().getDeclaredField(fieldName); } else if (obj.getClass().getSuperclass() != null && hasField(obj.getClass().getSuperclass(), fieldName)) { typesField = obj.getClass().getSuperclass().getDeclaredField(fieldName); } if (typesField == null) continue; typesField.setAccessible(true); Integer typeValue = (Integer) typesField.get(obj); // 转换字段名为下划线格式 String dictKey = toUnderScoreCase(fieldName); // 赋值 if (typeValue != null && dictionaryMap.containsKey(dictKey)) { String dictValue = dictionaryMap.get(dictKey).get(typeValue); valueField.set(obj, dictValue); } else { valueField.set(obj, ""); } } } catch (Exception e) { e.printStackTrace(); } } private String toUnderScoreCase(String camelCase) { StringBuilder sb = new StringBuilder(); for (char c : camelCase.toCharArray()) { if (Character.isUpperCase(c)) { sb.append('_').append(Character.toLowerCase(c)); } else { sb.append(c); } } return sb.toString(); } private boolean hasField(Class<?> clazz, String fieldName) { for (Field f : clazz.getDeclaredFields()) { if (f.getName().equals(fieldName)) { return true; } } return false; } }出现类“DictionaryServiceImpl”必须声明为抽象,或为实现“DictionaryService”中的抽象方法“queryPage(Map<String, Object>, Wrapper<DictionaryEntity>)”和方法未从其超类重写方法
最新发布
07-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值