我試圖創建一個代表一個人姓名的類,該字段具有代表名字,中間名字和姓氏(也有幾種方法)的字段。創建一個包含字段的類java
當我嘗試運行我的代碼時,收到Illegal modifier for the local class Name; only abstract or final is permitted
錯誤,並找不到解決此問題的方法。請幫助我解決任何錯誤,以便我的代碼執行所需的(和評論的)任務。先謝謝你。
public class Name {
String first_name;
String middle_initial;
String last_name;
//constructor that accepts first name, middle initial, and last name as parameters and initializes the Name object's state with those values
public Name(String first_name, String middle_initial, String last_name) {
first_name = "John";
middle_initial = "Q.";
last_name = "Public";
}
//method to return the person's name in first name, middle initial, and last name order.
public String getNormalOrder() {
String name = first_name + " " + middle_initial + " " + last_name;
return name;
}
//method to return the person's name in reversed order: last name, first name, and middle name.
public String getReverseOrder() {
String name = last_name + ", " + first_name + " " + middle_initial;
return name;
}
//method to return the full name in first, middle, last order, in String form
public String toString() {
return first_name + " " + middle_initial + " " + last_name;
}
public static void main(String[] arg){
first_name = "John";
middle_initial = "Q.";
last_name = "Public";
getNormalOrder();
getReverseOrder();
toString();
}
}
爲什麼它是一個本地類,其餘的代碼在哪裏?旁註:您的構造函數不會將字段初始化爲傳遞的參數。 – Li357
我是新來的,我沒有學到任何其他方式(非本地方式)創建一個類。我如何讓我的構造函數初始化傳遞的參數? – anonym
發佈您的主要功能。 –