Java---对象与类的封装

举报
谙忆 发表于 2021/05/27 01:49:45 2021/05/27
【摘要】 一、类和对象: package cn.hncu.Myclasslearn; /** * * @author hncu_chx * * Mylove amin */ /**类是一种数据类型,声明一个类就是定义了一个数据类型。 类的实例(instance)是类的取值,对象就是类的变量,一个对象能引用一个实例, 就像一个int变量i能够保存int类型的一个常数。...

一、类和对象:

package cn.hncu.Myclasslearn;
/**
 * 
 * @author hncu_chx
 *
 * Mylove amin
 */
/**类是一种数据类型,声明一个类就是定义了一个数据类型。 类的实例(instance)是类的取值,对象就是类的变量,一个对象能引用一个实例, 就像一个int变量i能够保存int类型的一个常数。 声明对象: 类  对象 MyDate d1;//声明d1是MyDate类的一个对象
**/
public class MyDate {//类声明 int year; int month; int day;//成员变量 void set(int y,int m,int d){//成员方法,设置日期值 year=y; month=m; day=d; } private void set(MyDate d) { //将当前对象值设置为参数值,重载 set(d.year,d.month,d.day); //调用重载的同名成员方法 } @Override public String toString() {   //返回中文日期字符串 return  year + "年" + month + "月" + day + "日"; } public static void main(String[] args) { MyDate d1 = new MyDate(); //声明对象、创建实例、引用赋值 d1.set(2012, 1, 1); //调用类成员方法 MyDate d2 = d1; //对象引用赋值 System.out.println("d1:"+d1.toString()+",d2:"+d2.toString()); d2.month = 10; //修改实例成员变量值 System.out.println("d1:"+d1+",d2:"+d2); //输出对象字符串描述,默认调用toString() d2 = new MyDate();  //创建另一个实例 d2.set(d1); System.out.println("d1:"+d1+",d2:"+d2); }

}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

程序运行图片:

二、类的封装

package cn.hncu.Myclasslearn;
/**
 * 
 * @author hncu_chx
 *
 * Mylove amin
 */
public class MyDate_2 { private int year,month,day; //私有成员变量 private static int thisYear; //当前年份,私有化静态成员变量 static{ thisYear = 2012; } public MyDate_2(int year,int month,int day){   //构造方法,指定日期 this.set(year,month,day); //调用本类的成员方法 } public MyDate_2(){  //无参数构造方法,指定默认日期,重载 this(1970,1,1); //调用本类已声明的其他构造方法 } public MyDate_2(MyDate_2 d){  //拷贝构造方法,日期通参数,重载 this.set(d); } private void set(int year, int month, int day) {  //设置日期值,算法不全 this.year = year; this.month = (month>=1&&month<=12)?month:1; this.day = (day>=1&&day<=31)?day:1;  //this引用不能省略 } private void set(MyDate_2 d) { //设置日期值,重载 set(d.year,d.month,d.day);//调用同名成员方法,不能使用this() } public int getYear(){ //获得年份 return this.year; } public int getMonth(){  //获得月份 return this.month; } public int getDay() {  //获得当月日期 return this.day; } public String toString(){  //返回中文日期字符串,月日占2位 return year+"年"+String.format("%02d", month)+"月"+String.format("%02d", day)+"日"; } public static int getThisYear(){//获得今年年份,静态方法 return thisYear; //访问静态成员变量 } public static boolean isLeapYear(int year){ //判断指定年份是否闰年,静态方法 return year%400==0||year%100!=0&&year%4==0; } public boolean equals(MyDate_2 d){ //比较当前日期值与d是否相等 //this指代调用当前方法的对象 return this==d||d!=null&&this.year==d.year&&this.month==d.month&&this.day==d.day; } public static int daysDfMonth(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 MyDate_2.isLeapYear(year)?29:28; default : return 0; } } public int daysOfMonth(){//返回当月天数 return daysDfMonth(this.year, this.month); } public void tomorrow(){//当前日期改为之后一天日期 this.day++; if(this.day>this.daysOfMonth()){ this.day=1; this.month++; if(this.month>12){ this.month=1; this.year ++; } } } public MyDate_2 yestoday(){ //返回当前日期的前一天日期 MyDate_2 date = new MyDate_2(this);//进行拷贝构造方法,创建实例,没有改变this date.day--; if(date.day==0){ date.month--; if(date.month==0){ date.month=12; date.year--; } date.day = daysDfMonth(date.year, date.month); } return date;//返回对象date引用的实例 }


}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
package cn.hncu.Myclasslearn;
/**
 * 
 * @author hncu_chx
 *
 * Mylove amin
 */
public class MyDate_ex { public static void main(String[] args) { System.out.println("今年是"+MyDate_2.getThisYear() +",闰年?"+MyDate_2.isLeapYear(MyDate_2.getThisYear())); MyDate_2 d1 =new MyDate_2(2012, 12, 31); MyDate_2 d2 =new MyDate_2(d1);//调用拷贝构造方法复制实例 System.out.println("d1:"+d1+",d2:"+d2+",d1==d2?"+(d1==d2)+",d1.equals(d2)?"+d1.equals(d2)); System.out.print(d1+"的明天是"); d1.tomorrow(); System.out.println(d1+"\n"+d1+"的昨天是 "+(d2=d1.yestoday())); }
}
/**日期类的设计问题讨论
 * 虽然用3个整数表示一个日期符合人的思维习惯,但很多运算实现困难,如判断日期是否有效、
 * 求多少天之前/之后的日期等。
 * 本程序中的MyDate_2中的set()方法的算法不完整,其一,仍然会产生诸如“2013-2-30”之类的
 * 错误日期;其二,将“2013-12-32”之类的错误日期改为“2013-12-1”,虽然得到了一个正确
 * 日期,但这是一种不好的程序设计习惯,因为“2013-12-1”并不是调用者希望的数据,而且调用
 * 者并不知道数据被修改了。正确的处理方法应该是抛出异常,详见以后的博客。
 * 本题这样设计知识为了演示类的封装性。
 * */

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

程序运行图片:

文章来源: chenhx.blog.csdn.net,作者:谙忆,版权归原作者所有,如需转载,请联系作者。

原文链接:chenhx.blog.csdn.net/article/details/50314513

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。