用Java中的方法调用来创建一个对象。

huangapple go评论56阅读模式
英文:

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();`&lt;br&gt;
How can we do that? And, if we want to move the `Object [Name]` from the method to a public variable in class `Clazz` ?&lt;br&gt;
Example:&lt;br&gt;

public class clazz {
void MakeObject(String name) {
Object[name] = new Object();
[name].UpgradeToClassVariable(); // It should be a global variable
}
}

Usage of `clazz` :&lt;br&gt;

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&lt;String, Integer&gt; for example.

Take your second snippet:

clazz c = new clazz();
c.makeObject(&quot;Hello&quot;);
c.Hello.doOperations(); // For example, if it&#39;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.

huangapple
  • 本文由 发表于 2020年9月30日 19:32:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/64136697.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定