如何修复此警告?

时间:2012-06-11 12:19:52

标签: java model-view-controller model game-engine

我正在为人类开设课程,我对此有一两个问题。

/**
 * 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?它警告我的类型安全。

  3. 谢谢

2 个答案:

答案 0 :(得分:4)

  1. 如果Person只在一个包中使用,您可以将其设为默认值。否则public有意义。

  2. 尝试提供通用类型。

    List<Person> people = Collections.synchronizedList(new LinkedList<Person>());
    
  3. BTW:大多数IDE都会自动/快速修复这些问题。

答案 1 :(得分:2)

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

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

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