创建用户配置文件的简单Java构造函数和setter
我有一个关于我正在做的作业的问题。我将给出主要代码和有问题的方法,以便清楚地了解我的问题创建用户配置文件的简单Java构造函数和setter,java,oop,methods,constructor,setter,Java,Oop,Methods,Constructor,Setter,我有一个关于我正在做的作业的问题。我将给出主要代码和有问题的方法,以便清楚地了解我的问题 import java.util.Scanner; public class MyBookDriver { private static final Scanner KBD = new Scanner(System.in); /** * @param args the command line arguments */ public static void
import java.util.Scanner;
public class MyBookDriver {
private static final Scanner KBD = new Scanner(System.in);
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Constructors
MyBookAccount bbSheldon = new MyBookAccount("Sheldon", true);
MyBookAccount bbPenny = new MyBookAccount("Penny", false);
MyBookAccount bbAmy = new MyBookAccount("Amy", "Montreal", true);
MyBookAccount bbLeonard = new MyBookAccount("Leonard");
System.out.println("\n" + MyBookAccount.getNumAccounts()
+ " MyBook accounts have been created.");
// Mybook ID
System.out.println("\nMyBook Accounts:");
System.out.println(" Sheldon's ID: " + bbSheldon.ID);
System.out.println(" Penny's ID: " + bbPenny.ID);
System.out.println(" Amy's ID: " + bbAmy.ID);
System.out.println(" Leonard's ID: " + bbLeonard.ID);
pause();
// logged in
System.out.println("\nMyBook Accounts:");
System.out.println(" Sheldon is "
+ (bbSheldon.isLoggedIn() ? "" : "not ") + "logged in");
System.out.println(" Penny is "
+ (bbPenny.isLoggedIn() ? "" : "not ") + "logged in");
System.out.println(" Amy is "
+ (bbAmy.isLoggedIn() ? "" : "not ") + "logged in");
System.out.println(" Leonard is "
+ (bbLeonard.isLoggedIn() ? "" : "not ") + "logged in");
pause();
//post a wall message
System.out.println("\nPosting wall update:");
bbSheldon.setWallPost("I like flags!");
bbPenny.setWallPost("Looking for a job.");
bbLeonard.setWallPost("I'm just hoping I can date a girl "
+ "from next door.");
System.out.println(" Sheldon's: " + bbSheldon.getWallPost() + "\n"
+ " Penny's: " + bbPenny.getWallPost() + "\n"
+ " Amy's: " + bbAmy.getWallPost() + "\n"
+ " Leonard's: " + bbLeonard.getWallPost() + "\n");
pause();
//Sending messages
System.out.println("\nSending messages:");
bbLeonard.sendMessage(bbPenny, "Will you go out with me tonight?");
bbAmy.sendMessage(bbSheldon, "Neuroscience is a real science.");
bbPenny.sendMessage(bbAmy, "What a nice picture.");
checkMessages(bbSheldon);
checkMessages(bbPenny);
checkMessages(bbAmy);
checkMessages(bbLeonard);
pause();
//toString
System.out.println("\nDisplaying info:");
System.out.println(bbSheldon);
System.out.println(bbPenny);
System.out.println(bbAmy);
System.out.println(bbLeonard);
pause();
}
private static void checkMessages(MyBookAccount user) {
MyBookAccount aFriend;
aFriend = user.getFriend();
if (aFriend != null) {
System.out.println(" " + user.getName() + "'s message from "
+ aFriend.getName()
+ " is " + user.getMessage());
} else {
System.out.println(" " + user.getName() + " has no messages");
}
}
private static void pause() {
System.out.print("\n...press enter...");
KBD.nextLine();
}
}
其中一个方法使用这个名称和这些参数,是两个属性“message”和“friend”的setter
如何创建一个可以处理多个属性的setter?我不会创建一个可以处理多个属性的setter。这会使您的代码变得混乱,并且在将来更难维护。最好是制作一种能够做到这一点的方法。为每个字段创建setter,然后使用一种方法以您想要的方式操作它们。您是否只需要
this.friend=to;this.message=消息代码>?为什么要“创建一个处理多个属性的setter”?制作一个方法来接受2条数据,这些数据本身调用相应的setter(或直接更新字段)。我想他不知道“setter”是什么意思。因为他所说的假设的sendMessage
方法不会像setter那样工作。我认为在您向我们展示MyBookAccount类的代码之前,我们无法回答这个问题。
sendMessage(MyBookAccount to, String message)