我有 3 個類,人類、日期和十二生肖。在 Date 我有兩種 int 型別,月和日。我有普通的建構式和吸氣劑。在人類中,我有一個字串名稱和一個來自日期型別的生日。
我的上課日期:
public class Date {
private int month;
private int day;
public Date(int month, int day) {
this.month = month;
this.day = day;
}
public int getMonth() { return month;}
public int getDay() {return day;}
我的班級人類
public class Human {
private String name;
private Date birthday;
public Human(String name, Date birthday) {
this.name = name;
this.birthday = birthday;
}
public String getName() { return name;}
public BirthDate getBirthday() { return birthday;}
在我的班級 Zodiac 中,我有一個 Main,我在其中創建了一些物件。然后我有一個方法 zodiacToHuman 如果我給一個人類他的星座。但這種方法根本不起作用。該方法有一個 List 作為引數并回傳一個 Map。
我在十二生肖課上的方法:
public static Map<Human, String> zodiacToHuman(List<Human> humanlist){
Map<Human, String> personSign = new HashMap<>();
Human human;
String sign = "";
int day = Date.getDay();
int month = Date.getMonth();
if (month == 1) {
if (day < 20)
sign = "capricornus";
humanSign.put(human, sign);
else
sign = "aquarius";
humanSign.put(human, sign);
}//and so on
}
這是我得到的錯誤:
不能從靜態背景關系中參考非靜態方法“getDay()”
不能從靜態背景關系參考非靜態方法“getMonth()”
變數 Human 可能尚未初始化
有人能幫我嗎?
uj5u.com熱心網友回復:
你不能做
int day = Date.getDay()
首先創建一個 Date 類的物件,并用它來獲取日期和月份
Data date = new Date()
int day = date.getDay()
int month = date.getMonth()
此外,您還沒有初始化 Human 類物件。你可以寫
Human human = new Human(some_day, some_month)
uj5u.com熱心網友回復:
據我了解,humanList 包含人類物件的條目。您應該嘗試迭代串列,就像這樣
public static Map<Human, String> zodiacToHuman(List<Human> humanlist){
Map<Human, String> personSign = new HashMap<>();
for (Human human : humanList){
String sign = "";
int day = human.getBirthday().getDay();
int month = human.getBirthday().getMonth();
if (month == 1) {
if (day < 20){
sign = "capricornus";
}else{
sign = "aquarius";
}
}//and so on
humanSign.put(human, sign);
}
}
uj5u.com熱心網友回復:
讓我們將 的名稱更改Date
為更精確的MonthWithDay
。
我們可以通過記錄來縮短該類的代碼。默認情況下,隱式創建的 getter 方法的名稱與成員欄位名稱相同。
public record MonthWithDay( int month , int day ) { }
同樣,我們可以將您的Human
班級定義為一條短線中的一條記錄。
public record Human( String name , MonthWithDay monthDayWhenBorn ) { }
關于你確定生肖的方法:
public static Map<Human, String> zodiacToHuman(List<Human> humanlist){ …
……沒有必要static
。在您的場景中,這似乎是您Human
班級的一個合理功能。
提示:在面向物件編程中,usingstatic
不是面向物件的。盡量減少使用static
. 僅作為最后手段使用。
public record Human( String name , MonthWithDay monthDayWhenBorn )
{
public String zodiac ( )
{
int day = this.monthDayWhenBorn.day();
int month = this.monthDayWhenBorn.month();
if ( month == 1 )
{
if ( day < 20 )
{ return "capricornus"; }
else
{ return "aquarius"; }
}
return "other";
}
}
填充一些示例資料。
List < Human > humans =
List.of(
new Human( "Alice" , new MonthWithDay( 1 , 11 ) ) ,
new Human( "Alice" , new MonthWithDay( 1 , 22 ) ) ,
new Human( "Carol" , new MonthWithDay( 11 , 27 ) )
);
創建您的人到黃道帶地圖。
Map< Human , String > mapOfHumanToZodiac = new HashMap<>() ;
遍歷每個Human
物件,詢問它的生肖,然后放入我們的地圖中。
for ( Human human : humans )
{
mapOfHumanToZodiac.put( human , human.zodiac() );
}
轉儲到控制臺。
System.out.println( "mapOfHumanToZodiac = " mapOfHumanToZodiac );
mapOfHumanToZodiac = {Human[name=Alice, monthDayWhenBorn=MonthWithDay[month=1, day=11]]=摩羯座, Human[name=Alice, monthDayWhenBorn=MonthWithDay[month=1, day=22]]=水瓶座, Human[name =Carol, monthDayWhenBorn=MonthWithDay[月=11, 日=27]]=其他}
順便說一句,在實際作業中,我們會定義一個列舉來表示每個星座,而不是僅僅使用字串。這樣做可以提供型別安全,確保值有效(避免字串中的拼寫錯誤),并使代碼更具自我記錄性。
java.time
Java 帶有行業領先的日期時間類框架,可在java.time包中找到。這些類包括一個MonthDay
類。所以沒必要自己發明。我們可以洗掉您的MonthWithDay
課程。
調整Human
班級。
public record Human( String name , MonthDay monthDayWhenBorn ) // <-- Use java.time.MonthDay class.
{
public String zodiac ( )
{
int day = this.monthDayWhenBorn.getDayOfMonth(); // <-- Use java.time.MonthDay class.
int month = this.monthDayWhenBorn.getMonthValue(); // <-- Use java.time.MonthDay class.
if ( month == 1 )
{
if ( day < 20 )
{ return "capricornus"; }
else
{ return "aquarius"; }
}
return "other";
}
}
更改我們創建示例資料的方式。
List < Human > humans =
List.of(
new Human( "Alice" , MonthDay.of( 1 , 11 ) ) , // <-- Use java.time.MonthDay class.
new Human( "Alice" , MonthDay.of( 1 , 22 ) ) ,
new Human( "Carol" , MonthDay.of( 11 , 27 ) )
);
我們得到相同的結果。
uj5u.com熱心網友回復:
錯誤和原因
變數 Human 可能尚未初始化
它不是一個錯誤,它是一個警告,說human
變數可能是null
因為你只貼了變數human
。要初始化,您需要創建一個實體或將 null 分配給它
Human human = new Human(YOUR VALUES);
//or
Human human = null;
不能從靜態背景關系中參考非靜態方法“getDay()”
不能從靜態背景關系參考非靜態方法“getMonth()”
不創建物件就不能直接訪問類的公共方法。
筆記
根據我的理解,您給每個物件human
一個sign
值。您可以在創建每個human
物件時實作相同的值,然后再從中創建一個地圖。例如:
public class Human {
private String name;
private Date birthday;
private String sign;
public Human(String name, Date birthday) {
this.name = name;
this.birthday = birthday;
assignZodiac();
}
private void assignZodiac(){
String sign = "";
//getting birhday month and day values
int day = birthday.getDay();
int month = birthday.getMonth();
// your logic of assignment
if (month == 1) {
if (day < 20)
sign = "capricornus";
else
sign = "aquarius";
}//and so on
}
//getter setter
}
現在您可以從串列中創建地圖。例如:
// human list has been already created
Map<Human,String> humanSign=newHasmap<>();
for(Human human : humanList) {
humanSign.put(human,human.getSign()) //asuming getSign is the getter for sign filed in Human.
}
另外,我建議您將Date
類名更改為其他名稱,因為java
已經有一個同名的類。只是為了命名對流
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/485081.html