您的位置:首页 > 编程语言 > Java开发

学习笔记 Java_ch03_封装 2014.7.22

2014-07-22 19:09 260 查看

1、封装的日期类。代码:MyDate_ex.java

class MyDate
{
private static int ThisYear = 2014;
private int year, month, day;

MyDate()
{
this(1990, 11, 11);
}
MyDate(int year, int month, int day)
{
this.year = year;
this.month = month;
this.day = day;
}
MyDate(MyDate d)
{
this(d.year, d.month, d.day);
}

void set(int year, int month, int day)
{
this.year = year;
this.month = month;
this.day = day;
}
void setYear(int year)
{
this.year = year;
}
void setMonth(int month)
{
this.month = month;
}
void setDay(int day)
{
this.day = day;
}
int getYear()
{
return this.year;
}
int getMonth()
{
return this.month;
}
int getDay()
{
return this.day;
}

public static int getThisYear()
{
return ThisYear;
}

public static boolean isLeapYear(int y)
{
return y % 400 == 0 || y % 4 == 0 && y % 100 != 0;
}

public boolean isLeapYear()
{
return isLeapYear(this.year);
}

public static int dayOfMonth(int year, int month)
{
switch(month)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31;
case 4: case 6: case 9: case 11: return 30;
case 2: return isLeapYear(year) ? 29 : 28;
default: return 0;
}
}
public int dayOfMonth()
{
return dayOfMonth(this.year, this.month);
}

public MyDate yestoday()
{
MyDate yes = new MyDate(this);  //没有改变当前对象
yes.day--;
if(yes.day ==0)
{
yes.month--;
if(yes.month == 0)
{
yes.month = 12;
yes.year--;
}
yes.day = dayOfMonth(yes.year, yes.month);
}
return yes;
}

//public MyDate tomorrow()
public void tomorrow()
{
//MyDate tom = new MyDate(this);
this.day++;  //改变当前对象的值   为什么这里如果像yestoday()那样MyDate tom = new MyDate(this);tom.day++;的话,日期都不会改变?
if(this.day > dayOfMonth())
{
this.day = 1;
this.month++;
if(this.month > 12)
{
this.month = 1;
this.year++;
}
}
//return tom;
}

public String toString()
{
return year + "年" + month + "月" + day + "日";
}

public boolean equals(MyDate d)
{
return this == d || d != null && this.year == d.year && this.month == d.month && day == d.day;
}

}

class MyDate_ex
{
public static void main(String[] args)
{
/*
MyDate d1 = new MyDate();

d1.set(1992, 11, 07);

MyDate d2 = d1;

System.out.println("d1:" + d1 + " " + "d2:" + d2 + " " + "d1 == d2?" + (d1 == d2) + "d1.equals(d2)?" + d1.equals(d2));

d1.setYear(2014);
System.out.println("d1:" + d1 + " " + "d2:" + d2 + " " + "d1 == d2?" + (d1 == d2) + "d1.equals(d2)?" + d1.equals(d2));

d2 = new MyDate();
d2.set(d1);
System.out.println("d1:" + d1 + " " + "d2:" + d2 + " " + "d1 == d2?" + (d1 == d2) + "d1.equals(d2)?" + d1.equals(d2));
*/

//System.out.println(MyDate.getThisYear() + " " + MyDate.isLeapYear(MyDate.getThisYear()));

MyDate d3 = new MyDate(2007, 12, 25);
MyDate d4 = new MyDate(d3);
/*
System.out.println(d3 + " " + d4 + " " + (d3 == d4) + " " + d3.equals(d4));
System.out.println(d4.isLeapYear());
*/
System.out.println(d4.yestoday());

//System.out.println(d4.tomorrow());  //如果tomorrow()的返回值是void,这里就不能这么写了。
//而应该这么写:
d4.tomorrow();
System.out.println(d4);

MyDate d5 = new MyDate(1,1,1);
MyDate d6 = d5;
System.out.println("d5:" + d5 + " " + "d6:" + d6 + " " + "d5 == d6?" + (d5 == d6) + "d5.equals(d6)?" + d5.equals(d6));
}
}




2、使用对象作为成员变量并实现深拷贝的Person类。代码:MyPersonDemo.java

/*
问题:没搞懂深拷贝是否引用同一个对象?
*/
class Person
{
String name;
MyDate birthday;
private static int count = 0;

Person()
{
this("", new MyDate());
}

Person(String name, MyDate birthday)
{
this.name = name;
this.birthday = birthday;
count++;
}

Person(Person p)
{
//this(p.name, p.birthday);
}

Person(String name)
{
this(name, new MyDate());
}

public void count()
{
System.out.println(count);
}

public void setName(String name)
{
this.name = name;
}
public void setBirthday(MyDate birthday)
{
this.birthday = birthday;
}
public void set(String name, MyDate birthday)
{
this.setName(name);
this.setBirthday(birthday);
}

public String getName()
{
return this.name;
}
public MyDate getBirthday()
{
return this.birthday;
}

public int getAge(int year)
{
return year - this.birthday.getYear();
}
public int getAge()
{
return getAge(MyDate.getThisYear());
}

public String toString()
{
return this.name + "," + this.birthday;
}

public static void howMany()
{
System.out.println(count + "个Person对象");
}

public int oldThan(Person p)
{
return p.birthday.getYear() - this.birthday.getYear();
}

public void finalize()
{
System.out.println("释放对象(" + this + ")");
this.count--;
}
}

class MyPersonDemo
{
public static void main(String[] args)
{
Person p1 = new Person("小红", new MyDate(1998, 11, 9));
Person p2 = new Person(p1);

//p2.birthday.tomorrow();  //birthday被私有了
System.out.println(p1 == p2);
//得false  p1和p2不是引用的同一个对象,但是为什么p2.birthday.tomorrow();之后,p1也跟着被修改了呢?
System.out.println("p1:" + p1 + " " + "p2:" + p2);
//得p1:小红,1998年11月9日 p2:小红,1998年11月9日

p2.setName("xiaolan");
p2.setName(p2.getName().substring(0, 4) + "明");
System.out.println("p1:" + p1 + " " + "p2:" + p2);
//得p1:小红,1998年11月9日 p2:xiao明,1998年11月9日

MyDate d = p2.getBirthday();
//		d.set(d.getYear() + 2, d.getMonth(), d.getDay());
p2.setBirthday(d);
System.out.println("p1:" + p1 + " " + "p2:" + p2);
System.out.println(p1.name == p2.name);
System.out.println(p1.birthday == p2.birthday);
//得p1:小红,2000年11月9日 p2:xiao明,2000年11月9日   为什么年份一起变了?
//得false
//得true  为什么p1 p2的birthday是同一个引用?
p2.birthday = new MyDate(2003,12,1);
System.out.println(p1.birthday == p2.birthday);
//得false  new一下就不是一个引用啦?跟我想的是符合的,不过和教材上讲的合不起

Person.howMany();
System.out.println("p1:" + p1 + " " + "p2:" + p2);
//得2个Person对象
//得p1:小红,2000年11月9日 p2:xiao明,2003年12月1日

System.out.println(p1.getName() + "比" + p2.getName() + "大" + p1.oldThan(p2) + "岁");
//得小红比xiao明大3岁

p1.finalize();
Person.howMany();
//得释放对象(小红,2000年11月9日)
//得1个Person对象

System.out.println(p1.getAge());
//得14
}
}




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: