我的任务是制作这种静态方法: " createPerson - 此方法根据名字,姓氏,姓名和年龄创建新人。然后它返回该人的新实例。"
但是因为我们刚刚学习了静态方法,所以我很困惑。
这是驱动程序类:
public class FriendlyPeople {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Let's meet some friendly people");
Person[] people =
{
new Person("Tom Jones", 29),
new Person("Bob Jones", 40),
new Person("Alf Pererdur", 10),
new Person("Ripley Carver",20),
new Person("Britton Raven",47),
new Person("Joyce Elihu",93),
new Person("Kevin Jody",63),
new Person("Ben Jayce", 32),
new Person("Emerson Ezra",27),
new Person("Yorick Fearghal",44),
new Person("Kim Yori",23),
new Person("Sheldon Ambrose", 36),
new Person("Leonard Damion",33),
new Person("Agam Saburou", 53),
new Person ("Webster Chaz", 46),
new Person("Carol Dudel", 31)
};
Person p1 = new Person("Dacre Casey", 29);
System.out.println("Testing the initial person");
System.out.println(p1.toString());
System.out.println();
System.out.println("Adding one friend");
p1.addFriend(Person.createPerson("Wetzel Edmund", 62));
p1.printFriends();
System.out.println();
System.out.println("Adding a new friend using the overloaded operator");
p1.addFriend("Judith Katheryne", 32);
p1.printFriends();
System.out.println();
System.out.println("Adding a group of friends that exceeds the number of friends.");
p1.addFriends(people);
p1.printFriends();
System.out.println();
System.out.println("Removing a friend "+people[0].toString());
p1.removeFriend(people[0]);
p1.printFriends();
System.out.println();
}
到目前为止,这是我的代码:
public class Person {
private String name;
private int age;
private Person[] friends;
//Constructor for person
public Person(String name, int age)
{
this.name = name;
this.age = age;
}
//Constructor array for person
public Person()
{
this.friends = new Person[10];
}
//Accessors
public String getName() {return name;}
public int getAge() {return age;}
//Mutators
public void setName (String name)
{
this.name = name;
}
public void setAge (int age)
{
if (age < 0)
{
System.out.println("That is an invalid age");
return;
}
this.age = age;
}
//Methods
public static String createPerson (String s)
{
}
public String toString() { return name + " " + age; }
public boolean equals (Person person) {
return this.name.equalsIgnoreCase(person.getName()) && this.age == person.getAge();
}
//Add friend method
public void addFriend(Person person)
{
for (int column = 0; column < friends.length; column++)
{
if (friends[0] != null || (friends[column] == null && friends[column-1] != null))
{
friends[column] = person;
}
if (friends[9] == person) {
System.out.println("Friends list is full!");
}
}
}
以下是我想弄清楚的方法,这可能很简单:
public static String createPerson (String s)
{
}
答案 0 :(得分:2)
此时请注意,静态方法与实例化类无关。要调用静态方法:
Person.createPerson("first", "last", 20);
请注意,对方法的调用使用了类名,而不是引用Person类型对象的变量。要创建方法,请像使用静态一样使用static。从静态方法,您无法访问Person的非静态类字段。像在其他方法中一样,将逻辑放在静态方法中。 没有判断任何其他代码,方向告诉你这样做:
public static Person createPerson (String first, String last, int age)
{
return new Person(first, last, age);
}
答案 1 :(得分:0)
首先,如果您使用的是Factory(静态初始化程序),则应将该实例设为私有。
其次,您应该在两个构造函数上实例化好友列表,或者您应该使用this();
来获取其他构造函数(它被称为重载),但我建议您使用ArrayList。如果您不知道如何使用它,请使用跟踪您有多少朋友将其用作指针的变量。
我强烈建议您学习收集框架。数组列表可以非常轻松地完成您想要的工作。
第三,如果你要添加已经存在的朋友,你应该从你得到的列表中获取它。如果它不存在,您应该将其添加到列表中。我建议使用HashMap,但是ArrayList就足够了。这是对代码的重新调制:
public class FriendlyPeople {
public static void main( String[] args ) {
// TODO Auto-generated method stub
System.out.println("Let's meet some friendly people");
ArrayList<Person> people = new ArrayList<Person>();
Person[] peopleList = {
Person.createPerson( "Tom Jones", 29),
Person.createPerson( "Bob Jones", 40),
Person.createPerson( "Alf Pererdur", 10),
Person.createPerson( "Ripley Carver",20),
Person.createPerson( "Britton Raven",47),
Person.createPerson( "Joyce Elihu",93),
Person.createPerson( "Kevin Jody",63),
Person.createPerson( "Ben Jayce", 32),
Person.createPerson( "Emerson Ezra",27),
Person.createPerson( "Yorick Fearghal",44),
Person.createPerson( "Kim Yori",23),
Person.createPerson( "Sheldon Ambrose", 36),
Person.createPerson( "Leonard Damion",33),
Person.createPerson( "Agam Saburou", 53),
Person.createPerson( "Webster Chaz", 46),
Person.createPerson( "Carol Dudel", 31)
};
people.ensureCapacity( peopleList.length );
for ( int i = 0 ; i < peopleList.length ; i++ ) {
people.add( peopleList[i] );
}
Person p1 = Person.createPerson( "Dacre Casey", 29);
people.add( p1 );
System.out.println("Testing the initial person");
System.out.println(p1.toString());
System.out.println();
System.out.println("Adding one friend");
Person newPerson1 = Person.createPerson("Wetzel Edmund", 62);
people.add( newPerson1 );
p1.addFriend( newPerson1 );
p1.printFriends();
System.out.println();
System.out.println("Adding a new friend using the overloaded operator");
Person newPerson2 = Person.createPerson( "Judith Katheryne", 32);
people.add( newPerson2 );
p1.addFriend( newPerson2 );
p1.printFriends();
System.out.println();
System.out.println("Adding a group of friends that exceeds the number of friends.");
p1.addFriends(people);
p1.printFriends();
System.out.println();
Person friendToRemove = people.get( 0 );
//You don't need to explicit .toString
System.out.println("Removing a friend "+friendToRemove);
p1.removeFriend( friendToRemove );
p1.printFriends();
System.out.println();
}
}
至于Person
类:
public class Person {
private String name;
private int age;
private ArrayList<Person> friends;
//Constructor for person
private Person(String name, int age) {
this();
this.name = name;
this.age = age;
}
//Constructor array for person
private Person() {
this.friends = new ArrayList<Person>(10);
}
//Accessors
public String getName() {return name;}
public int getAge() {return age;}
//Mutators
public void setName (String name) {
this.name = name;
}
public void setAge (int age) {
if (age < 0) {
System.out.println("That is an invalid age");
return;
}
this.age = age;
}
//Methods
@Override
public String toString() { return name + " " + age; }
public boolean equals (Person person) {
return this.name.equalsIgnoreCase(person.getName()) && this.age == person.getAge();
}
//Add friend method
public void addFriend(Person person) {
friends.add( person );
}
public void addFriends( ArrayList<Person> people ) {
friends.addAll( people );
}
public void addFriends( Person[] people ) {
for ( int i = 0 ; i < people.length; i++ ) {
Person friend = people[i];
friends.add( friend );
}
}
//To remove over an ArrayList I advice using an Iterator, but for now
//this is enought
public void removeFriend(Person friend ) {
for ( int i = 0 ; i < friends.size(); i++ ) {
Person person = friends.get( i );
if( person.equals(friend) ){
friends.remove( i );
}
}
}
public void printFriends() {
System.out.println( "Friends: " );
for ( int i = 0 ; i < friends.size() ; i++ ) {
System.out.println( friends.get( i ) );
}
}
//I advise to leave static methods to the end. It leaves the code cleaner.
public static Person createPerson ( String name, int age ) {
return new Person( name, age );
}
public static Person createPerson () {
return new Person();
}
}
我再次建议您学习Collection FrameWork。然而,没有它可以做到这一点。你可以用一个简单的数组做你想做的事情,但是你应该使用一个变量来跟踪朋友的数量(作为一个指针。)。当指针到达数组长度时,只需执行以下操作:
private void increaseListLenght() {
Person[] newFriendList = new Person[friends.lenght*1.75];
for ( int i = 0 ; i < friends.lenght ; i++ ) {
newFriendList[i] = friends[i];
}
friends = newFriendList;
}
但如果您选择这样做,则无法获得列表final
。
我希望我有所帮助。
祝你有个愉快的一天。 :)