方法重写 属性封箱操作

package New_Dao;

public class MyDate {
public MyDate(int year,int month,int day){
this.year=year;
this.month=month;
this.day=day;
}
int year;
int month;
int day;

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;
}

@Override
public boolean equals(Object obj) {
    int flag = 1;
	//值比较instanceof
    if (obj instanceof MyDate){
        MyDate md = (MyDate) obj;
        flag=0;
        if (this.year != md.year){
            flag +=1;
        }
        if (this.month != md.month){
            flag +=1;
        }
        if (this.day != md.day){
            flag +=1;
        }
    }
    if (flag == 0 ){
        return true;
    }else {
        return false;
    }
}

//重写父类方法
@Override
public String toString() {
String str = this.year + “-” + this.month + “-” + this.day;
return str;
}
}

package New_Main;

import New_Dao.MyDate;
import New_Dao.Order;
import New_Dao.Person;
import New_Dao.Student;

public class Test_Main {
public static void main(String[] args) {
// Student s = new Student();
// Person p = new Person()
// Order o = new Order(123,“root”);
// Order o1 = new Order(123,“root”);
// System.out.println(o.equals(o1));
//装箱
// Integer i = new Integer(111);
// Integer i = new Integer(“123”);
// Integer i = new Integer(“qwe”); //因为不是数字类型不能装箱
// Integer i = new Integer(111);
//拆箱
// int i0= i.intValue();
// System.out.println(i0);
// Integer i1 = 111; //自动装箱
// int i2= i1; // 自动拆箱
// System.out.println(i2);

// int i = Integer.parseInt(“123”); //字符串转成基本数据类型
// float f = Float.parseFloat(“0.44”);
// boolean b = Boolean.parseBoolean( “false”);
//
// String istr = String.valueOf(i); //基础数据类型转字符串
// String fstr = String.valueOf(f);
// String bstr = String.valueOf(b);
MyDate m = new MyDate(2030,12,12);
System.out.println(m.toString()); //父类方法默认输出对象内存地址
System.out.println(m); //默认调用toString 方法

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值