Java基础-class 类的基础方法

本文探讨了Java中Class类的getClasses、getDeclaredClasses、getDeclaredConstructors和构造内部类的方法。getDeclaredConstructors返回所有构造函数,而getClasses仅获取public类。对于内部类,静态内部类可以通过`new Payment.InnerStaticAccount()`创建,而非静态内部类需通过反射如`c.newInstance(new Payment(), "123")`创建。" 100535994,8367711,FAST算法:高效角点检测在实时应用中的实现,"['计算机视觉', '图像处理', '特征检测', '机器学习', 'OpenCV']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Class里的方法

  1. getClasses 和 getDeclaredClasses

getDeclaredClasses 获取到类里所有的的class ,interface 包括了private ,protected,default,public

例子:

定义一个Payment的基本类如下

public class Payment {

protected class InnerPayment{

}

String name;

interface Account{}

public class InnerAccount implements Payment.Account{

}

private class InnerAccount2 implements Payment.Account{

}

}

测试

public class PaymentReflectTest {

public static void main(String[] args) {

  Class[] cls=Payment.class.getDeclaredClasses();//获取到所有的定义的class

    for (int i = 0; i <cls.length ; i++) {

        System.out.println(cls[i]);

    }

}

}

打印出来的结果如下

class rechard.learn.reflect.Payment$InnerAccount2

class rechard.learn.reflect.Payment$InnerAccount

interface rechard.learn.reflect.Payment$Account

class rechard.learn.reflect.Payment$InnerPayment

getClasses 只获取到public

上面的测试代码改成

Class[] cls=Payment.class.getClasses();

    for (int i = 0; i <cls.length ; i++) {

        System.out.println(cls[i]);

    }

只获取到

1.class rechard.learn.reflect.Payment$InnerAccount

  1. getConstructors 和 getDeclaredConstructors

getDeclaredConstructors 打印出类的所有的构造函数

Class[] cls=Payment.class.getDeclaredClasses();

    for (int i = 0; i <cls.length ; i++) {

        Constructor[] cs= cls[i].getDeclaredConstructors();

        for (int j = 0; j <cs.length; j++) {

            System.out.println(cs[j]);

        }

    }

打印的结果如下

private rechard.learn.reflect.Payment$InnerAccount2(rechard.learn.reflect.Payment)

public rechard.learn.reflect.Payment$InnerAccount(rechard.learn.reflect.Payment)

protected rechard.learn.reflect.Payment$InnerPayment(rechard.learn.reflect.Payment)

由于这里的class都是内部类,第一个参数是父类。在此我向大家推荐一个架构学习交流圈。交流学习指导伪鑫:1253431195(里面有大量的面试题及答案)里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化、分布式架构等这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多
new 的时候使用new Payment().new InnerAccount();

如果定义的内部类是static ,则new 的时候使用new Payment.InnerStaticAccount()

public class Payment {

public static class InnerStaticAccount implements Payment.Account{

}

}

getConstructors 打印出类的public构造函数

  1. new instance

如何new Payment里的InnerAccount

public class Payment {

  protected class InnerAccount implements Payment.Account{

    private String acctNumber;

    public InnerAccount(String acctNumber){

        this.acctNumber=acctNumber;

    }

    public String getAcctNumber() {

        return acctNumber;

    }

    @Override

    public String toString() {

        return "InnerAccount{" +

                "acctNumber='" + acctNumber + '\'' +

                '}';

    }

}

}

如果 new InnerAccount 的类不和Payment 在同一个package下,写成如下,会报错,InnerAccount为proctected 不可见:

new Payment().new InnerAccount(“111111”);

改成以下代码调用

    Class[] cls = Payment.class.getDeclaredClasses();

    for (int i = 0; i < cls.length; i++) {

      if(cls[i].getSimpleName().equals("InnerAccount")){

          try {

              Constructor c=(Constructor)cls[i].getDeclaredConstructor(Payment.class,String.class);

              c.setAccessible(true);

              System.out.println(c.newInstance(new Payment(),"123"));

          } catch (Exception e) {

              e.printStackTrace();

          }

      }

    }

其实这样构造出来的有诸多不变,上面的c.newInstance(new Payment(),“123”),没法用一个实际的引用的引用,只能用Object o 来引用,如果如果要调用getAcctNumber(),只能通过反射来调用,如下:

Class[] cls = Payment.class.getDeclaredClasses();

    for (int i = 0; i < cls.length; i++) {

      if(cls[i].getSimpleName().equals("InnerAccount")){

          try {

Constructor c=(Constructor)cls[i].getDeclaredConstructor(Payment.class,String.class);

              c.setAccessible(true);

              Object o=c.newInstance(new Payment(),"123");

              Method m=o.getClass().getMethod("getAcctNumber",null);

              System.out.println(m.invoke(o,null));

              }catch (Exception e) {

              e.printStackTrace();

          }

      }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值