如何从静态 main() 方法调用内部类的方法

编程入门 行业动态 更新时间:2024-10-27 19:25:58
本文介绍了如何从静态 main() 方法调用内部类的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

尝试在父类中创建 1 个接口和 2 个具体类.这将使封闭类成为内部类.

Trying to create 1 interface and 2 concrete classes inside a Parent class. This will qualify the enclosing classes to be Inner classes.

public class Test2 { interface A{ public void call(); } class B implements A{ public void call(){ System.out.println("inside class B"); } } class C extends B implements A{ public void call(){ super.call(); } } public static void main(String[] args) { A a = new C(); a.call(); } }

现在我不太确定如何在静态 main() 方法中创建类 C 的对象并调用类 C 的 call() 方法.现在我遇到问题了:A a = new C();

Now I am not really sure how to create the object of class C inside the static main() method and call class C's call() method. Right now I am getting problem in the line : A a = new C();

推荐答案

这里的内部类不是静态的,所以需要创建外部类的实例,然后调用new,

Here the inner class is not static, so you need to create an instance of outer class and then invoke new,

A a = new Test2().new C();

但在这种情况下,您可以将内部类设为静态,

But in this case, you can make the inner class static,

static class C extends B implements A

那么就可以使用了,

A a = new C()

更多推荐

如何从静态 main() 方法调用内部类的方法

本文发布于:2023-06-11 23:34:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/645341.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:部类   方法   静态   main

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!