如何在同一个类中调用方法

时间:2014-04-12 00:24:48

标签: java methods

我正在编写电子邮件系统的代码,我有一个小问题,我无法弄清楚如何调用方法。

在main方法中调用writeAllToFile方法的正确方法是什么?

public class MailboxSystemSol{

    public static void main (String args[]) throws IOException{
    //code is too long to post here
    }

    public static void writeAllToFile(Userlist ul, User u, Message me){
    //code is too long to post here
    }

}

这是UserList类:

/**
* Created by Broomhead0 on 4/11/14.
*/
import java.util.ArrayList;
public class Userlist
{
    ArrayList<User> users;  //this is an arraylist that will store references to all users

    public Userlist()
    {

        users = new ArrayList<User>();
    }

    // find a user in the list based on the username
    public User findUser(String username)
    {
        // iterate through the array; only iterate according to how many users are currently in     the array
        for (int i = 0; i < users.size(); i++)
        {
            // access the particular user through users.(i), then get the name,
            // and call the equals method on that name to compare it with the input username
            if (users.get(i).userName.equals(username)){
                return users.get(i);
            }

        }
        // no user found
        return null;
    }

    // add a user to the list; only do so if the user does not yet exist
    public void addUser(User u)
    {
        if (findUser(u.userName) != null) //if there is a match,
            System.out.println("User already exists");
        else //if there is not match
        {
           users.add(u); //add the username
        }

    }
    //check if this is correct
    //accessors
    public User getUser(int i){
        return users.get(i);
    }

    public int getNumUsers(){
        return users.size();
    }


}

这是User类:

/**
 * Created by Broomhead0 on 4/11/14.
 */
public class User
{

    public String userName;  // the name of the user
    public Mailbox outbox; // reference to the mailbox of sent messages
    public Mailbox inbox;  // reference to the mailbox of received messages


    // create mailboxes according to the size given as input
    public User(String o, int boxsize) {
        userName = o;
        outbox = new Mailbox(boxsize);
        inbox = new Mailbox(boxsize);
    }

}

这是Message类:

/ **      *由Broomhead0于2014年11月4日创建。      * /

public class Message {

    // the properties of a message
    private String sender;
    private String receiver;
    private String subject;
    private String body;

    // all property values are known at creation of the message; so initialize
    public Message (String s, String r, String sub, String b)
    {
        sender = s;
        receiver = r;
        subject = sub;
        body = b;
    }

    // any nice format of printing the names and the values of the properties will do
    public void printMsg()
    {
        System.out.println("Sender: " + sender);
        System.out.println("Receiver: " + receiver);
        System.out.println("Subject: " + subject);
        System.out.println("Message: " + body);
    }

    // what follows are basic getter methods

    public String getSender()
    {
        return sender;
    }

    public String getReceiver()
    {
        return receiver;
    }

    public String getSubject()
    {
        return subject;
    }

    public String getBody()
    {
        return body;
    }

}

1 个答案:

答案 0 :(得分:0)

public class MailboxSystemSol{

    public static void main (String args[]) throws IOException{

        //define userlist, user and message variables..
        UserList userList = new UserList();
        User user = new User("Matthew", 1024);
        Message message = new Message("sender@gmail.com", "receiver@gmail.com", "Subject Content", "Body Content");

        //do something with declared variables...            

        writeAllToFile(userList, user, message);
    }

    public static void writeAllToFile(Userlist ul, User u, Message me){
        //code is too long to post here
    }

}
相关问题