上代碼
定義兩個class
class Year{
Int year;
public Year(int year){
this.year = year;
}
public int getValue(){
return this.year;
}
public void setValue(int year){
this.year = year;
}
}
clss Date{
Year year;
public DateUtil(){
year = new Year();
}
public DateUtil(int y){
qwq這個地方
}
public Year setYear(){
return this.year;
}
public void setYear(Year year){
this.year = year;
}
}
請問我從主函式輸入一個y。Date date = new Date(y);
那我怎么將y的值賦到自定義的class year 上去
public DateUtil(int y){
}
應該寫什么?求助
uj5u.com熱心網友回復:
???? 賦值在呼叫就行了啊uj5u.com熱心網友回復:
什么 = y;uj5u.com熱心網友回復:
clss Date{
Year year;
public DateUtil(){
year = new Year();
}
public DateUtil(int y){
qwq這個地方
}
public Year setYear(){
return this.year;
}
public void setYear(Year year){
this.year = year;
}
}這種寫法,你編譯通過了嗎?
clss Date{
Year year;
public Date(){
this.year = new Year();
}
public Date(int y){
//qwq這個地方
this.year = new Year(y);
}
public Year getYear(){
return this.year;
}
public void setYear(Year year){
this.year = year;
}
}
uj5u.com熱心網友回復:
代碼如樓上這樣寫可以
public Date(int y){
this.year = new Year(y);
}
但是你寫的問題太多了,首先構造法于類名竟然不同名,其次Year類中沒有無參構造法,你怎么呼叫Year的無參構造!?基礎太差了,好好補補。
public DateUtil(){
year = new Year();
}
uj5u.com熱心網友回復:
謝謝大佬
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/110251.html
標籤:Java相關
