Java 如何在同一类中调用方法
我正在为一个电子邮件系统编写代码,我有一个小问题,我不知道如何调用一个方法 在main方法中调用writeAllofile方法的正确方法是什么Java 如何在同一类中调用方法,java,methods,Java,Methods,我正在为一个电子邮件系统编写代码,我有一个小问题,我不知道如何调用一个方法 在main方法中调用writeAllofile方法的正确方法是什么 public class MailboxSystemSol{ public static void main (String args[]) throws IOException{ //code is too long to post here } public static void writeAllToFile(U
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();
}
}
下面是消息类:
/**
*由Broomhead0于2014年4月11日创建。
*/
你的问题回避了一个问题——你试过什么?这对你不起作用吗?您从哪里获取作为参数传递给方法的UserList、User和Message值?很抱歉,我应该指定,UserList、User和Message是其他类
WriteAllofile(您的\UL\U参数、您的\U\U参数、您的\U ME\U参数)代码>它是一个静态方法,因此不需要实例化对象。您不需要包含类名,因为您是从定义它的同一个类中调用它的。我认为你最初提出的问题具有误导性。正如@WillNewton向您展示的那样,您已经知道如何在静态方法中调用静态方法,对吗?您的问题在于上面代码的细节,以及如何使用正确且有意义的参数调用此方法。如果是这样,您仍然需要弄清楚如何在主方法中创建User、UserList和Message对象,然后将它们传递到静态方法中。如果你仍然被卡住,你应该重新考虑你的问题,包括改变误导性的部分。
/**
* 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);
}
}
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;
}
}
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
}
}