【发布时间】:2016-05-31 09:04:30
【问题描述】:
我对编程很陌生,这是我的第一门课程。我很难理解整个概念,希望我能得到一些关于如何继续的提示。
基本上我需要编写一个简单的地址簿程序,让用户输入 2 个(或更多?)用户的信息。我的课程援助给了我以下建议:
在我的应用程序(演示或测试)类的 main 方法中 - 创建两个或多个 AddressBook 类的实例/对象 - 使用实例(对象)调用类AddressBook的实例方法 - 使用 2 个实例/对象来比较它们的名称
我目前有以下:
AddressBook.java
import java.util.Scanner;
public class AddressBook {
Scanner sc = new Scanner(System.in);
//Declare method variables and initial value (empty)
String firstName = "";
String middleName = "";
String lastName = "";
String homeAddress = "";
String businessPhone = "";
String homePhone = "";
String cellPhone = "";
String skypeId = "";
String facebookId = "";
String personalWebSite = "";
public AddressBook(String firstName, String middleName,
String lastName, String homeAddress, String businessPhone,
String homePhone, String cellPhone, String skypeId,
String facebookId, String personalWebSite) {
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
this.homeAddress = homeAddress;
this.businessPhone = businessPhone;
this.homePhone = homePhone;
this.cellPhone= cellPhone;
this.skypeId = skypeId;
this.facebookId = facebookId;
this.personalWebSite = personalWebSite;
}
//Setters
void setFirstName(String firstName) {
this.firstName = firstName;
}
void setMiddleName(String middleName) {
this.middleName = middleName;
}
void setLastName(String lastName){
this.lastName = lastName;
}
void setHomeAddress(String homeAddress) {
this.homeAddress = homeAddress;
}
void setBusinessPhone(String businessPhone) {
this.businessPhone = businessPhone;
}
void setHomePhone(String homePhone) {
this.homePhone = homePhone;
}
void setCellPhone(String cellPhone) {
this.cellPhone = cellPhone;
}
void setSkypeId(String skypeId) {
this.skypeId = skypeId;
}
void setFacebookId(String facebookId) {
this.facebookId = facebookId;
}
void setPersonalWebSite(String personalWebSite) {
this.personalWebSite = personalWebSite;
}
//Getters
public String getFirstName() {
System.out.print("First name: ");
String firstName = sc.nextLine();
return firstName;
}
public String getMiddleName() {
System.out.print("Middle name: ");
String middleName = sc.nextLine();
return middleName;
}
public String getLastName() {
System.out.print("Last name: ");
String lastName = sc.nextLine();
return lastName;
}
public String getHomeAddress() {
System.out.print("Home address: ");
String homeAddress = sc.nextLine();
return homeAddress;
}
public String getBusinessPhone() {
System.out.print("Business phone number: ");
String businessPhone = sc.nextLine();
return businessPhone;
}
public String getHomePhone() {
System.out.print("Home phone number: ");
String homePhone = sc.nextLine();
return homePhone;
}
public String getCellPhone() {
System.out.print("Cell phone number: ");
String cellPhone = sc.nextLine();
return cellPhone;
}
public String getSkypeId() {
System.out.print("Skype ID: ");
String skypeId = sc.nextLine();
return skypeId;
}
public String getFacebookId() {
System.out.print("Facebook ID: ");
String facebookId = sc.nextLine();
return facebookId;
}
public String getPersonalWebSite() {
System.out.print("Personal Website: ");
String personalWebSite = sc.nextLine();
return personalWebSite;
}
public String compareNames() {
String comp1 = name1.getFirstName;
String comp2 = name2.getFirstName;
if (comp1.equalsIgnoreCase(comp2)) {
System.out.println("The names match!");
} else {
System.out.println("The names don't match!");
}
}
} // end of class AddressBook
AddressBookDemo.java
public class AddressBookDemo {
public static void main(String[] args) {
// Create two AddreeBook objects
AddressBook name1 = new AddressBook();
AddressBook name2 = new AddressBook();
// Invoke methods on those objects
System.out.println("Enter the first person's info");
name1.getFirstName();
//Testing input/output
System.out.println(name1.firstName);
// name1.getMiddleName();
// name1.getLastName();
// name1.getHomeAddress();
// name1.getBusinessPhone();
// name1.getHomePhone();
// name1.getCellPhone();
// name1.getSkypeId();
// name1.getFacebookId();
// name1.getPersonalWebSite();
System.out.println("Enter the second person's info");
name2.getFirstName();
//Testing input/output
System.out.println(name2.firstName);
// name2.getMiddleName();
// name2.getLastName();
// name2.getHomeAddress();
// name2.getBusinessPhone();
// name2.getHomePhone();
// name2.getCellPhone();
// name2.getSkypeId();
// name2.getFacebookId();
// name2.getPersonalWebSite();
} // end of Main
} // End of class AddressBookDemo
如您所见,在尝试初始化类 AddressBook() 的两个实例时,我遇到了构造函数错误。如果我尝试添加一个 'null' 的 arg,当我在调用方法后测试输出时,我只会得到一个返回 'null'。我还在 main 中创建了 compareName() 方法,但还没有调用它,因为我什至无法让第一部分工作。此外,我有点困惑我应该如何将 name1.firstName 和 name2.firstName 的值发送回 main (或者我应该简单地将值存储到另一个变量中并在每次 getFirstName() 时创建一个增量循环叫什么?)。
【问题讨论】:
-
构造函数用于构造对象。您似乎没有传递任何有意义的东西来构建地址簿。
-
Getter 不应该获得任何用户输入——他们应该只返回对象中已经存在的内容。您的主要方法应该使用扫描仪完成所有操作,然后调用设置器以适当地设置字段,或者更好的是,将参数传递给
AddressBook构造函数。 -
最后,
String middleName = sc.nextLine();not 在AddressBook对象中设置middleName字段;它创建了一个恰好具有相同名称的新变量。即使对于你想要做的事情,你也应该只写middleName = sc.nextLine()而不要写String。 -
我想我明白你们的意思了。我正在修改我的代码以解决 set/get 的不当使用。还要感谢您让我知道我只是在为每个字符串创建一个新实例;现在看起来很明显......叹息
标签: java class methods encapsulation instances