我对C#比较陌生,每次我开始研究C#项目(我只用C#中几乎成熟的项目)我想知道为什么没有内部类?
也许我不明白他们的目标.对我来说,内部类 - 至少是私有内部类 - 看起来很像Pascal/Modula-2/Ada中的"内部程序":它们允许在较小的部分中分解主类以便于理解.
示例:这是大多数时间看到的内容:
public class ClassA
{
public MethodA()
{
<some code>
myObjectClassB.DoSomething(); // ClassB is only used by ClassA
<some code>
}
}
public class ClassB
{
public DoSomething()
{
}
}
Run Code Online (Sandbox Code Playgroud)
由于ClassB只会被ClassA使用(至少有一段时间),我猜这个代码会更好地表达如下:
public class ClassA
{
public MethodA()
{
<some code>
myObjectClassB.DoSomething(); // Class B is only usable by ClassA
<some code>
}
private class ClassB
{
public DoSomething()
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
我很高兴收到你关于这个主题的消息 - 我是对的吗?
是否可以在Java中创建接口实例?
在某处我读过使用内部匿名类,我们可以这样做,如下所示:
interface Test
{
public void wish();
}
class Main
{
public static void main(String[] args)
{
Test t=new Test()
{
public void wish()
{
System.out.println("output: hello how r u");
}
};
t.wish();
}
}
cmd> javac Main.java
cmd> java Main
output: hello how r u
Run Code Online (Sandbox Code Playgroud)
这是正确的吗?
是否有可能this从Java内部类中获取引用?
即
class Outer {
void aMethod() {
NewClass newClass = new NewClass() {
void bMethod() {
// How to I get access to "this" (pointing to outer) from here?
}
};
}
}
Run Code Online (Sandbox Code Playgroud) 我想知道将私有方法声明为最终是否有意义,我认为这没有意义.但我想象有一个独特的情况,并编写代码来弄清楚:
public class Boom {
private void touchMe() {
System.out.println("super::I am not overridable!");
}
private class Inner extends Boom {
private void touchMe() {
super.touchMe();
System.out.println("sub::You suck! I overrided you!");
}
}
public static void main(String... args) {
Boom boom = new Boom();
Boom.Inner inner = boom.new Inner();
inner.touchMe();
}
}
Run Code Online (Sandbox Code Playgroud)
它编译和工作."我应该让touchMe()最终成功"我想到并做到了:
public class Boom {
private final void touchMe() {
System.out.println("super::I am not overridable!");
}
private class Inner extends Boom {
private void touchMe() {
super.touchMe();
System.out.println("sub::You suck! I …Run Code Online (Sandbox Code Playgroud) 我今天在阅读Accelerated GWT(Gupta)时遇到了这段代码- 第151页.
public static void getListOfBooks(String category, BookStore bookStore) {
serviceInstance.getBooks(category, bookStore.new BookListUpdaterCallback());
}
public static void storeOrder(List books, String userName, BookStore bookStore) {
serviceInstance.storeOrder(books, userName, bookStore.new StoreOrderCallback());
}
Run Code Online (Sandbox Code Playgroud)
这些新运营商在那里做什么?我从未见过这样的语法,任何人都可以解释一下吗?
有谁知道在java规范中哪里可以找到这个?
我读到了结构化单元测试,每个类都有一个测试类,每个方法都有一个内部类.想象一下,这似乎是一种组织测试的方便方法,所以我在Java项目中尝试过.但是,内部类中的测试似乎根本没有被接受.
我大致这样做了:
public class DogTests
{
public class BarkTests
{
@Test
public void quietBark_IsAtLeastAudible() { }
@Test
public void loudBark_ScaresAveragePerson() { }
}
public class EatTests
{
@Test
public void normalFood_IsEaten() { }
@Test
public void badFood_ThrowsFit() { }
}
}
Run Code Online (Sandbox Code Playgroud)
JUnit不支持这个,还是我做错了?
假设我在表单中有一个下拉列表,并且我在此类中有另一个嵌套类.现在,从嵌套类访问此下拉列表的最佳方法是什么?
我尝试做的是:
public class History {
public class State {
public enum StateType {
Run Code Online (Sandbox Code Playgroud)
Eclipse给出了我的编译错误StateType:The member enum StateType must be defined inside a static member type.
当我使State类静态时,错误消失.我可以制作State静态,但我不明白为什么我不能enum在内部类中声明.
如果我有一个类和一个辅助类来执行它的一些功能,那么将它作为内部类是否有意义.
public class Foo {
private FooHelper helper;
// constructor & any other logic
public void doSomeThing() {
helper.do();
}
}
public class FooHelper {
public void do() {
// code
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,将内容FooHelper作为一个内部类是否有意义?道歉,如果这听起来很愚蠢,但我对用例感到困惑.
final JTextField jtfContent = new JTextField();
btnOK.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent event){
jtfContent.setText("I am OK");
}
} );
Run Code Online (Sandbox Code Playgroud)
如果我省略final,我看到错误" 不能引用在不同方法中定义的内部类中的非最终变量jtfContent ".
为什么匿名内部类必须要求外部类实例变量为最终才能访问它?