设置接口问题和对象
抱歉,主题主题不好,但我找不到正确的内容。 (如果题目有误解,请指正)。
所以我的问题是: 我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
关于 NullPointerException 的答案可能是因为在您的 ShapeSet 类中,您没有分配成员字段“设置”,如
我的问题是,为什么要有一个 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
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.
您忘记初始化字段
设置
You forgot to initialize your field
setting
我同意@MeBigFatGuy - 你不需要你的
ShapeSet
类。你可以像这样编写你的 main 代码:I agree with @MeBigFatGuy - you don't need your
ShapeSet
class. You can code your main like this: