我正在为人类开设课程,我对此有一两个问题。
/**
* 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() );
}
...
Person类是公开还是默认访问权限更好?一个人,一个私人人应该公开并不是有点奇怪,即使这里的含义不完全相同。
当我的IDE警告inventory = Collections.synchronizedCollection( new LinkedList() );
时,我该如何处理arning?它警告我的类型安全。
谢谢
答案 0 :(得分:4)
如果Person只在一个包中使用,您可以将其设为默认值。否则public
有意义。
尝试提供通用类型。
List<Person> people = Collections.synchronizedList(new LinkedList<Person>());
答案 1 :(得分:2)
对于警告,您需要指定LinkedList
的元素类型:
inventory = Collections.synchronizedCollection( new LinkedList<Thing>());
至于private
的{{1}} / public
:如果类 Person
标记为Person
,则表示其他代码(其包装的其他部分)可以根据需要参考/使用它。当您将public
类型的成员变量声明为Person
时,这意味着其他代码无法直接访问成员变量。两者互不影响