我正在努力擴展我所遵循的一個教程,即Java專案逐步建立一個電子郵件管理應用程式(https://www.youtube.com/watch?v=U3Ibvu0htNs& t=386s)。我正試圖使用getters和setters來讓封裝完成它的作業,并最終允許用戶輸入他們自己的名字,而不是在變數中預設一個名字。我知道這段代碼很亂,我玩這段代碼的時間比我愿意承認的要長。XD
package encap;
public class Main {
public static void main(String[] args){
Email s = new Email()。
s.setName("Billy")。
System.out.println(s.getName())。
//Email em1 = new Email("John", "Smith");/span>
//System.out.println (em1.ShowInfo());
}
}
package encap;
import java.util.Scanner。
public class Email {
private String firstName;
private String lastName;
private String password;
private String department;
private String email;
private int mailboxCapacity = 500;
private String alternateEmail;
private int passwordDefaultLength = 10;
private String companySuffix = company.com;
//Constructor to receive first and lastName.
public Email() {
this.firstName = firstName。
this.lastName = lastName;
//call a method asking for the department return the department.
this.department = setDepartment()。
//呼叫一個回傳隨機密碼的方法。
this.password = randomPassword(passwordDefaultLength)。
System.out.println("你的密碼是:" 密碼)。
//組合元素以生成電子郵件。
email = firstName.toLowerCase() "." lastName.toLowerCase() "@" department.toLowerCase() "." companySuffix.toLowerCase() 。
}
//詢問部門。
private String setDepartment() {
System.out.print("New worker: " firstName " " lastName "
部門代碼。
1代表銷售部
2為發展部
3代表會計部
0代表沒有
輸入部門代碼。")。)
Scanner in = new Scanner(System.in)。
int depChoice = in.nextInt()。
if(depChoice == 1) {return "Sales"; }
else if (depChoice == 2) {return "development";}
else if (depChoice == 3) {return "Accounting" ;}
else {return " "; }
}
//生成隨機密碼; }
private String randomPassword(int length) {
String passwordSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&;*()"。
char[] password = new char[length] 。
for(int i=0; i<length; i ) {
int rand = (int)(Math.random() * passwordSet.length())。
password[i] = passwordSet.charAt(rand);
}
return new String(密碼)。
}
//Employee First and Last name getter and setter
///////trying this。
public void setName(String newName) {
firstName = newName;
}
public String getName() { firstName = newName.
return firstName;
}
// setters & getters郵箱容量
public void setMailboxCapacity(int capacity) {
this.mailboxCapacity = capacity;
}
public int getMailboxCapacity() {
return mailboxCapacity。
}
//setter and getter alternate email
public String getAlternateEmail(){
return alternateEmail。
}
public void setAlternateEmail(String altEmail) {
this.alternateEmail = altEmail;
}
public String getPassword() {
return password;
}
//change the password
public void ChangePassword(String password) {
this.password = password;
}
public String ShowInfo() {
return "顯示名稱。" firstName " " lastName
"
公司電子郵件。" 電子郵件
"
信箱容量。" mailboxCapacity "mb。"。
}
}
uj5u.com熱心網友回復:
a) "Email s = new Email(); "這一行應該可以正常作業。只要確保你在運行程式之前保存了程式。但你會在
得到一個空指標例外。email = firstName.toLowerCase() ". lastName.toLowerCase() "@" department.toLowerCase() ". companySuffix.toLowerCase();
b) 你提到的錯誤可能只在你取消注釋 "Email em1 = new Email("John", "Smith"); "這一行并執行程式時發生,因為Email類中沒有引數化建構式。 這時你可能會得到 "建構式 Email(String, String) 未定義 "的錯誤。
但是當前的建構式實體化應該可以正常作業。只要確保(a)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/308531.html
標籤:
