小编ako*_*ako的帖子

使构造函数抛出异常是一种好习惯吗?

使构造函数抛出异常是一个好习惯吗?例如,我有一个类Person,我有age它的唯一属性.现在我提供课程为

class Person{
  int age;
  Person(int age) throws Exception{
   if (age<0)
       throw new Exception("invalid age");
   this.age = age;
  }

  public void setAge(int age) throws Exception{
  if (age<0)
       throw new Exception("invalid age");
   this.age = age;
  }
}
Run Code Online (Sandbox Code Playgroud)

java constructor exception

155
推荐指数
8
解决办法
13万
查看次数

在不使用recurrsion的情况下遍历n-ary树

如何在n不使用递归的情况下遍历一棵树?

递归方式:

traverse(Node node)
{
    if(node == null)
        return;

    for(Node child : node.getChilds()) {
        traverse(child);
    }
}
Run Code Online (Sandbox Code Playgroud)

algorithm tree-traversal

19
推荐指数
3
解决办法
3万
查看次数

在开关内使用枚举

我们可以在交换机内使用枚举吗?

public enum Color {
  RED,BLUE,YELLOW


}


 public class Use {
  Color c = Color.BLUE;

  public void test(){
      switch(c){
      case Color.BLUE:

      }
  }
}
Run Code Online (Sandbox Code Playgroud)

我在这收到一些错误.

The enum constant Color.BLUE reference cannot be qualified in a case label  Use.java        line 7  Java Problem
Run Code Online (Sandbox Code Playgroud)

java enums

4
推荐指数
1
解决办法
4329
查看次数

0
推荐指数
1
解决办法
1071
查看次数