Java 一个开关中有多个对象;可能吗?

Java 一个开关中有多个对象;可能吗?,java,if-statement,constructor,switch-statement,Java,If Statement,Constructor,Switch Statement,我只是一个Java初学者,我正在尝试创建一个程序,可以显示多个对象的名称、年龄和出生年份。我是否可以使用switch语句来计算这两个对象的出生年份?请把你的答案和我的一样简单。谢谢这是我的密码 package PackageOne; public class NewClass { static String name; static int age = 0; static int currentYear = 2015; static int birth; public stati

我只是一个Java初学者,我正在尝试创建一个程序,可以显示多个对象的名称、年龄和出生年份。我是否可以使用switch语句来计算这两个对象的出生年份?请把你的答案和我的一样简单。谢谢这是我的密码

package PackageOne;
public class NewClass {

 static String name;
 static int age = 0;
  static int currentYear = 2015;
 static int birth;

 public static void main (String []args){

      NewClass person1 = new NewClass("Ichiro ", 15);
      NewClass person2 = new NewClass("Marie ", 21);
      person1.getInfo();
      person2.getInfo();
     birth = currentYear - age;

switch(birth){
    case 1: birth = 2000;
        break;
    case 2: birth = 2001; break;
    case 3: birth = 2002;break;
    default: System.out.println(birth);break;

}
 }
public NewClass(String x, int y){
   this.name = x;
   this.age = y;
}
public void getInfo(){
    System.out.println(name + age);
}
 }

你把这一切都搞错了。。。您的变量、类和包都应该是可读的和具有代表性的。您还需要使用封装和适当的方法。此外,还存储了一个易于计算的字段。我真的不确定switch语句想要实现什么

此外,在类中使用控制台将其绑定到输出,而不是返回字符串并从main打印它

package com.denmark16;


public class Person {

      // Fields are private, and thus encapsulated
      private static String _name;
      private static int _age;


      // I'd put main in another class to make it tidier, but it's ok here
      public static void main (String []args){

          //By using a list, you can add as many people as you wish
          ArrayList<Person> people = new ArrayList<Person>();

          people.add(new Person("Ichiro ", 15));
          people.add(new Person("Marie ", 21));


      for (Person person in people) {
               System.Console.writeLine(getDetails(new DateTime().year()));
          }

     }


    // Not sure why you'd store age, I'm guessing it's an exercise, but really it should be Date of Birth
    public Person(String name, DateTime age){
       _name = name;
       _age = age;
    }


    // This should probably be a toString with hashCode
    public string getAllDetails(int year){
        return _name + " " + _age + " " + getYearOfBirth(new DateTime().getYear());
    }


    // This is flaky as really, we should store and calculate against the Date of Birth
    public int getYearOfBirth(int year) {
        return year - _age;     

    }

}
package com.denmark16;
公共阶层人士{
//字段是私有的,因此被封装
私有静态字符串_name;
私有静态整数;
//我想把main放在另一节课上,让它更整洁,但这里没问题
公共静态void main(字符串[]args){
//通过使用列表,您可以添加任意多的人
ArrayList people=新建ArrayList();
添加(新人物(“Ichiro”,15));
添加(新人物(“玛丽”,21));
for(人与人之间){
System.Console.writeLine(getDetails(newdatetime().year());
}
}
//我不知道你为什么要储存年龄,我猜这是一种锻炼,但实际上应该是出生日期
公众人物(字符串名称、日期时间年龄){
_名称=名称;
_年龄=年龄;
}
//这可能是一个带有hashCode的toString
公共字符串getAllDetails(整年){
返回_name++++u age+++getYearOfBirth(new DateTime().getYear());
}
//这真的很脆弱,我们应该根据出生日期进行存储和计算
公共int getYearOfBirth(int year){
返回年份-_年龄;
}
}