所以我們說我有3個班。Java擴展類不保存變量,它可以做到嗎?
public class sup extends sup3{
private int year, month;
private sup2 supp = null;
public sup(int year, int month){
this.year = year;
this.month = month;
super.setYear(this.year);
super.setMonth(this.month);
createClass();
}
public void createClass(){
sup2 supp = new sup2();
}
}
二等
public class sup2 extends sup3 {
public sup2(){
whatever();
}
public void whatever(){
System.out.println(getYear() + " " + getMonth());
}
}
;第三類
public class sup3 {
private int year, month;
public void setYear(int year){
this.year = year;
System.out.println("Year: " + this.year);
}
public void setMonth(int month){
this.month = month;
System.out.println("Month: " + this.month);
}
public int getYear(){
return this.year;
}
public int getMonth(){
return this.month;
}
}
很顯然主
public class Main {
public static void main(String[] args) {
sup suppp = new sup(2000, 15);
}
}
即使我設置sup3時變量槽一流的我所以基本上看不到他們儘管我可以訪問sup3課程,但在sup2課程中。我不會在任何地方製作和保存擴展sup3類,爲什麼我無法在任何地方看到設置變量?
它只是打印出0 0
SUP2擴展自己的sup3時 – Jdman1699
注意的實例的實例'sup'有_TWO different_'year' s和_two different_'month's。你重複了私有變量。 –
@LouisWasserman因爲它們在類的構造函數參數和變量中是相同的名字? – CyprusN