-3

我想叫有CEO = true的人的名字;在这种情况下是 Tristen。我该怎么办?我已经将公共字符串名称和公共布尔 CEO 转换为静态。

我想要达到的是它看起来像这样

system.out.printIn("你的 CEO 是 Tristen");

public class business {

public static String name;
public String lastname;
public String email;
public String department;
public static boolean CEO;
public boolean employee;
public boolean assistant;
public boolean head;
public boolean manager;

public static void main(String[]args){


    business tristen;
    business jan;
    business robert;
    business kevin;
    business ben;

    tristen = new business();
    tristen.name = "Tristen";
    tristen.lastname = "Korsuize";
    tristen.email = " ";
    tristen.department = "Magic";
    tristen.CEO = true;
    tristen.employee = false;
    tristen.assistant = false;
    tristen.head = false;
    tristen.manager = false;

    jan = new business();
    jan.name = "Jan";
    jan.lastname = "Lindenberg";
    jan.email = "Jan002_003@hotmail.com";
    jan.department = "Magic";
    jan.CEO = false;
    jan.employee = false;
    jan.assistant = false;
    jan.head = false;
    jan.manager = true;

    robert = new business();
    robert.name = "Robert";
    robert.lastname = "Irving";
    robert.email = " ";
    robert.department = "Magic";
    robert.CEO = false;
    robert.employee = false;
    robert.assistant = true;
    robert.head = false;
    robert.manager = false;


    if(business.CEO = true){
        System.out.println("Your CEO is"+ " "+ business.name);
    }

    }

}
4

2 回答 2

3

我会将人们放在一个集合中并对其进行迭代,例如

List<business> businesses = new ArrayList<business>();
// add people here...

foreach (business b : businesses) {
   if (b.isCEO()) {
     System.out.println(b);
   }
}

一些额外的评论:

  1. Java 命名约定建议您应该大写您的类名,例如Business
  2. 你的成员是公开的。我会将它们设为私有并通过构造函数进行实例化。这促进了封装——OO 编程的一个关键特性。通过方法公开它们(隐藏实现)
  3. 询问对象是否是 CEO - 不要将领域拉出来自己决定。见上面的例子
  4. 我会尽量不使用static. 您拥有的所有信息都绑定在您的对象中。
于 2013-02-22T12:34:16.897 回答
2

和字段不应该是静态的CEOname静态字段意味着该值被赋予business类的所有实例,而不仅仅是单个实例。例如;

tristen = new business();
tristen.CEO = true;

jan = new business();
jan.CEO = false;  //CEO is now false for all instances of business (tristen and jan)

您可能希望在创建它们时将所有实例存储在某种形式中Collection,然后您可以遍历集合并检查哪个是 CEO。

于 2013-02-22T12:39:58.043 回答