当我尝试比较Qt中的两个整数时出错.
if ((modus==2) & (move != -1))
error: invalid operands of types '<unresolved overloaded function type>' and 'int' to binary 'operator!='
Run Code Online (Sandbox Code Playgroud)
我需要其他运营商吗?我用谷歌搜索,但似乎Qt使用相同的.谢谢你的激光
假设我有一个Person类,具有first,middle和last name属性.我希望能够对Person对象执行两种不同类型的相等性检查:
我一直在考虑使用__eq__和__ne__单独使用这个:
Person('g', '', 'bluth') == Person('george', 'oscar', 'bluth') # False
Person('g', '', 'bluth') != Person('george', 'oscar', 'bluth') # False
Run Code Online (Sandbox Code Playgroud)
这似乎是一个简洁的解决方案,但!=并不总是返回相反的,但==让我感到紧张.这被认为是不好的做法吗?我应该避免使用运算符,只使用类似的方法consistent(self, other)吗?
示例实现:
class Person(object):
def __init__(self, first, middle, last):
self.first = first
self.middle = middle
self.last = last
def __eq__(self, other):
if type(other) is type(self):
return self.__dict__ == other.__dict__
return NotImplemented
def __ne__(self, other):
if type(other) is type(self):
return not (self._compatible(self.first, other.first) and …Run Code Online (Sandbox Code Playgroud) 我读到.equals()比较对象的值,而==比较引用(即 - 变量指向的内存位置).请参见:Java中== vs equals()之间的区别是什么?
但请注意以下代码:
package main;
public class Playground {
public static void main(String[] args) {
Vertex v1 = new Vertex(1);
Vertex v2 = new Vertex(1);
if(v1==v2){
System.out.println("1");
}
if(v1.equals(v2)){
System.out.println("2");
}
}
}
class Vertex{
public int id;
public Vertex(int id){
this.id = id;
}
}
Run Code Online (Sandbox Code Playgroud)
输出:(
没什么)
不应该打印2吗?
我想在我的应用程序中检查Integer相等,但遇到了一个奇怪的行为.在某些时候我的应用程序正常工作,但在某些时候它失败了.所以我在这里写了一个测试代码
public class EqualityTest {
public static void main(String args[]) {
Integer a = 100;
Integer b = 100;
Integer c = 1000;
Integer d = 1000;
if (a == b) {
System.out.println("a & b are Equal");
}
else {
System.out.println("a & b are Not Equal");
}
if (c == d) {
System.out.println("c & d are Equal");
} else {
System.out.println("c & d are Not Equal");
}
}
}
Run Code Online (Sandbox Code Playgroud)
产量
a & b are Equal
c & d are Not …Run Code Online (Sandbox Code Playgroud) 我从jls找到了引用:
等于运算符可用于将两个可转换的操作数(第5.1.8节)与数字类型,或两个类型为boolean或Boolean的操作数,或两个操作数(分别为引用类型或null类型)进行比较.所有其他情况都会导致编译时错误.
但是这段代码
String str= "";
Number num = 1;
System.out.println(str == num);
Run Code Online (Sandbox Code Playgroud)
每个操作数都是参考!
说它是不兼容的类型.
jls在哪里说这些类型应该兼容?
我有两个同一个类的实例.
public class Gilda {
private String nome;
public Gilda(String nome) {
this.nome = nome;
}
// More stuff
}
Run Code Online (Sandbox Code Playgroud)
当尝试通过Object.equals(Object)方法比较它们时,它返回false.这很奇怪,因为nome在这两个实例上具有相同的值.
比较如何运作?这是预期的行为吗?我应该覆盖那种方法吗?
为什么这样(Firebug控制台):
> "?" == ";"
> false
Run Code Online (Sandbox Code Playgroud)
但是这个:
> ';' == ';'
> true
Run Code Online (Sandbox Code Playgroud)
谁知道为什么会这样?
我正在尝试检查字符串是否为空,小于或等于9位数,或最多10位数.但它始终遵循else if (str.length <= 9).
if (str = ''){
console.log("The string cannot be blank");
} else if (str.length <= 9) {
console.log("The string must be at least 9 characters long");
} else if (str.length <= 10) {
console.log("The string is long enough.");
}
Run Code Online (Sandbox Code Playgroud)
无论我投入什么,我总能得到The string must be at least 9 characters long.为什么?
public class Test {
public static void main(String[] args) {
Object o1 = new Object();
Object o2 = new Object();
System.out.print((o1 == o2) + " " + (o1.equals(o2)));
}
}
Run Code Online (Sandbox Code Playgroud)
我以不同的答案阅读:
该
==运营商测试两个变量是否具有相同的引用(又名指向一个内存地址)。而该
equals()方法测试两个变量是否引用具有相同状态(值)的对象。
在这里,由于 o1和o2引用了两个不同的对象,我得到了为什么 ==返回false。
但是,这两个对象都是使用Object该类的默认构造函数创建的,因此具有相同的值。为什么该equals()方法返回false?
该snmp.h头文件包含的定义,AsnObjectIdentifier结构和遗憾的是这个结构不相等运算符重载。我想AsnObjectIdentifier成为的钥匙,std::map但问题find()是无法在地图上找到钥匙。我定义了一个自定义比较器AsnObjectIdentifierComparator,用作std :: map声明的第三个模板参数。该方案的最小可复制代码如下:
#include <iostream>
#include <string>
#include <map>
using namespace std;
typedef unsigned int UINT;
typedef struct {
UINT idLength;
UINT * ids;
} AsnObjectIdentifier;
struct AsnObjectIdentifierComparator {
bool operator()(const AsnObjectIdentifier& left, const AsnObjectIdentifier& right) const {
UINT* leftOidArr = left.ids, * rightOidArr = right.ids;
UINT smallerOidLen = (left.idLength < right.idLength ? left.idLength : right.idLength);
for (UINT i = 0; i < smallerOidLen; i++) {
if (leftOidArr[i] …Run Code Online (Sandbox Code Playgroud)