相关文章
更多最近更新
更多这个测试,为什么总是false
2019-03-25 13:51|来源: 网路
//TPoint.java /* This is just a trivial "struct" type class -- it simply holds an int x/y point for use by Tetris, and supports equals() and toString(). We'll allow public access to x/y, so this is not an object really. */ public class TPoint { public int x; public int y; // Creates a TPoint based in int x,y public TPoint(int x, int y) { // questionable style but convenient -- // params with same name as ivars this.x = x; this.y = y; } // Creates a TPoint, copied from an existing TPoint public TPoint(TPoint point) { this.x = point.x; this.y = point.y; } // Standard equals() override public boolean equals(Object other) { // standard two checks for equals() if (this == other) return true; if (!(other instanceof TPoint)) return false; // check if other point same as us TPoint pt = (TPoint)other; return(x==pt.x && y==pt.y); } // Standard toString() override, produce // human-readable String from object public String toString() { return "(" + x + "," + y + ")"; } }
import java.util.HashSet; public class test { /** * @param args */ public static void main(String[] args) { TPoint a =new TPoint(0,1); TPoint b =new TPoint(0,1); HashSet<TPoint> setA = new HashSet<TPoint>(); HashSet<TPoint> setB = new HashSet<TPoint>(); setA.add(a); setB.add(b); boolean res = setA.equals(setB); System.out.println(res); } }
问题补充:
redstarofsleep 写道
你只重写了TPoint的equals方法,但是你调用的是Hashset的equals方法,Hashset的equals方法你并没有重写哦
但是我不用TPoint,而用awt的Point 就能通过。
import java.awt.Point; import java.util.HashSet; public class test { /** * @param args */ public static void main(String[] args) { Point a =new Point(0,1); Point b =new Point(0,1); HashSet<Point> setA = new HashSet<Point>(); HashSet<Point> setB = new HashSet<Point>(); setA.add(a); setB.add(b); boolean res = setA.equals(setB); System.out.println(res); } }
问题补充:如果不用 这个TPoint
而用awt.Point 就能通过
这是为什么呢
import java.awt.Point; import java.util.HashSet; public class test { /** * @param args */ public static void main(String[] args) { Point a =new Point(0,1); Point b =new Point(0,1); HashSet<Point> setA = new HashSet<Point>(); HashSet<Point> setB = new HashSet<Point>(); setA.add(a); setB.add(b); boolean res = setA.equals(setB); System.out.println(res); } }
相关问答
更多-
原因是因为当你明确地执行"0" == false ,双方正在转换为数字, 然后进行比较。 当你做: if ("0") console.log("ha") ,字符串值正在被测试。 任何非空字符串都为true ,而空字符串为false 。 等于(==) 如果两个操作数的类型不一样 ,JavaScript会转换操作数,然后应用严格的比较。 如果任一操作数是一个数字或一个布尔值 ,操作数可以转换为数字; 否则如果任一操作数是一个字符串 ,则另一个操作数可以转换为字符串。 如果两个操作数都是对象 ,则JavaScri ...
-
你的例子应该有效。 if x and y and z: ... 只有当x,y和z都没有计算为False ,并且在Python and短路时才会发生,所以只要一个项目失败就会返回False值。 我们可以很容易地证明这一点: >>> def test(n): ... print("test", n) ... return n ... >>> test(1) and test(2) test 1 test 2 2 >>> test(0) and test(2) test 0 0 >>> ...
-
在Unix上,进程退出代码0表示成功。 我想你用来运行测试的任何工具都将其解释为False 。 On Unix, the process exit code 0 means success. I guess whatever tool you're using to run the tests is interpreting that as False.
-
测试var可能是array / empty array / false(testing for var that might be array/empty array/false)[2023-08-20]
都不是。 返回false表示错误在非常低的级别上很好,但是在低级查询和应用程序代码之间应该有一层检查返回值并在false抛出异常。 调用代码的顶级控制器应该处理这些异常(之前没有捕获)并在生产中显示用户友好的错误页面,或在开发过程中转储调试信息,同时记录错误。 绝对没有办法在应用程序中执行三向if/elseif/else分支来检查每个数据库查询的返回值。 这是一种检查错误的难以置信的过时方法。 抛出异常,你可以使用你的第一个表单( if ($someVar) ),或者更好的是: foreach ($this ... -
你可以使用模拟。 将代码navigator.online包装在服务或实用程序类中。 在您的应用程序中使用该类。 使用此服务的模拟实现进行测试。 You can use mocking. Wrap the code navigator.online in a service or a utility class. Use that class in your application. Use a mock implementation of this service for your tests.
-
这个表达式: if (filter) { true } 是一个表达式,当filter计算结果为true时返回true,否则返回Unit () 。 这个块: { expr; false } 是一个计算expr的块,丢弃它,然后返回false。 所以: { if (filter) { true } false } 总是返回false 。 想想if (filter) { true }像expr;一样expr; This expression: if (filter) { ...
-
正则表达式根本不是使用的正确工具。 它不会告诉你每个数字是否存在。 它会告诉你是否有数字存在,或者正则表达式的不同形式可以告诉你除了数字之外什么都没有,但它不会告诉你每个数字是否存在(以任何顺序)。 这里有一个概念上简单的方法来测试任何给定的数组,以查看是否存在所有数字1-9: function test(arr) { for (var i = 1; i <= 9; i++) { if (arr.indexOf(i) === -1) { return fals ...
-
测试是否有“假像”(Test if something is “False Like”)[2023-07-30]
另一个简单的“错误的”测试是将变量评估为布尔值 PS C:\Users\Matt> $test = "" PS C:\Users\Matt> If($test){"'' Test is good"} PS C:\Users\Matt> $test = $false PS C:\Users\Matt> If($test){"False test is good"} PS C:\Users\Matt> $test = " " PS C:\Users\Matt> If($test){"Space test i ... -
你没有在任何地方使用expected - 甚至没有把它作为阶级状态的一部分。 你的断言在这里: assertTrue(value == o.add(value, num)); ......并没有断言你想要它。 当num为0时它才会起作用。仔细看看。 你应该有: // final is optional here, of course - but that's what I'd do, anyway :) private final int expected; private final int value ...
-
逻辑否定运算符在VBScript中称为Not : >> b = 1 = 1 >> WScript.Echo TypeName(b), CStr(b), CStr(Not b) >> Boolean True False >> 第二个样本: >> For Each e In Split("1 a 2") >> If Not IsNumeric(e) Then >> WScript.Echo e, "not a number" >> Else >> WScript.Echo e, ...