gpt4 book ai didi

Java 静态嵌套类型语义

转载 作者:行者123 更新时间:2023-12-01 16:52:47 24 4
gpt4 key购买 nike

最近在学习Java,得出以下结论:声明静态嵌套类型是毫无意义的冗余。

我正在阅读以下问题的答案,虽然我确实理解答案(并且它消除了我最初的困惑 - 类似于提问者的困惑) Can a Static Nested Class be Instantiated Multiple Times?我还得出结论,关于内部类型的“静态”一词是没有意义的,例如,如果我们没有将内部类型声明为静态,我们仍然不需要实例化外部类型来创建内部类型的新对象。毕竟它是嵌套类型而不是外部类型的成员/变量。我刚刚测试了一些代码(删除了静态),我的假设是正确的。

有什么我错过的吗?

这是我测试的代码(请注意,当我从 IGameInput 内的 KeyEvent Type 中删除 Static 时,一切正常)

package FrameWork;
import java.util.List;
public interface IGameInput {

//type definitions
public static class KeyEvent {
public static final int KEY_DOWN = 0;
public static final int KEY_UP = 1;
public int type;
public int keyCode;
public char keyChar;
}
}

////////////////////////

package FrameWork.Imp;


import java.util.ArrayList;
import java.util.List;

/*
*/
public class Pool<T>
{
public interface PoolObjectFactory<T> {
public T createObject();
}

private final ArrayList<T> freeObjects;
private final PoolObjectFactory<T> factory;
private final int maxSize;

public Pool(PoolObjectFactory<T> factory, int maxSize)
{
this.factory = factory;
this.maxSize = maxSize;
this.freeObjects = new ArrayList<T>(maxSize);
}

public T newObject()
{
T object = null;
if (freeObjects.size() == 0)
{
object = factory.createObject();
}
else
{
object = freeObjects.remove(freeObjects.size() - 1);
}
return object;
}

public void free(T object)
{
if (freeObjects.size() < maxSize)
{
freeObjects.add(object);
}
}
}

/////////////////////////

package FrameWork.Imp;

import java.util.ArrayList;
import java.util.List;
import android.view.View;
import android.view.View.OnKeyListener;
import FrameWork.IGameInput.KeyEvent;
import FrameWork.Imp.Pool.PoolObjectFactory;

public class KeyboardHandler implements OnKeyListener {
boolean[] pressedKeys = new boolean[128];
Pool<KeyEvent> keyEventPool;
List<KeyEvent> keyEventsBuffer = new ArrayList<KeyEvent>();
List<KeyEvent> keyEvents = new ArrayList<KeyEvent>();

public KeyboardHandler(View view) {

PoolObjectFactory<KeyEvent> factory;
factory =
new PoolObjectFactory<KeyEvent>() {
@Override
public KeyEvent createObject() {
return new KeyEvent();
}
};
keyEventPool = new Pool<KeyEvent>(factory, 100);
view.setOnKeyListener(this);
view.setFocusableInTouchMode(true);
view.requestFocus();
}

@Override
public boolean onKey(View v, int keyCode, android.view.KeyEvent event) {
if (event.getAction() == android.view.KeyEvent.ACTION_MULTIPLE) return false;
synchronized (this) {
KeyEvent keyEvent = keyEventPool.newObject();
keyEvent.keyCode = keyCode;
keyEvent.keyChar = (char) event.getUnicodeChar();
if (event.getAction() == android.view.KeyEvent.ACTION_DOWN) {
keyEvent.type = KeyEvent.KEY_DOWN;
if (keyCode > 0 && keyCode < 127) pressedKeys[keyCode] = true;
}
if (event.getAction() == android.view.KeyEvent.ACTION_UP) {
keyEvent.type = KeyEvent.KEY_UP;
if (keyCode > 0 && keyCode < 127) pressedKeys[keyCode] = false;
}
keyEventsBuffer.add(keyEvent);
}
return false;
}
}

最佳答案

如果没有包含类的实例,您无法实例化非静态嵌套类。

class Test {
public static void main(String[] args) { //static, non-instance method
Inner inner = new Inner(); //error
}

class Inner { }
}

您能够执行此操作的唯一方法是在包含类的实例中实例化它(最有可能是您正在做的事情,根据您的解释):

class Test {
public void someMethod() { //non-static, instance method
Inner inner = new Inner();
}

class Inner { }
}

或者通过使用包含类的实例来实例化内部类:

class Test {
public static void main(String[] args) { //static, non-instance method
Test test = new Test();
Inner inner = test.new Inner(); //error
}

class Inner { }
}

否则你会得到编译时错误:

non-static variable this cannot be referenced from a static context

<小时/>

通过您的代码示例,它更有意义。 KeyEvent 仍然是静态的。调查JLS §9.5 :

A member type declaration in an interface is implicitly static and public.

这意味着所有类,无论您是否将它们显式定义为静态,都将是静态

关于Java 静态嵌套类型语义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36609804/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com