Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何打开equals语句以接受Java中的多个参数? 我有一个梦想。。。。_Java_Variables - Fatal编程技术网

如何打开equals语句以接受Java中的多个参数? 我有一个梦想。。。。

如何打开equals语句以接受Java中的多个参数? 我有一个梦想。。。。,java,variables,Java,Variables,在这个梦中,我可以替换如下结构: if(aLongVariableName.equals(classInstance.aPropertyOfThatInstance) || aLongVariableName.equals(classInstance.aDifferentProperty)){ ... } 与 我认为后者更简洁,更容易阅读。我确信有些事情应该是可能的,因为方法可以用这种签名声明public void readUnlimitedVariables(Var

在这个梦中,我可以替换如下结构:

if(aLongVariableName.equals(classInstance.aPropertyOfThatInstance) ||  
   aLongVariableName.equals(classInstance.aDifferentProperty)){  
    ...  
}

我认为后者更简洁,更容易阅读。我确信有些事情应该是可能的,因为方法可以用这种签名声明
public void readUnlimitedVariables(Variable…vars)
,它使用逗号分隔的变量(而不是
|
甚至
&&

这可以打开到任何返回布尔值的对象,例如:

if(myLovelyInstance instanceof AwesomeClass && EpicClass){
    ...
}
其中
myLovelyInstance
implements
AwesomeClass和EpicClass


这在Java中可能吗?到目前为止,我还没有在谷歌上找到任何东西。如果不是在vanilla Java中,是否有任何第三方软件包(如Lombok或lambdaJ)允许这样做?如果不是,这是否是任何语言的特征?这对我来说似乎非常直观,但我在任何地方都没有见过它。

这在Java中是不可能的。
|
的参数必须是
布尔值
s,现在决定
.equals
已经太迟了,因为它不能同时得到两个值,只能得到一个
布尔值

为了使之成为可能,必须更改Java语言规范。我看不出会有这样的趋势,所以你应该告诉Java的维护者你想要这个。即使他们接受了它,也可能需要几年才能达到语言规范,然后几年才能部署到大多数Java安装中

这在Java中可能吗

没有

|
运算符接受两个布尔操作数。这是无法改变的。Java不允许重载运算符,也不允许以其他方式“扩展语言语法”

最接近的方法是为如下等式定义重载:

public boolean equals(Object ... others) {
    for (Object other: others) {
        if (this.equals(other)) {
            return true;
        }
    }
    return false;
}
if (aLongVariableName.equals(classInstance.aDifferentProperty,
                             classInstance.anotherProperty)) {
    ...  
}
if (userInputFile != null && userInputFile.getAbolutePath() != null 
      && user != null && user.getAccessRules() != null 
      && userHasAccess(user, userInputFile) {
    doStuff();
}
if (isValid(userInputFile, user)) {
    doStuff();
}
然后像这样使用它:

public boolean equals(Object ... others) {
    for (Object other: others) {
        if (this.equals(other)) {
            return true;
        }
    }
    return false;
}
if (aLongVariableName.equals(classInstance.aDifferentProperty,
                             classInstance.anotherProperty)) {
    ...  
}
if (userInputFile != null && userInputFile.getAbolutePath() != null 
      && user != null && user.getAccessRules() != null 
      && userHasAccess(user, userInputFile) {
    doStuff();
}
if (isValid(userInputFile, user)) {
    doStuff();
}
。。。这一点也不接近。(国际海事组织,这也是一个非常糟糕的主意)


事实上,我想不出有哪种语言支持类似于您所建议的语法。我觉得有太多的歧义,这是一个合理的结构。

< P>你能不能考虑一个坏习惯?(我对Java还比较陌生,所以我可能会发明轮子,或者只是做些废话*t:)

公共类主{
公共静态布尔等式变量(类型为toComp,类型为…args){
对于(类型arg:args){
if(toComp.equals(arg)){
返回true;
}
}
返回false;
}
公共静态布尔等式变量AND(类型为toComp,类型为…args){
对于(类型arg:args){
如果(!toComp.equals(arg)){
返回false;
}
}
返回true;
}
公共静态void main(字符串参数[]){
字符串testcaseAllA[]=新字符串[]{“a”、“a”、“a”、“a”、“a”、“a”、“a”、“a”、“a”、“a”、“a”、“a”、“a”};
字符串testcaseWithB[]=新字符串[]{“b”、“a”、“a”、“a”、“a”、“a”、“a”、“a”、“a”、“a”、“a”};
System.out.println(等变量和(“a”,testcaseAllA));
System.out.println(相等变量和(“b”,testcaseWithB));
System.out.println(相等变量(“b”,testcaseWithB));
System.out.println(相等变量(“a”,testcaseAllA));
}
}

如果答案不是,OP可能会发现这很有用?

为了可读性,如果检查的数量足够少,您可以只使用简单的变量

例如(对于任何类型的对象都相同)

如果使用字符串,正则表达式是另一种方式:

if (extension.matches("PDF|HTML")) {
   // do your processing
}

有时候简单比聪明好

不要忘记电子封装

您通常可以使用实例方法清理代码:

与冗长、混乱的if语句不同,您可以这样做:

if (classInstance.matches(reallyLongVariableName)) {
    doStuff();
}
你用一种方法埋葬了丑陋:

class LongNamedObject {

    String propertyWithNameThatIsLong;
    String anotherLongNamedProperty;

    public boolean matches(String s) {
        return ( s.equals(propertyWithNameThatIsLong) ||
                 s.equals(anotherLongNamedProperty) );
    }
}
private boolean isValid(File f, User u) {
    if (u == null || f == null) {
        return false;
    }
    if (f.getAbsolutePath() == null) {
        return false;
    }
    if (u.getAccessRules() == null) {
        return false;
    }
    return userHasAccess(u, f);
}
编写具有表达性名称的布尔方法是使代码更易于阅读的一种好方法

你经常会看到这样的混乱局面:

public boolean equals(Object ... others) {
    for (Object other: others) {
        if (this.equals(other)) {
            return true;
        }
    }
    return false;
}
if (aLongVariableName.equals(classInstance.aDifferentProperty,
                             classInstance.anotherProperty)) {
    ...  
}
if (userInputFile != null && userInputFile.getAbolutePath() != null 
      && user != null && user.getAccessRules() != null 
      && userHasAccess(user, userInputFile) {
    doStuff();
}
if (isValid(userInputFile, user)) {
    doStuff();
}
它可能是这样的:

public boolean equals(Object ... others) {
    for (Object other: others) {
        if (this.equals(other)) {
            return true;
        }
    }
    return false;
}
if (aLongVariableName.equals(classInstance.aDifferentProperty,
                             classInstance.anotherProperty)) {
    ...  
}
if (userInputFile != null && userInputFile.getAbolutePath() != null 
      && user != null && user.getAccessRules() != null 
      && userHasAccess(user, userInputFile) {
    doStuff();
}
if (isValid(userInputFile, user)) {
    doStuff();
}
你用一种方法埋葬了丑陋:

class LongNamedObject {

    String propertyWithNameThatIsLong;
    String anotherLongNamedProperty;

    public boolean matches(String s) {
        return ( s.equals(propertyWithNameThatIsLong) ||
                 s.equals(anotherLongNamedProperty) );
    }
}
private boolean isValid(File f, User u) {
    if (u == null || f == null) {
        return false;
    }
    if (f.getAbsolutePath() == null) {
        return false;
    }
    if (u.getAccessRules() == null) {
        return false;
    }
    return userHasAccess(u, f);
}

您可以使用varargs和Lombok的扩展方法获得非常类似的结果:

package test;

@lombok.experimental.ExtensionMethod({test.Util.class})
public class Work {
  public static void main(String[] args) {
    System.out.println("a".eqOr("b", "c", "a"));
    System.out.println("a".eqOr("b", "c"));
    System.out.println("a".eqAnd("a", "a", "a"));
    System.out.println("a".eqAnd("a", "a", "c"));
  }
}
  • 在实用程序类中定义以下静态方法:

    package test;
    
    public final class Util {
        private Util() {}
    
        public static boolean eqOr(Object tthis, Object ... those) {
            for (Object that : those) {
                if (tthis.equals(that)) return true;
            }
            return false;
        }
    
        public static boolean eqAnd(Object tthis, Object ... those) {
            for (Object that : those) {
                if (!tthis.equals(that)) return false;
            }
            return true;
        }
    }
    
  • 用于使
    eqAnd
    eqOr
    成为扩展方法:

    package test;
    
    @lombok.experimental.ExtensionMethod({test.Util.class})
    public class Work {
      public static void main(String[] args) {
        System.out.println("a".eqOr("b", "c", "a"));
        System.out.println("a".eqOr("b", "c"));
        System.out.println("a".eqAnd("a", "a", "a"));
        System.out.println("a".eqAnd("a", "a", "c"));
      }
    }
    
    显然,如果您不喜欢lombok,可以将其作为常规静态方法使用


  • 为什么不将它封装到一个类中并重写equals方法呢?@BogdanEmilmaresan,因为我经常想用字符串来实现这一点,而且它们似乎工作得很好,没有我的干扰。如果我知道我会怎么做。我能想到的最好的签名是so
    readUnlimitedVariables(Variable…vars,String and choice)
    ,但即使这样,我也不知道如何实现。可能是@seanhodges的重复是的,这很相似,但我追求的不仅仅是字符串。@bogdanemilisan我收回这一点。这将是伟大的工作与许多不同的东西。查看我的编辑。是的,这不是有效的Java。不能使用泛型参数生成方法。您必须创建一个泛型类并实例化它的至少一个对象,才能执行
    equalvariadicand
    equalvariadicor
    方法。它实际上是有效的代码,可以编译和运行。我只是想问它是否有效,是否有java语言等等。证据:我被纠正了。出于某种原因,我认为Java中不存在泛型方法。@E_net4,我之所以将其设置为泛型方法,是因为我不确定是否只使用参数
    对象
    ,可以通过比较某个类与另一个类的列表来调用方法,这对我来说似乎有潜在的危险,泛型提供了一种简洁的方式来控制类型。在这种情况下,大多数比较在面对对象时返回false