class Mail {
// Method to accept and print mail ID
public void printMail(String mail) {
System.out.println("Mail ID: " + mail);
}
// Overloaded method to accept and print mail ID
public void printMail(int mail) {
System.out.println("Mail ID: " + mail);
}
}
public class Main {
public static void main(String[] args) {
Mail mail = new Mail();
// Accepting mail ID as string input
System.out.println("Enter mail ID: ");
Scanner sc = new Scanner(System.in);
String mailId = sc.nextLine();
mail.printMail(mailId);
// Accepting mail ID as integer input
System.out.println("Enter mail ID: ");
int mailIdInt = sc.nextInt();
mail.printMail(mailIdInt);
}
1条答案
按热度按时间bvhaajcl1#
}
}
在此示例中,printMail方法被重载以接受字符串和整数作为输入。main方法提示用户输入其邮件ID,然后根据输入类型调用相应版本的printMail方法。然后,邮件ID被输出到控制台。请注意,此示例使用Scanner对象在运行时读取用户的输入。