public?class?Shallow_2_1?{
????private?int?year;
????private?int?month;
????private?int?day;
????//?主方法
????public?static?void?main(String[]?args)?{
????????Shallow_2_1?t?=?new?Shallow_2_1(1998,?4,?16);
????????System.out.println(t.toString());
????????System.out.println(t.equals(t));
????}
????//?無參數(shù)構(gòu)造函數(shù)
????public?Shallow_2_1(){
????}
????//?有參數(shù)構(gòu)造函數(shù)
????public?Shallow_2_1(int?year,?int?month,?int?day){
????????this.year?=?year;
????????this.month?=?month;
????????this.day?=?day;
????}
????//?生成set?get方法
????public?int?getYear(){
????????return?year;
????}
????public?void?setYear(int?year){
????????this.year?=?year;
????}
????public?int?getMonth(){
????????return?month;
????}
????public?void?setMonth(int?month){
????????this.month?=?month;
????}
????public?int?getDay(){
????????return?day;
????}
????public?void?setDay(int?day){
????????this.day?=?day;
????}
????//?生成toString方法,一般生成toString方法要重寫
????public?String?toString(){
????????return?year?+?"/"?+?month?+?"/"?+?day;
????}
????//?生成equals方法,一般生成equals方法要重寫
????@Override
????public?boolean?equals(Object?obj)?{
????????if(this?==?obj){
????????????return?true;
????????}
????????if(!(obj?instanceof?Shallow_2_1)){
????????????return?false;
????????}
????????Shallow_2_1?tt?=?(Shallow_2_1)obj;
????????return?this.year?==?tt.year?&&?this.month?==?tt.month?&&?this.day?==?tt.day;
????}
}
2021-05-05
嗯優(yōu)秀