private 변수를 사용하여 클래스를 작성한 다음 해당 변수에 액세스하는 데 필요한 접근 자 및 mutator 메서드를 작성했지만 기본 클래스를 작성한 후에 실행하면 작동하지 않습니다. 왜 있다는 것은 일어나고 여기에 내 코드를 확인 않습니다액세서 및 뮤 테이터 메서드를 작성했지만 여전히 개인 변수에 액세스 할 수 없습니다! 왜?
public class DateTest{
public static void main (String [] args){
Date d1 = new Date();
Date d2 = new Date();
d1.month = "February ";
d1.day = 13;
d1.year = 1991;
d2.month = "July";
d2.day = 26;
d2.year = 1990;
d1.WriteOutput();
d2.WriteOutput();
}
}
class Date {
private String month;
private int day;
private int year;
public String getMonth(){
return month;
}
public int getDay(){
return day;
}
public int getYear(){
return year; }
public void setMonth(String m){
if (month.length()>0)
month = m;
}
public void setDay(int d){
if (day>0)
day = d; }
public void setYear(int y){
if (year>0)
year = y;
}
public void WriteOutput(){
System.out.println("Month " + month + "Day "+ day + " year" + year);
}
}
하시기 바랍니다들 그냥 나와 함께 인내심, 나는
글쎄, 나는 그것들의 설정 메소드에 의해 호출했다. 실제로 작동하지 않는다. – AbdullahR
'if (month.length()> 0)'필드를 결코 업데이트 할 수 없기 때문에, 'if (m.length()> 0)'. 다른 설정자에게도 마찬가지입니다. – Krizz
죄송합니다. 그래, 그게 내 실수라고 생각해. 고마워! – AbdullahR