📌  相关文章
📜  Java中的 CompositeName equals() 方法及示例

📅  最后修改于: 2022-05-13 01:55:48.901000             🧑  作者: Mango

Java中的 CompositeName equals() 方法及示例

javax.naming.CompositeName 类equals()方法用于将此 CompositeName 与作为参数传递的指定对象进行比较,并检查两个对象是否相等。如果两个对象相等,则 equals() 方法返回 true,否则返回 false。如果传递的 obj 为 null 或不是复合名称,则该方法返回 false。如果一个复合对象中的每个组件都等于另一个复合对象中的相应组件,则两个复合对象是相等的。

句法:

public boolean equals(Object obj)

参数:此方法接受 obj ,它是要比较的可能为 null 的对象。

返回值:如果 obj 等于此复合名称,则此方法返回 true,否则返回 false。

下面的程序说明了 CompositeName.equals() 方法:
方案一:

// Java program to demonstrate
// CompositeName.equals()
  
import javax.naming.CompositeName;
import javax.naming.InvalidNameException;
  
public class GFG {
    public static void main(String[] args)
        throws InvalidNameException
    {
  
        // create Composite name object
        CompositeName compositeName1
            = new CompositeName("x/y/a/b");
        CompositeName compositeName2
            = new CompositeName("x/y/a/b");
  
        // apply equals()
        boolean flag
            = compositeName1.equals(
                compositeName2);
  
        // print value
        if (flag)
            System.out.println("CompositeName1 is "
                               + "equal to CompositeName2");
        else
            System.out.println("CompositeName1 is "
                               + "not equal to CompositeName2");
    }
}
输出:
CompositeName1 is equal to CompositeName2

方案二:

// Java program to demonstrate
// CompositeName.equals() method
  
import javax.naming.CompositeName;
import javax.naming.InvalidNameException;
  
public class GFG {
    public static void main(String[] args)
        throws InvalidNameException
    {
  
        // create Composite name object
        CompositeName compositeName1
            = new CompositeName("c/d/a/b");
        CompositeName compositeName2
            = new CompositeName("e/d/a/b");
  
        // apply equals()
        boolean flag
            = compositeName1.equals(
                compositeName2);
  
        // print value
        if (flag)
            System.out.println("CompositeName1 is "
                               + "equal to CompositeName2");
        else
            System.out.println("CompositeName1 is "
                               + "not equal to CompositeName2");
    }
}
输出:
CompositeName1 is not equal to CompositeName2

参考资料:https://docs.oracle.com/javase/10/docs/api/javax/naming/CompositeName.html#equals(Java.lang.Object)