首页 > 其他分享 >非静态内部类newInstance

非静态内部类newInstance

时间:2023-07-03 11:36:44浏览次数:45  
标签:them outer 内部 newInstance 静态 inner class ctor NewTest

https://stackoverflow.com/questions/25634542/newinstance-with-inner-classes

 

Non-static inner classes need an instance of the outer class to work properly. So, they don't "really" have a default constructor, they always have a kind of hidden parameter in which they expect an outer class instance.

I don't know why you want to have them all in a single class. If you are doing this so that it's only one file, put them in a package and in separate classes. Having less files does not make your program better.

If instead you need them to share something, so the outer class will work as a kind of "scope", you can still do that without using inner classes, but by passing them a context of some sort.

If you really really want to instantiate the inner class, you need to use the hidden constructor taking the outer class as a parameter :

NewTest outer = new NewTest();
Class<?> toRun = Class.forName("NewTest$" + args[0]);
Constructor<?> ctor = toRun.getDeclaredConstructor(NewTest.class);
ctor.newInstance(outer);

 

public HttpServerJob setModifier(Class<? extends Modifier> modifier) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Constructor<?> ctor = modifier.getDeclaredConstructor(HttpServerJob.class);
ctor.setAccessible(true);
this.modifier = (Modifier)ctor.newInstance(this);
return this;
}

 

 

 

Non-static inner classes need an instance of the outer class to work properly. So, they don't "really" have a default constructor, they always have a kind of hidden parameter in which they expect an outer class instance.

I don't know why you want to have them all in a single class. If you are doing this so that it's only one file, put them in a package and in separate classes. Having less files does not make your program better.

If instead you need them to share something, so the outer class will work as a kind of "scope", you can still do that without using inner classes, but by passing them a context of some sort.

If you really really want to instantiate the inner class, you need to use the hidden constructor taking the outer class as a parameter :

NewTest outer = new NewTest();
Class<?> toRun = Class.forName("NewTest$" + args[0]);
Constructor<?> ctor = toRun.getDeclaredConstructor(NewTest.class);
ctor.newInstance(outer);

标签:them,outer,内部,newInstance,静态,inner,class,ctor,NewTest
From: https://www.cnblogs.com/silyvin/p/17522310.html

相关文章

  • 21-数码管静态显示
    1.数码管静态显示数码管是一种半导体发光器件,其基本单元是发光二极管常见的数码管有七段数码管和八段数码管(相差一个小数点),还有米字管,十六段管等八段发光数码管每一段对应a,b,c,d,e,f,g,dp(小数点)八段数码管有十个管教,八段+两个公共端com,与数码管内部是导通的八段......
  • Kafka—生产者和消费者的内部结构
    生产者将数据发布到Kafka主题的应用程序称为生产者。应用程序集成了一个Kafka客户端库来写入Kafka。编写过程从创建ProducerRecird开始。KafkaProducers中的组件/流程拦截器——可以在发送之前改变记录的拦截器,例如Claim-check-interceptor。生产者元数据——管理生产者所需......
  • Pycharm执行allure命令报错:allure 不是内部或外部命令,也不是可运行的程序 - 实测有效
    Pycharm执行allure命令报错:allure不是内部或外部命令,也不是可运行的程序现象:1、在doc中正常执行allure命令,只有在pycharm中无法执行2、在pycharm中打印os.environ,没有allureenv_dist=os.environforkeyinenv_dist:if'allure'inenv_dist[key]:print(k......
  • Kafka—生产者和消费者的内部结构
     生产者将数据发布到Kafka主题的应用程序称为生产者。应用程序集成了一个Kafka客户端库来写入Kafka。编写过程从创建ProducerRecird开始。 KafkaProducers中的组件/流程拦截器——可以在发送之前改变记录的拦截器,例如Claim-check-interceptor。生产者元数据—......
  • 12.1 内部类基本概念
    democlassOuter{ //外部类 privateStringmsg="www.mldn.cn"; //私有成员属性 publicvoidfun(){ //普通方法 Innerin=newInner(); //实例化内部类对象 in.print(); //调用内部类方法 } classInner{ //......
  • [WPF]静态资源(StaticResource)和动态资源(DynamicResource)
    一、文章概述本演示介绍了WPF的静态资源和动态资源的基本使用,并对两者做了简单的比较。静态资源(StaticResource)指的是在程序载入内存时对资源的一次性使用,之后就不再访问这个资源了;动态资源(DynamicResource)使用指的是在程序运行过程中然会去访问资源。相关下载(代码、屏幕录像):h......
  • 结合静态与动态分析优势范围的fuzzer:Arbiter
    对于Arbier这款新型fuzzer的研究,目前网络上几乎没有内容,因此大部分内容的直接来源于Arbier的论文《Arbiter:BridgingtheStaticandDynamicDivideinVulnerabilityDiscoveryonBinaryPrograms》,该论文随着2022年8月的USENIX安全研讨会的论文集中发表。1.引言当前最先......
  • 将onnx的静态batch改为动态batch及修改输入输出层的名称
    目录背景操作修改输入输出层修改输入输出层名称完整代码背景在模型的部署中,为了高效利用硬件算力,常常会需要将多个输入组成一个batch同时输入网络进行推理,这个batch的大小根据系统的负载或者摄像头的路数时刻在变化,因此网络的输入batch是在动态变化的。对于pytorch等框架来说,我......
  • Java基础复习——内部类
    内部类什么是内部类?一个类中又完整的嵌套了另一个类结构。被嵌套的类称为内部类(innerclass),嵌套其他类的类称为外部类(outerclass)。内部类的最大特点:可以直接访问私有属性,并且可以体现类与类之间的包含关系。为什么使用内部类内部类方法可以访问该类定义所在的作用域中的......
  • Java中的内部类,代码块
    前言学习Java过程中,对内部类和代码块部分知识比较模糊,容易忘掉。今天再学的时候有一些其他的收获,整理记录一下。1.代码块代码块的作用可以为初始化变量。都在在构造器方法之前执行,分为静态代码块和匿名代码块。静态代码块是在类加载时执行,因此只执行一次。匿名代码块在new实......