A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

已Mydate为例:

package text1;

public class MyDate {
        int year;
        int month;
        int day;
       
        //1.构造函数
        public MyDate(int y,int m,int d)
        {
                year = y;
                month = m;
                day = d;
        }
       
        //2.默认构造函数
        public MyDate()
        {/*
                year =  2018;
                month = 11;
                day = 11;
        */
                this(2018,11,13);//在构造方法中,this必须是第一行语句
        }
       
        //构造函数重载
               
        //4拷贝构造函数
        public MyDate(MyDate d)
        {
                //year =  d.year;
                //month = d.month;
                //day = d.day;
                this(d.year,d.month,d.day);
        }
       
        //析构函数
        public void finalize()
        {
                System.out.println("空间已释放!");
        }
       
        //String函数,输出类对象
        public String toString()
        {
                return year + "年" + month + "月" + day + "日";
        }

    //判断该类的两个对象是否相等只能用equals()方法;
        //java不支持运算符重载,对于=、!=只能判断两个对象是否引用同一个实例
        public boolean equals(MyDate d)
        {
                return this==d || d!=null && this.year==d.year && this.month==d.month && this.day==d.day;
                //this表调用当前方法的对象;对于this.year,this.month,this.day中this可省略;
                //this==d判断是否指向同一实例
        }
       
        public static void main(String arg[]) {
               
                MyDate d1 = new MyDate(2018,11,10);
                System.out.println(d1.toString());
                //d1.finalize();
               
                MyDate d2 = new MyDate();
                System.out.println(d2.toString());
                //d2.finalize();
               
                MyDate d3 = new MyDate(d2);
                System.out.println(d3.toString());
                //d3.finalize();
               
                //System.gc();//释放所有垃圾

        System.out.println("d1与d2是否为同一天:  " + d1.equals(d2));
                System.out.println("d2与d3是否为同一天 : " + d2.equals(d3));
        }
       
}

---------------------
【转载】
作者:淡茶煮清酒
原文:https://blog.csdn.net/qq_42195682/article/details/84501302


2 个回复

倒序浏览
奈斯
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马