如何定义属性? [英] How to define attributes?

查看:176
本文介绍了如何定义属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的问题是困惑我,我不知道我的想法是正确的。这是个问题。


  

由于三条直线A,B和; C。他们将能够形成
  三角形提供的任何两行的那笔总是比大于
  第三行(即A + B> C和B + C> A和A + C


  
  

    

B)。编写一个Java类三角形与以下内容:


  

 属性:三角形的三个边长
行为:


  

      
  • 构造,设置了三面传递的值的长度,构造函数应该抛出一个InvalidValueException
      当值是不能够形成三角形对象。
      

        
    • findArea()方法使用公式面积= A + B + C
    • 来计算三角形对象的区域
        

  •   

只是抬起头来问题的下一个部分是创建一个GUI。我已经做到了。

所以上面的问题是它要求我只是简单地获取值,而是来自哪里?它是通过GUI?命令行或基地?问题是不特定,所以我可以假设simmply创建准备好接受值的变量?


解决方案

为什么不只是很难code它在测试类或类似例如时刻。

 新三角(3,4,5);

在这种情况下,你有兴趣测试你的code的各种功能(有效无效对比)等我会写一个测试(或多个测试)有特定的数据集例如

  //预期是确定
   新三角形(3,4,5);   //预期异常
   新的三角形(1,1,3);

在后一种情况下,你会想捕捉异常和错误保释程序中的如果你没有得到一个异常

您可能想看看单元测试框架来帮助你完成这个。

编辑:您的修改问题,现​​在包含三角双方制定者。我觉得你不要生产领域的只读,建设这样做的,从构造函数抛出一个异常好。如果你需要制定者,那么你必须调用的validate()在某个阶段的方法,否则你可能会创建一个三角这是无效的。请注意,您不能呼吁各二传手调用验证器,因为你可能会设置多个侧面和中间状态可能是无效的。

The below question is confusing me, I am not sure if my thinking is right. This is the question

Given three straight lines a, b & c. They will be able to form a triangle provided that sum of any two lines is always greater than the third line (i.e. a + b > c and b + c > a and a + c

b). Write a Java class Triangle with the following:

Attributes: length of the three sides of the triangle 
Behaviour:

  • Constructor that sets the length of the three sides to the values passed in. The constructor should throw an InvalidValueException object when the values are not able to form a triangle.
    • findArea() method to calculate the area of the Triangle object using the formula area = a +b+c

Just a heads up the next part of the question is to create a GUI. I already did that.

So the above question is it asking me to just simply get the values, but from where? Is it through the GUI? or Commandline base? the question was not specific so can I assume that simmply create variables that are ready to accept the values?

解决方案

Why not just hardcode it for the moment in a test class or similar e.g.

new Triangle(3,4,5);

In this scenario you're interested in testing your code for various functions (valid vs. invalid) and so I would write a test (or multiple tests) that have particular sets of data e.g.

   // expected to be ok
   new Triangle(3,4,5);

   // expected exception
   new Triangle(1,1,3);

In the latter case you'd want to catch the exception and bail out of your program with an error if you don't get an exception.

You might want to check out unit test frameworks to help you with this.

Edit: Your amended question now contains setters for the Triangle sides. I think you're better off making the fields read only, doing this on construction and throwing an Exception from the constructor. If you need setters, then you have to call the validate() method at some stage otherwise you're potentially creating a Triangle which is invalid. Note that you can't call the validator upon each setter invocation since you may be setting multiple sides and an intermediate state may be invalid.

这篇关于如何定义属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆