英文:
Make an object by calling a method in java
问题
如果我们有一个 `MakeObject(String Name)` 方法,并且调用此方法将创建 `Object [Name] = new Object();`。<br>
我们应该如何实现这个?另外,如果我们想将 `Object [Name]` 从方法移到类 `Clazz` 中的一个公共变量怎么办?<br>
示例:<br>
```java
public class Clazz {
void MakeObject(String name) {
Object[name] = new Object();
[name].UpgradeToClassVariable(); // 应该是一个全局变量
}
}
Clazz
的用法:
public class Main {
public static void main(String[] args) {
Clazz c = new Clazz();
c.makeObject("Hello");
c.Hello.doOperations(); // 例如,如果它是一个字符串,doOperation() 可以是 equals()
}
}
<details>
<summary>英文:</summary>
if we have `MakeObject(String Name)` and calling this method will create `Object [Name] = new Object();`<br>
How can we do that? And, if we want to move the `Object [Name]` from the method to a public variable in class `Clazz` ?<br>
Example:<br>
public class clazz {
void MakeObject(String name) {
Object[name] = new Object();
[name].UpgradeToClassVariable(); // It should be a global variable
}
}
Usage of `clazz` :<br>
public class main {
public static void main(String[] args) {
clazz c = new clazz();
c.makeObject("Hello");
c.Hello.doOperations(); // For example, if it's a String, doOperation() can be equals()
}
}
</details>
# 答案1
**得分**: 2
在Java中,`.`是解引用运算符:获取点左边的东西。这必须是一个引用。如果它是一个空引用(null),就会抛出NullPointerException(空指针异常)。
点右边的东西是您发送给它的消息。
因此,向对象发送消息“使自己变成全局的”毫无意义。像`MyClass.makeGlobal([name])`这样的写法可能行得通,但Java不支持这样的操作。
类的结构在编译时被定义。您不能在运行时添加属性,或者将字段从实例更改为静态。
“我想添加一个属性”的一般思想仍然存在,但不是您所讨论的层次。也许您想要一个`Map<String, Integer>`,例如。
看看您的第二个片段:
```java
clazz c = new clazz();
c.makeObject("Hello");
c.Hello.doOperations(); // 例如,如果它是一个String,doOperation()可能是equals()
这只不过不是Java的工作方式。Java首先会编译代码(使用javac
),然后运行它。javac
不运行任何代码,它只进行编译。因此,javac
看到c.Hello
,并在那里停止,说:Hello?什么?我不知道你在说什么。
这就是为什么不可能使用字符串字面量来创建这样的字段。
Java不是JavaScript,也不是Python或Ruby。
英文:
In java, .
is the dereference operator: Take the thing to the left of the 'dot'. This must be a reference. If it is a reference to nothing (null), a NullPointerException occurs.
The thing to the right is the message you send to it.
Thus, sending an object the message 'make yourself global' doesn't make any sense. Something like MyClass.makeGlobal([name])
would, but java doesn't support this.
A class's structure is defined at compile time. You cannot, at runtime, add properties, or change a field from being instance to static.
The general idea of 'I want to add a property' still exists, but, not at the level you're talking about. Perhaps you want a Map<String, Integer>
for example.
Take your second snippet:
clazz c = new clazz();
c.makeObject("Hello");
c.Hello.doOperations(); // For example, if it's a String, doOperation() can be equals()
That's just not how java works. Java will first compile code (javac
), and then run it. javac does not run any code, it only compiles it. Thus, javac sees c.Hello
and stops you right there, and says: Hello? What? I have no idea what you're talking about.
That's why it is not possible to use a string literal to make a field like this.
Java isn't javascript, or python, or ruby.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论