我可以在 Ruby / Java / C# 中创建这样的对象吗?

发布于 2024-10-01 13:26:12 字数 83 浏览 6 评论 0原文

由于 NguyenThe 不是方法,您能否从名为 Trung.NguyenThe 的类创建一个对象?

Could you create an object from a class named Trung.NguyenThe since NguyenThe isn't a method ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

余厌 2024-10-08 13:26:12

在 Java C# 和 Ruby 中,无法创建名称中带有 . 的变量。

// C# and Java
Object Trung.NguyenThe = "";
# Ruby
Trung.NguyenThe = ""

该变量名在所有三种语言中都是非法的。

如果您想做类似的事情,请使用下划线_

// C# and Java
Object Trung_NguyenThe = "";
# Ruby
Trung_NguyenThe = ""

In Java C# and Ruby, you cannot create variables with a . in the name.

// C# and Java
Object Trung.NguyenThe = "";
# Ruby
Trung.NguyenThe = ""

That variable name is illegal in all three languages.

If you want to do something similar to this, use an underscore _.

// C# and Java
Object Trung_NguyenThe = "";
# Ruby
Trung_NguyenThe = ""
活雷疯 2024-10-08 13:26:12

首先,对象没有名称。类和变量可以。

所以我假设你问的是用句点命名(基于你最后的评论)......

直接的答案是“否”,但可以使用命名空间/包或内部来模拟类。

命名空间:

namespace Trung {
    public class NguyenThe {}
}

// Usage:
namespace Whatever {
    public class Client {
        public main() {
            var x = new Trung.NguyenThe();
        }
    }
}

内部类:(

namespace Whatever {
    public class Trung {
        public class NguyenThe {}
    }

    // Usage:
    public class Client {
        public main() {
            var x = new Trung.NguyenThe();
        }
    }
}

注意:这是 C# 代码,但我确信在 Java 中也可以完成同样的操作。我猜 Ruby 也有相同的概念。)

First of all, objects do not have names. Classes and variables do.

So I'll assume you're asking about naming classes with periods (based on your last comment)...

The straight answer is "no", but one can emulate that using namespaces/packages or inner classes.

Namespace:

namespace Trung {
    public class NguyenThe {}
}

// Usage:
namespace Whatever {
    public class Client {
        public main() {
            var x = new Trung.NguyenThe();
        }
    }
}

Inner class:

namespace Whatever {
    public class Trung {
        public class NguyenThe {}
    }

    // Usage:
    public class Client {
        public main() {
            var x = new Trung.NguyenThe();
        }
    }
}

(Note: This is C# code, but I'm sure the same could be done in Java. I guess Ruby has the same concepts also.)

瀞厅☆埖开 2024-10-08 13:26:12

在 Ruby 中,没有类名这样的东西。类只是像其他对象一样的对象,就像任何其他变量一样被分配给变量。

Trung.NguyenThe 不是合法的变量名,因此这是不可能的:

class Object
  klass = Class.new do
    def to_s; 'Hello' end
  end
  const_set :'Trung.NguyenThe', klass
  # NameError: wrong constant name Trung.NguyenThe
end

当然,您可以创建一个响应 NguyenThe 的对象消息与类并将该对象分配给名为 Trung 的变量,但这不是您要问的:

klass = Class.new do
  def to_s; 'Hello' end
end
(Trung = Object.new).define_singleton_method(:NguyenThe) { klass }

puts Trung.NguyenThe.new
# Hello

在 C# 和 Java 中,类名中的句点也是非法的。 C# 有一个转义机制,允许您使用其他保留字作为名称,但此机制不允许使用非法名称,只能使用保留名称。

有人提出了在 Java 名称中提供更多符号自由的建议,但到目前为止,这些建议尚未实施。

In Ruby, there's no such thing as a class name. Classes are just objects like every other object which get assigned to variables just like any other variable.

Trung.NguyenThe is not a legal variable name, therefore this is impossible:

class Object
  klass = Class.new do
    def to_s; 'Hello' end
  end
  const_set :'Trung.NguyenThe', klass
  # NameError: wrong constant name Trung.NguyenThe
end

You could, of course, create an object which responds to a NguyenThe message with a class and assign that object to a variable named Trung, but that's not what you are asking about:

klass = Class.new do
  def to_s; 'Hello' end
end
(Trung = Object.new).define_singleton_method(:NguyenThe) { klass }

puts Trung.NguyenThe.new
# Hello

In C# and Java, periods are illegal in class names as well. C# has an escaping mechanism that allows you to use otherwise reserved words as names, but this mechanism doesn't make it possible to use illegal names, only reserved ones.

There are proposals for more symbolic freedom in Java names, but so far, they have not yet been implemented.

粉红×色少女 2024-10-08 13:26:12

当然。创建一个名为 Trung 的包。在此包中创建一个名为 NguyenThe 的类。

或者您是否假设 Trung 已经是一个类名?

Sure. Create a package called Trung. Within this package create a class called NguyenThe.

Or are you assuming that Trung is already a class name?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文