使用HashMap来存储实例变量?

发布于 2024-12-11 02:10:19 字数 675 浏览 0 评论 0原文

我想创建一个基类,我的程序中的所有类都将扩展它。我想做的一件事是找到一种统一的方法来存储对象内的所有实例变量。

我想出的是使用 HashMap 来存储对象的键/值对,然后通过 get 和 set 方法公开这些值。

到目前为止,我的代码如下:

package ocaff;

import java.util.HashMap;

public class OcaffObject {

    private HashMap<String, Object> data;

    public OcaffObject() {
        this.data = new HashMap<String, Object>();
    }

    public Object get(String value) {
        return this.data.get(value);
    }

    public void set(String key, Object value) {
        this.data.put(key, value);
    }

}

虽然从功能上来说这是可行的,但我很好奇这个实现是否存在任何真正的问题,或者是否有更好的方法来做到这一点?

在我的日常工作中,我是一名 PHP 程序员,我的目标是在 Java 中模仿我在 PHP 中使用的功能。

I would like to create a base class that all classes in my program will extend. One thing I wanted to do was find a uniform way to store all instance variables inside the object.

What I have come up with is to use a HashMap to store the key/value pairs for the object and then expose those values through a get and set method.

The code that I have for this so far is as follows:

package ocaff;

import java.util.HashMap;

public class OcaffObject {

    private HashMap<String, Object> data;

    public OcaffObject() {
        this.data = new HashMap<String, Object>();
    }

    public Object get(String value) {
        return this.data.get(value);
    }

    public void set(String key, Object value) {
        this.data.put(key, value);
    }

}

While functionally this works, I am curious if there are any real issues with this implementation or if there is a better way to do this?

In my day to day work I am a PHP programmer and my goal was to mimic functionality that I used in PHP in Java.

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

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

发布评论

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

评论(2

盗心人 2024-12-18 02:10:19

我认为这不是处理你的意思的好方法。
从我的角度来看,java 编程与 php 编程有很大不同。

您需要使用干净的面向对象编程的真正范式来保持事物的干净和强类型。

我想到了这种技术的一些问题,这里有一些,不按重要性顺序排列。

  1. 您遇到的第一个问题是性能和内存占用:这将消耗大量内存并且性能会非常糟糕。

  2. 第二个问题是并发,HashMap不是线程安全的。

  3. 第三个问题是类型安全:你不再有类型安全了,你可以向字段写入任何你想要的内容,但没有人检查它,这是一个真正的反模式。

  4. 第四个问题是调试...将很难调试您的代码。

  5. 第五个问题是:每个人都可以写入和读取任何知道他名字的字段。

  6. 第六个问题:当您更改哈希集中的字段名称时,您不会收到任何类型的编译时错误,只会收到奇怪的运行时行为。重构将变得不可能。

键入字段更加有用和干净。

I don't think this is a good way to deal with what you mean.
Programming in java is quite different than programming in php, from my point of view.

You need to keep things clean and strongly typed, using the real paradigm of clean object oriented programming.

Some problems with this technique comes to my mind, here are some, not in importance order.

  1. First problem you have with this is performance and memory footprint: this will consume a lot of memory and will perform very badly.

  2. Second problem is concurrency, HashMap is not thread safe.

  3. Third problem is type safety: you don't have type safety anymore, you can write to a field whatever you want and no one is checking it, a real anti-pattern.

  4. Fourth problem is debugging... it will be hard to debug your code.

  5. Fifth problem is: everyone can write and read any field knowing his name.

  6. Sixth problem: when you change the name of a field in the hash set you don't get any kind of compile time error, you only get strange run-time behavior. Refactoring will become impossible.

Typed fields are much more useful and clean.

垂暮老矣 2024-12-18 02:10:19

如果您花时间为此开设课程,我会简单地添加您作为课程成员所需的内容。这将使您能够在编译时检查类成员,从而大大减少细微的错误。

If you're taking the time to make a class for this, I would simply add what you need as members of the class. This will give you compile time checking of your class members, greatly reducing your subtle bugs.

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