总结所有的变量怎么访问,总结所有的方法怎么访问!!!
public class Test07 {
public static void main(String[] args) {
Date t =new Date(2008,8,8);
//new 关键字 表示创建一个对象 实例话对象 表示申请空间
System.out.println(t.getYear()+":"+t.getMonth()+":"+t.getDay());//引用.成员方法
Date.seeMoon(); //类.静态方法
}
}
class Date {
//私有 实例变量year 年
private int year;
//私有 实例变量month 月
private int month;
//私有 实例变量day 日
private int day;
static String MOON="月亮";// 静态变量
//get方法获取 外部程序只能通过set方法修改, 是实例方法
public int getYear() {
return year;
}
public void setYear(int year) { //括号里是局部变量
this.year = year;
}
public int getMonth() {
return month; //也可以写成 return this.month; 就近原则
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
//无参构造
public Date() {
// this(this year,this.month,thisday);
}
//有参构造
public Date(int year,int month,int day) {
this(); //调用无参构造
this.setYear(year);
this.setMonth(month);
this.setDay(day);
}
//静态方法
public static void seeMoon() {
System.out.println(MOON+"有嫦娥");//date.MOON
//this.MOON; 错误 不能使用this static 不能使用this
}
}