0

私はPersonsのクラスを開発していますが、それについて1つか2つの質問があります。

/**
 * 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() );ですか?それは私の型安全性について警告します。

ありがとうございました

4

2 に答える 2

4
  1. Personが1つのパッケージでのみ使用される場合は、デフォルトにすることができます。そうでなければpublic意味があります。

  2. ジェネリック型を指定してみてください。

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

ところで:ほとんどのIDEには、これらの問題のいずれかに対する自動/クイック修正があります。

于 2012-06-11T12:23:01.603 に答える
2

警告については、次の要素のタイプを指定する必要がありますLinkedList

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

private/ for :について:クラスがマークされている場合publicそれは他のコード(そのパッケージの外)が必要に応じてそれを参照/使用できることを意味します。タイプのメンバー変数をとして宣言すると、他のコードがメンバー変数に直接アクセスできないことを意味します。2つはお互いに影響を与えませんPerson PersonpublicPersonprivate

于 2012-06-11T12:26:53.223 に答える