Java 可变长度参数列表中的类型检查

Java 可变长度参数列表中的类型检查,java,Java,我需要创建一个TreeNode类,它将能够存储两种类型的child:String和TreeNode。孩子的数量不是固定的 我想以如下方式创建TreeNode对象: TreeNode a = new TreeNode("str", new TreeNode("str2"), "str3"); //Correct TreeNode b = new TreeNode(a, "str4); //Correct TreeNode c = new TreeNode(54); //Wrong 如何在编译时使

我需要创建一个TreeNode类,它将能够存储两种类型的child:String和TreeNode。孩子的数量不是固定的

我想以如下方式创建TreeNode对象:

TreeNode a = new TreeNode("str", new TreeNode("str2"), "str3"); //Correct
TreeNode b = new TreeNode(a, "str4); //Correct
TreeNode c = new TreeNode(54); //Wrong
如何在编译时使用通配符或其他方法进行参数类型检查

我不合适的运行时解决方案:

private static final boolean debug = "true".equals(System.getProperty("debug"));

public <T> TreeNode (T... childs) {
    if (debug) {
        for (Object child : childs) {
            if (!(child instanceof String || child instanceof TreeNode)) {
                throw new RuntimeException("Type of childs must me Tree or String");
            }
        }
    }
}
试试这个

public <T> TreeNode (Object... childs) {
  //code
}
试试这个

public <T> TreeNode (Object... childs) {
  //code
}

您应该尝试找到一个可以添加到树中的基类型。然后,从中派生出具体的节点类型:

abstract class Node { }
class TreeNode extends Node {
  public TreeNode(Node... children) {
    // ...
  }
}
class StringNode extends Node { 
  public StringNode(String value) {
    // ...
  }
}
用法:

TreeNode a = new TreeNode(
  new StringNode("str"), 
  new TreeNode(new StringNode("str2")), 
  new StringNode("str3"));

您应该尝试找到一个可以添加到树中的基类型。然后,从中派生出具体的节点类型:

abstract class Node { }
class TreeNode extends Node {
  public TreeNode(Node... children) {
    // ...
  }
}
class StringNode extends Node { 
  public StringNode(String value) {
    // ...
  }
}
用法:

TreeNode a = new TreeNode(
  new StringNode("str"), 
  new TreeNode(new StringNode("str2")), 
  new StringNode("str3"));

如果仍希望移植某种程度的编译时类型安全性,可以执行以下操作:

public class AorB<A, B> {
  private final Object _instance;
  private final boolean _isA;

  public AorB(A instance) {
    _instance = instance;
    _isA = true;
  }

  public AorB(B instance) {
    _instance = instance;
    _isA = false;
  }

  public A asA() {
    assert _isA;
    return (A) _instance;
  }

  public B asB() {
    assert !_isA;
    return (B) _instance;
  }

  public boolean isA() {
    return _isA;
  }
}

如果仍希望移植某种程度的编译时类型安全性,可以执行以下操作:

public class AorB<A, B> {
  private final Object _instance;
  private final boolean _isA;

  public AorB(A instance) {
    _instance = instance;
    _isA = true;
  }

  public AorB(B instance) {
    _instance = instance;
    _isA = false;
  }

  public A asA() {
    assert _isA;
    return (A) _instance;
  }

  public B asB() {
    assert !_isA;
    return (B) _instance;
  }

  public boolean isA() {
    return _isA;
  }
}

构造函数中的参数应具有特殊含义。使用varargs是可以接受的,但它认为这些是特殊情况。你们的问题可以用另一种方式解决

public class TreeNode {

   public TreeNode() {
     //crate the object
   }

   public TreeNode addNode(String node, String... nodes) {
    //Do something with string node
    return this;
   }

   public TreeNode addNode(TreeNode node, TreeNode... nodes) {
   //Do something with TreeNode
    return this;
   }
}
所以你可以像这样举个例子

TreeNode node = new TreeNode().addNode("One","Two").addNode(node3,node4);

其中,节点3和节点4为树节点

构造函数中的参数应具有特殊含义。使用varargs是可以接受的,但它认为这些是特殊情况。你们的问题可以用另一种方式解决

public class TreeNode {

   public TreeNode() {
     //crate the object
   }

   public TreeNode addNode(String node, String... nodes) {
    //Do something with string node
    return this;
   }

   public TreeNode addNode(TreeNode node, TreeNode... nodes) {
   //Do something with TreeNode
    return this;
   }
}
所以你可以像这样举个例子

TreeNode node = new TreeNode().addNode("One","Two").addNode(node3,node4);

其中,节点3和节点4为树节点

使用泛型真的一点也不明智。TreeNode实例可以有没有上限的异构子级?编译时检查和泛型不能帮助您。TreeNodeget应该返回什么?TreeNode.get返回一个字符串,它是子节点的一个contating。这真的不是泛型的合理使用。TreeNode实例可以有没有上限的异构子级?编译时检查和泛型不能帮助您。TreeNodeget应该返回什么?TreeNode.get返回一个字符串,它是子节点的contating。TreeNode c=new TreeNode54;不应该被编译这是如何在编译时禁止54的;不应编译此操作如何禁止54在编译时执行?我可以将字符串隐式转换为StringNode吗?我可以将字符串隐式转换为StringNode吗?