2012-06-11 251 views
0

我正在为人员开发一个班级,我对此有一个或两个问题。如何解决此警告?

/** 
* ADT for persons which is completed which subclasses 
* to creating actors with specific properties 
*/ 
public class Person { 

    /** 
    * Name of the Person. 
    */ 
    public String name; 

    /** 
    * The World which this Person is associated with. 
    */ 
    public World world; 

    /** 
    * Tells where this Person is at the moment. 
    */ 
    public Place where; 

    /** 
    * The inventory, contains Things. 
    */ 
    public Collection<Thing> inventory; 

    /** 
    * Shows how the Person looks like 
    */ 
    public Image appearance; 

    /** 
    * Create Person named `name' in world `world'. 
    * 
    * @param world The world where the Person is created. 
    * @param name Name of the Person. 
    * @param app An image used to display the Person. 
    */ 
    public Person(World world, String name, Image app) { 
    this.world = world; 
    this.name = name; 
    this.appearance = app; 

    where = world.defaultPlace(); 
    where.enter(this); 

    inventory = Collections.synchronizedCollection(new LinkedList()); 
    } 

...

  1. 如果Person类是公共的,或者可以默认访问是更好吗?一个人,私人人应该是公众是不是有点奇怪,即使这里的含义不完全相同。

  2. 当我的IDE对inventory = Collections.synchronizedCollection(new LinkedList());发出警告时,应如何处理该arning?它警告我的类型安全。

谢谢

回答

4
  1. 如果人只在一个封装中使用,你可以把它违约。否则public是有道理的。

  2. 尝试给出泛型类型。

    List<Person> people = Collections.synchronizedList(new LinkedList<Person>()); 
    

BTW:大多数IDE将对这些问题之一自动/速战速决。

2

对于警告,需要指定元素的类型为LinkedList

inventory = Collections.synchronizedCollection(new LinkedList<Thing>()); 

至于private/publicPerson:如果Person被标记public,这意味着其他代码(其包装的另一侧)可以根据需要参考/使用它。当您将类型Person成员变量声明为private时,表示其他代码无法直接访问成员变量。两者互不影响