一.理论
1.面向对象的三大特点:
封装性:内部操作对外部而言不可见;
继承性:无需重新编写原来的类就可以使用现有类的所有功能;
多态性:一个类实例的相同方法在不同情形有不同的表现形式。
2.类与对象的定义与使用
1>.类的组成:
属性:描述对象的具体特点;
方法:操作行为。
2>类的语法
修饰符 变量类型 变量名称
3>方法定义
修饰符 返回值类型 方法名(形参列表){
方法实现语句;
}
4>.构造函数定义:(与类名相同;没有返回值)
修饰符 类名称 (形参列表)
5>.构造对象的语法
类名称 变量名称 =new 类名称(实参列表)
3.访问对象的属性
内部:直接通过属性(方法)名称访问;
外部:引用.方法(属性)名称。
二.实践练习
1.
- Date 存储 年-月-日 信息
- 原则: 一切从用户角度出发
- 功能:
-
1) 初始化
-
i. 传入年/月/日
-
2. 不传,今天 回头
-
2) 多少天之后的年/月/日
-
3) 多少天之前的年/月/日
public class Date {
public int year;
public int month;
public int day;
public int[] day_of_month = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
// 构造方法
// 年支持的范围 [1840, 2020]
// 月支持的范围 [1, 12]
// 日支持的范围
public Date(int year, int month, int day) {
// 用户传入参数的合法性校验
if (year < 1840 || year > 2020) {
System.err.println("年的支持范围是 [1840, 2020]");
return;
}
if (month < 1 || month > 12) {
System.err.println("不是地球人的月份");
return;
}
if (day < 1 || day > calcDaysOfMonth(year, month)) {
System.err.println("天数不对");
return;
}
this.year = year;
this.month = month;
this.day = day;
}
public int calcDaysOfMonth(int year, int month) {
if (month != 2) {
return day_of_month[month - 1];
}
if (isLeapYear(year)) {
return 29;
} else {
return 28;
}
}
public boolean isLeapYear(int year) {
if (year % 4 == 0 && year % 100 != 0) {
return true;
}
if (year % 400 == 0) {
return true;
}
return false;
}
public Date after(int days) {
day += days;
while (day > calcDaysOfMonth(year, month)) {
day -= calcDaysOfMonth(year, month);
month += 1;
if (month > 12) {
month = 1;
year += 1;
}
}
return this;
}
public Date before(int days) {
while(day<days){
day+=calcDaysOfMonth(year, month);
month--;
if(month<1){
month=12;
year-=1;
}
}
day-=days;
return this;
}
public String toString() {
return String.format("%04d-%02d-%02d", year, month, day);
}
public static void main(String[] args) {
Date d = new Date(2019, 7, 20);
Date r = d.after(80);
System.out.println(r.toString());
Date l=new Date(2019,7,18);
Date n = l.before(80);
System.out.println(n.toString());
}
}
结果:
- Time 存储 时:分:秒 信息
- 原则: 一切从用户角度出发
- 功能:
-
1) 初始化
-
i. 传入时分秒
-
2. 不传,秒 回头
-
2) 多少秒之后的时分秒
-
3) 多少天之前的时分秒
public class Time{
public int hour;
public int minute;
public int second;
public Time(int hour,int minute,int second){
if(hour<0||hour>23){
System.err.println("时的支持范围是 [0,23]");
return ;
}
if(minute<0||minute>59){
System.err.println("分输入有误");
return ;
}
if(second<0||second>59){
System.err.println("秒输入有误");
return ;
}
this.hour=hour;
this.minute=minute;
this.second=second;
}
public Time after(int seconds) {
second += seconds;
while (second > 59) {
second -= 59;
minute += 1;
if (minute > 59) {
minute = 0;
hour += 1;
}
}
return this;
}
public Time before(int seconds) {
while(second<seconds){
second+=60;
minute--;
if(minute<0){
minute=59;
hour-=1;
}
}
second-=seconds;
return this;
}
public String toString2() {
return String.format("%02d:%02d:%02d", hour, minute, second);
}
public static void main(String[] args) {
Time d = new Time(16, 46, 20);
Time r = d.after(80);
System.out.println(r.toString2());
Time l=new Time(16, 46, 20);
Time n = l.before(80);
System.out.println(n.toString2());
}
}
结果: