设计一个合适的类
我想设计一个 OilPump 类,它有 2 个属性。Id 和容量。现在有一个约束,oilpump 的容量应为 <6 和 >0 。
1)如下所示设计我的类是一个好的做法吗?如果不满足条件,则从结构中抛出异常。
2)如果我提供一个setter方法,它会是什么样子?
3)我应该开发一个实际上从 setCapicity 调用的辅助方法 validateCapacity 吗?
public class OilPump {
private String ID;
private int Capacity;
public OilPump(String id,int c)throws MyException{
if(id.length()==6 && (c<6 && c>0)) {Capacity=c;ID=id;}
else{
throw new MyException("Invalid OilpumpID or Capacity");
//System.out.println("Invalid OilpumpID");
}
}
void start()
{
System.out.println("Oil Pump is started");
}
void stop()
{
System.out.println("Oil Pump is Stopped");
}
}
I want to design a class OilPump which has 2 attribute.Id and capacity.Now there is a constraint that oilpump should have capacity <6 and >0 .
1) Is it a a good practice to design my class as show below?Throwing exception out of constructure if it doesnot satisfy the onditions.
2) If i provide a setter method what will it look like?
3) Should i develop a helper method validateCapacity that is actually called from setCapicity?
public class OilPump {
private String ID;
private int Capacity;
public OilPump(String id,int c)throws MyException{
if(id.length()==6 && (c<6 && c>0)) {Capacity=c;ID=id;}
else{
throw new MyException("Invalid OilpumpID or Capacity");
//System.out.println("Invalid OilpumpID");
}
}
void start()
{
System.out.println("Oil Pump is started");
}
void stop()
{
System.out.println("Oil Pump is Stopped");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看起来不错。你有私有字段和公共方法,这是正确的。我要指出的唯一事情是:
1) java 约定适用于驼峰命名法,因此您的字段应该是
id
和capacity
。2)现在你的类是不可变的(即其中的数据不能更改)。如果你想要这样,很好,但是如果想要能够修改容量,你应该添加 java-bean 样式的 setter 和 getter。
looks fine. you have private fields and public methods, which is right. The only things I'll point out are:
1) java convention is for camelCasing, so your fields should be
id
andcapacity
.2) Right now your class is immutable (i.e. the data in it cant be changed). If you want that, fine, but if want to be able to modify capacity you should add java-bean style setters and getters.
1)构造函数不应抛出异常(请记住,允许用户继续执行可能导致异常的步骤是不好的做法:考虑数据契约);
2)您可以为您的设置器使用 SetCapacity(Int32) ;
3)是的,为特定功能创建单独的方法是OO设计的目标;
希望有帮助,
--KRG
1) Constructors should not throw exceptions (remember it's bad practice to allow the user to proceed in an step that would cause an exception: consider data contracts);
2) You could use a SetCapacity(Int32) for your setter;
3) Yes, creating separate methods for specific functionality is the objective of OO design;
Hope it helps,
--KRG
您应该以小写字母开头变量/字段。
尽管您所拥有的并没有错,但是如果您在参数错误时抛出未经检查的异常 IllegalArgumentException 而不是自定义的检查异常,则会使类的使用更简单。然后您应该提供适当的 javadoc 来定义可接受的参数。
除非您希望类是可变的,否则不需要 setter/getter。
如果您的验证仍然如此简单,我不会打扰验证方法,但如果它变得更复杂,我会这样做。无论如何,这样做肯定没有什么错。
如果需要的话,通常会预先进行测试并失败,然后只需在 if 块之外执行其余代码即可。
You should start variables/fields with lowercase.
Although what you have isn't wrong, it makes class usage simpler if you throw the unchecked exception IllegalArgumentException when the parameters are wrong instead of a custom, checked exception. Then you should just provide appropriate javadoc to define what the acceptable parameters are.
The setters/getters are not required unless you want the class to be mutable.
I would not bother with the validate method if your validation remains this trivial, but I would if it got any more complex. There certainly isn't anything wrong with doing it anyway.
It would also be typical to test and fail if necessary up front, then simply do the rest of your code outside of the if block.
也许是个人风格的问题,但我会创建一个封装验证并将构造函数设为私有的工厂方法。如果将来需要,这种方式可以更轻松地在层次结构中使用 OilPump 类。该类将是这样的:
Perhaps a question of personal style, but I would create a factory method that encapsulates the validation and make the constructor private. That way is is easier to use the OilPump class in a hierarchy if it is required in the future. The class would be something like this: