设置接口问题和对象

发布于 2024-10-23 16:35:18 字数 1034 浏览 1 评论 0原文

抱歉,主题主题不好,但我找不到正确的内容。 (如果题目有误解,请指正)。

所以我的问题是: 我在 Circle 和 Square 之后有接口 Shape 和两个类实现。 我需要编写一个收集圆形和方形的类。它必须是不会添加任何重复对象的收集方法之一。在阅读了 java 文档后,我选择了“set”。但我不确定这是否是个好主意。 (我可以使用四种方法之一:map.set.list.或queque)。

毕竟我创建了另一个名为 ShapeSet 的类和方法,

public void ShapeSet(Shape Set)

它看起来像这样:

public class ShapeSet {
    public ShapeSet() {}

    Set <Shape> setting; //is it wrong? 
    public void addShape(Shape shape) {
    setting.add(shape);
    }
...

}

之后我认为我做对了,我在主类中创建了定义正方形和圆形的构造函数。我还创建了ShapeSet ss。

public static void main(String[] args) {
        // TODO code application logic here
        ShapeSet ss = new shapes.ShapeSet();
        Shape c = new Circle(3);
        Shape s = new Square(4);
        ss.addShape(c);
        ss.addShape(s);
        ss.iterator();
    }

但是在运行程序时,我在 ss.addShape(x) 行上遇到错误,netbeans 抱怨他发现了 null 异常。为什么? ;( 我认为输入到方法 shapeset 的类型是错误的,并且声明集合设置的位置可能不好。但是如何解决这个问题?我是 java 新手。我很感谢您的帮助。提前致谢。

sorry for bad Subject of topic but i couldnt find out what to write proper. (please correct topic if it gives missunderstood).

So my problem is:
I have interface Shape and two classes implements after this Circle and Square.
I need to write class which will collect Circle and Square. It must be one of methods of collecting which will not add any duplicate objects. I've chosen "set" after reading in documentation of java. But i am not sure if it was good idea. (i can use one of the four methods: map. set. list. or queque).

After all I created another class named ShapeSet and method

public void ShapeSet(Shape Set)

It looks like this:

public class ShapeSet {
    public ShapeSet() {}

    Set <Shape> setting; //is it wrong? 
    public void addShape(Shape shape) {
    setting.add(shape);
    }
...

}

After that thinking that i am doing right i created in main class, constructor defining square and circle. I created also ShapeSet ss.

public static void main(String[] args) {
        // TODO code application logic here
        ShapeSet ss = new shapes.ShapeSet();
        Shape c = new Circle(3);
        Shape s = new Square(4);
        ss.addShape(c);
        ss.addShape(s);
        ss.iterator();
    }

But while running program i got error on line ss.addShape(x), netbeans complains that he found null exception. Why? ;( I think types inputed to method shapeset was wrong and maybe bad position of declaring set setting. But how to fix that? I am total novice in java. I appreciate for a help. Thanks in advance.

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

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

发布评论

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

评论(3

旧城空念 2024-10-30 16:35:18

关于 NullPointerException 的答案可能是因为在您的 ShapeSet 类中,您没有分配成员字段“设置”,如

Set <Shape> setting = new HashSet<Shape>();

我的问题是,为什么要有一个 ShapeSet 类?看来您只需要将 Set 作为具有 main 方法的类中的字段即可。

The answer about the NullPointerException is probably because in your ShapeSet class, you haven't allocated the member field 'setting' as in

Set <Shape> setting = new HashSet<Shape>();

The question I have however, is why have a ShapeSet class at all? It seems you only need to have Set as a field in the class that has the main method.

以可爱出名 2024-10-30 16:35:18

您忘记初始化字段设置

public class ShapeSet {
    public ShapeSet() {}

    Set <Shape> setting = new HashSet<Shape>();

    public void addShape(Shape shape) {
        setting.add(shape);
    }
...

}

You forgot to initialize your field setting

public class ShapeSet {
    public ShapeSet() {}

    Set <Shape> setting = new HashSet<Shape>();

    public void addShape(Shape shape) {
        setting.add(shape);
    }
...

}
梦忆晨望 2024-10-30 16:35:18

我同意@MeBigFatGuy - 你不需要你的 ShapeSet 类。你可以像这样编写你的 main 代码:

public static void main(String[] args) {
    Set<Shape> ss = new HashSet<Shape>(); // or some other Set concrete class
    Shape c = new Circle(3);
    Shape s = new Square(4);
    ss.add(c);
    ss.add(s);
    ss.iterator(); // actually, you'd want to do something with the iterator
}

I agree with @MeBigFatGuy - you don't need your ShapeSet class. You can code your main like this:

public static void main(String[] args) {
    Set<Shape> ss = new HashSet<Shape>(); // or some other Set concrete class
    Shape c = new Circle(3);
    Shape s = new Square(4);
    ss.add(c);
    ss.add(s);
    ss.iterator(); // actually, you'd want to do something with the iterator
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文