用于创建用户配置文件的简单Java构造函数和setter

时间:2016-11-26 00:09:00

标签: 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 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();
    }

}

其中一个方法采用此名称和这些参数,并且是两个属性的setter," message"和"朋友"

sendMessage(MyBookAccount to, String message) 

如何制作一个操纵多个属性的setter?

1 个答案:

答案 0 :(得分:1)

我不会让一个二传手操纵多个属性。这可能会使您的代码混乱,并且将来更难以维护。制作一个能够做到这一点的方法会更好。为每个字段创建setter,然后有一个方法以您想要的方式操作它们。