201871010135-张玉晶《面向对象程序设计(Java)》第四周学习总结
项目 | 内容 |
这个作业属于哪个课程 | https://www.cnblogs.com/nwnu-daizh/ |
这个作业的要求在哪里 | https://www.cnblogs.com/nwnu-daizh/p/11552848.html |
作业的学习目标 | 1. 掌握类与对象的基础概念,理解类与对象的关系; 2. 掌握对象与对象变量的关系; 3. 掌握预定义类Date、LocalDate类的常用API; 4. 掌握用户自定义类的语法规则,包括实例域、静态域、构造器方法、更改器方法、访问器方法、静态方法、main方法、方法参数的定义要求;(重点、难点) 5. 掌握对象的构造方法、定义方法及使用要求;(重点); 6. 理解重载概念及用法; 掌握包的概念及用法 |
第一部分:总结第四章理论知识
一. 类与对象
1.对象
a)对象是具体实体,具有明确定义的状态和行为。
b)对象的三个主要特征:
-行为(behavior):可以对对象施加什么操作(方法)
-状态(state):当施加哪些方法时,对象如何如何响应
-标识(identity):如何辨别具体相同行为与状态的不同对象
2.类
a)类是具有相同属性和行为的一组对象的集合
b)类(class)是描述对象的模板,它定义一类对象所能拥有的数据和能完成的操作,在面向对象的程序设计中,类是构造程序的基本单元。
c)每个类由一组结构化的数据(称作实例域)和在其上的一组操作(称为方法)构成。
3.类之间的关系:
a)依赖(use-a):如果一个类中的方法操作了另一个类的对象,那么这个类就依赖于另一个类。
b)聚合(has-a):类A的对象包含类B的对象
c)继承(is-a):表示一个特定类和一个一般类之间的关系。一般来说,如果类A继承了类B,那么类A不仅继承了类B的方法和状态,而且还有属于自己的方法和状态。
4.预定义类的使用
a)已学过的预定义类:Math类、math类、String类、Scanner类等,掌握练习预定义类API的技术方法。
b)要使用预定义类的方法,只需知道方法名和参数即可,无需了解它的内部实现过程。
c)使用预定义类需要在程序开始处使用import命令导入该类所在的包路径。
5.对象与对象变量
a)在OOP中,要想使用对象,需先构造对象,并初始化对象状态,然后通过对象调用类中的办法。
b)java中,用构造器(constructor)构造并初始化对象。
c)构造器是类中一个特殊方法,该方法名与类名相同。
d)构造并初始化对象的格式:new 构造器名(参数)
1)对象初始化: 初始化Date 类对象的实例
new Date();
System.out.println(new Date());
可以将类中一个方法用于新创建的对象
String s = new Date(). toString();
2)对象变量
a)如果要多次使用初始化的对象,可将初始化的对象放在一个对象变量中。
格式: Date birthday = new Date();
b)对象变量保存了对象后,可用对象变量引用对象。例:System.out.println(birthday. to String());
c)可将一个对象变量设置为null, 表示该对象变量未引用任何对象。
6.更改器方法和访问器方法
a)更改器方法:一个类中对实例域进行修改的方法,通常更改器方法名前缀set。
b)访问器方法:一个类中对实例域进行访问的方法,通常访问器方法名前缀get。
7.用户自定义类
(1)类的定义包括两部分内容:声明和类体
(2)类体由两部分构成: a)实体域(或成员变量)定义; b)方法定义。
(3)域的定义
a)实例域(成员变量):类定义时实例域部分所定义的变量。实例域在整个类内都有效。
b)局部变量:方法体中定义的变量和方法的参数。只在定义它的方法内有效。
(4)实例域的隐藏性
局部变量与实例域名字相同时,则实例域被隐藏,即在这个方法内暂时失效。
可以将实例域定义为final,该域无更改器
8.方法定义
1)方法定义包括两部分:方法声明和方法体
其格式为:方法声明体部分{
方法体
}
2)方法的声明:名字,类型和参数等属性的说明。
注:方法的类型描述的是返回值类型,无返回值是类型为void.
3)方法体:有局部变量定义和JAVA语句组成。
重载:一个类中可以有多个方法具有相同的名字,不同类型,不同参数。
9.构造器
也叫构造方法,是类中一种特殊方法,其作用是用来构造并初始化对象。
-构造器的名字必须与他所在的类名字相同;
-每个类可以有一个以上的构造器;
-构造器可以有0个,1个,或1个以上的参数;
-构造器没有返回值;
-构造器总是被new运算符调用;
-构造器可以调用另一个构造器。
10.静态域与静态方法
1)静态域:用static修饰,每个类中只有这样的一个域;
2)静态常量:Math.PI System.out.
3)静态方法:静态方法可通过类名或对象来调用。
4)main方法:一个静态方法
11.包(package)
java常常把多个类放在一起,形成一个包,使用报的主要原因是确保类名的唯一性。
1)类的导入:一个类可以直接使用他所在包中的所有类,也可以使用来自其他包中的所有publc类。
2)访问其他包种类的两种方式:
a) 类名前加上完整的包名;
b)利用import语句导入特定的类。
3)将类放入包中
a)要把类放入一个包中,首先用package指明包的名字,且语句应为程序的第一条语句,然后才是定义类的语句。
b)如果源文件中不使用package语句指明包名,源文件的类将属于默认包。
c)当一个源文件中使用了package语句时,那么这个包中所用的类文件都必须放在与包名相匹配的的子目录中。
第二部分:实验部分
实验名称:实验三 类与对象的定义及使用
1. 实验目的:
(1) 熟悉PTA平台线上测试环境;
(2) 理解用户自定义类的定义;
(3) 掌握对象的声明;
(4) 学会使用构造函数初始化对象;
(5) 使用类属性与方法的使用掌握使用;
(6) 掌握package和import语句的用途。
2. 实验步骤与内容:
实验一 任务1
公民身份证号码按照GB11643—1999《公民身份证号码》国家标准编制,由18位数字组成:前6位为行政区划分代码,第7位至14位为出生日期码,第15位至17位为顺序码,第18位为校验码。从键盘输入1个身份证号,将身份证号的年月日抽取出来,按年-月-日格式输出。注意:输入使用Scanner类的nextLine()方法,以免出错。
输入样例:
34080019810819327X
输出样例:
1981-08-19
程序代码如下:
1 import java.util.Scanner;
2
3 public class Main {
4 public static void main(String[] args) {
5 Scanner in=new Scanner(System.in);
6 String s=in.next();
7 String y=s.substring(6,10);
8 String m=s.substring(10,12);
9 String d=s.substring(12,14);
10 System.out.println(y+"-"+m+"-"+d);
11 in.close();
12 }
13 }
知识兔运行结果如下:
任务2
studentfile.txt文件内容是某班同学的学号与姓名,利用此文件编制一个程序,将studentfile.txt文件的信息读入到内存,并提供两类查询功能:(1)输入姓名查询学号;(2)输入学号查询姓名。要求程序具有友好人机交互界面。
编程建议:
(1)从文件中读入学生信息,可以编写如下函数:
public static void StudentsFromFile(String fileName))
(2)输入姓名查找学生学号,可以编写如下函数:
public static String findStudent(String name)
(3)输入学号查找学生姓名,可以编写如下函数:
public static String findStudent(String ID)
a:用对象数组做的
程序代码如下:
1 import java.io.BufferedReader;
2 import java.io.FileReader;
3 import java.io.IOException;
4 import java.util.Scanner;
5
6 public class Main1 {
7 private static Student students[];
8 public static void main(String[] args) {
9 students=new Student[50];
10 Scanner in = new Scanner(System.in);
11 try {
12 readFile("studentfile.txt");
13 System.out.println("请选择操作,1按姓名,2按学号,3退出");
14 int i;
15 while ((i = in.nextInt()) != 3) {
16 switch (i) {
17 case 1:
18 System.out.println("请输入姓名");
19 String name = in.next();
20 Student student = findStudentByName(name);
21 if (student == null) {
22 System.out.println("没找到");
23 } else {
24 System.out.println(student.toString());
25 }
26 System.out.println("请选择操作,1按姓名,2按学号,3退出");
27 break;
28 case 2:
29 System.out.println("请输入学号");
30 String id = in.next();
31 Student student1 = findStudentById(id);
32 if (student1 == null) {
33 System.out.println("没找到");
34 } else {
35 System.out.println(student1.toString());
36
37 }
38 System.out.println("请选择操作,1按姓名,2按学号,3退出");
39 break;
40
41 default:
42 System.out.println("输入有误");
43 System.out.println("请选择操作,1按姓名,2按学号,3退出");
44 break;
45 }
46
47 }
48 } catch (IOException e) {
49 // TODO 自动生成的 catch 块
50 e.printStackTrace();
51 }finally {
52 in.close();
53 }
54
55 }
56
57 public static void readFile(String path) throws IOException {
58 FileReader reader = new FileReader(path);
59 BufferedReader br = new BufferedReader(reader);
60 String result;
61 int i=0;
62 while ((result = br.readLine()) != null) {
63 Student student = new Student();
64 student.setName(result.substring(13));
65 student.setID(result.substring(0,12));
66 students[i]=student;
67 i++;
68 }
69 br.close();
70 }
71
72 public static Student findStudentByName(String name) {
73 for (Student student : students) {
74 if (student.getName().equals(name)) {
75 return student;
76 }
77 }
78 return null;
79
80 }
81
82 public static Student findStudentById(String Id) {
83 for (Student student : students) {
84 if (student.getID().equals(Id)) {
85 return student;
86 }
87 }
88 return null;
89
90 }
91 }
92
93 class Student {
94 private String name;
95 private String ID;
96
97 public String getName() {
98 return name;
99 }
100
101 public void setName(String name) {
102 this.name = name;
103 }
104
105 public String getID() {
106 return ID;
107 }
108
109 public void setID(String iD) {
110 ID = iD;
111 }
112
113 @Override
114 public String toString() {
115 // TODO 自动生成的方法存根
116 return "姓名是:" + name + "学号是:" + ID;
117 }
118 }
知识兔运行结果如下:
b:用泛型列表做
程序代码如下:
1 import java.io.BufferedReader;
2 import java.io.FileReader;
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.Scanner;
6
7 public class Main1 {
8 // private static Student students[];
9
10 private static ArrayList<Student> list;
11
12 public static void main(String[] args) {
13 list = new ArrayList<>();
14 Scanner in = new Scanner(System.in);
15 try {
16 readFile("studentfile.txt");
17 System.out.println("请选择操作,1按姓名,2按学号,3退出");
18 int i;
19 while ((i = in.nextInt()) != 3) {
20 switch (i) {
21 case 1:
22 System.out.println("请输入姓名");
23 String name = in.next();
24 Student student = findStudentByName(name);
25 if (student == null) {
26 System.out.println("没找到");
27 } else {
28 System.out.println(student.toString());
29 }
30 System.out.println("请选择操作,1按姓名,2按学号,3退出");
31 break;
32 case 2:
33 System.out.println("请输入学号");
34 String id = in.next();
35 Student student1 = findStudentById(id);
36 if (student1 == null) {
37 System.out.println("没找到");
38 } else {
39 System.out.println(student1.toString());
40
41 }
42 System.out.println("请选择操作,1按姓名,2按学号,3退出");
43 break;
44
45 default:
46 System.out.println("输入有误");
47 System.out.println("请选择操作,1按姓名,2按学号,3退出");
48 break;
49 }
50
51 }
52 } catch (IOException e) {
53 // TODO 自动生成的 catch 块
54 e.printStackTrace();
55 }finally {
56 in.close();
57 }
58
59 }
60
61 public static void readFile(String path) throws IOException {
62 FileReader reader = new FileReader(path);
63 BufferedReader br = new BufferedReader(reader);
64 String result;
65 while ((result = br.readLine()) != null) {
66 Student student = new Student();
67 student.setName(result.substring(13));
68 student.setID(result.substring(0,12));
69 list.add(student);
70 }
71 br.close();
72 }
73
74 public static Student findStudentByName(String name) {
75 for (Student student : list) {
76 if (student.getName().equals(name)) {
77 return student;
78 }
79 }
80 return null;
81
82 }
83
84 public static Student findStudentById(String Id) {
85 for (Student student : list) {
86 if (student.getID().equals(Id)) {
87 return student;
88 }
89 }
90 return null;
91
92 }
93 }
94
95 class Student {
96 private String name;
97 private String ID;
98
99 public String getName() {
100 return name;
101 }
102
103 public void setName(String name) {
104 this.name = name;
105 }
106
107 public String getID() {
108 return ID;
109 }
110
111 public void setID(String iD) {
112 ID = iD;
113 }
114
115 @Override
116 public String toString() {
117 // TODO 自动生成的方法存根
118 return "姓名是:" + name + "学号是:" + ID;
119 }
120 }
知识兔运行结果如下:
实验2
测试程序1:
1)程序4-2代码如下:
1 import java.time.*;//导入java.time包
2
3 /**
4 * This program tests the Employee class.
5 * @version 1.13 2018-04-10
6 * @author Cay Horstmann
7 */
8 public class EmployeeTest
9 {
10 public static void main(String[] args)
11 {
12 // fill the staff array with three Employee objects(用三个employee对象填充三个数组)
13 Employee[] staff = new Employee[3];
14
15 staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15);
16 staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
17 staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15);
18
19 // raise everyone's salary by 5%(给每人涨%5的工资)
20 for (Employee e : staff)
21 e.raiseSalary(5);
22
23 // print out information about all Employee objects
24 for (Employee e : staff)
25 System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay="
26 + e.getHireDay());
27 }
28 }
29
30 class Employee
31 {
32 private String name; //实例域定义
33 private double salary; //实例域定义
34 private LocalDate hireDay; //实例域定义
35
36 public Employee(String n, double s, int year, int month, int day) //构造器的定义
37 {
38 name = n;
39 salary = s;
40 hireDay = LocalDate.of(year, month, day);
41 }
42
43 public String getName() //实例域Name的访问方法
44 {
45 return name;
46 }
47
48 public double getSalary() //实例域Salary的访问方法
49 {
50 return salary;
51 }
52
53 public LocalDate getHireDay() //实例域HireDay的访问方法
54 {
55 return hireDay;
56 }
57
58 public void raiseSalary(double byPercent)
59 {
60 double raise = salary * byPercent / 100;
61 salary += raise;
62 }
63 }
知识兔运行结果如下:
2)EmployeeText.java:
1 import java.time.*;//导入java.time包
2
3 /**
4 * This program tests the Employee class.
5 * @version 1.13 2018-04-10
6 * @author Cay Horstmann
7 */
8 public class EmployeeTest
9 {
10 public static void main(String[] args)
11 {
12 // fill the staff array with three Employee objects(用三个employee对象填充三个数组)
13 Employee[] staff = new Employee[3];
14
15 staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15);
16 staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
17 staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15);
18
19 // raise everyone's salary by 5%(给每人涨%5的工资)
20 for (Employee e : staff) //进行foreach循环
21 e.raiseSalary(5);
22
23 // print out information about all Employee objects
24 for (Employee e : staff)
25 System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay="
26 + e.getHireDay());
27 }
28 }
知识兔运行结果如下:
3)employee.java:
1 import java.time.LocalDate;
2
3 public class Employee {
4
5 private String name; //实例域定义
6 private double salary; //实例域定义
7 private LocalDate hireDay; //实例域定义
8
9 public Employee(String n, double s, int year, int month, int day) //构造器定义
10 {
11 name = n;
12 salary = s;
13 hireDay = LocalDate.of(year, month, day);
14 }
15
16 public String getName() //实例域Name访问器方法
17 {
18 return name;
19 }
20
21 public double getSalary() //实例域Salary访问器方法
22 {
23 return salary;
24 }
25
26 public LocalDate getHireDay() //实例域Hireday访问器方法
27 {
28 return hireDay;
29 }
30
31 public void raiseSalary(double byPercent)
32 {
33 double raise = salary * byPercent / 100;
34 salary += raise;
35 }
36
37
38 }
39
知识兔运行结果如下:
4)程序代码如下:
1 import java.util.Scanner;
2
3 public class student {
4 String name;
5 String sex;
6 double javascore;
7 public static void main(String[] args) {
8 System.out.println("请输入学生人数");
9 Scanner su = new Scanner(System.in);
10 int totalStudent = su.nextInt();
11 student[] stus= new student[totalStudent];
12 for(int i=0;i<totalStudent;i++) {
13 student s =new student();
14 stus[i]=s;
15 System.out.println("请输入第+“i"+"个学生的姓名");
16 s.name = su.next();
17 System.out.println("请输入第+“i"+"个学生的性别");
18 s.sex = su.next();
19 System.out.println("请输入第+“i"+"个学生的java成绩");
20 s.javascore = su.nextDouble();
21
22 }
23 printstudent(stus);
24 su.close();
25 }
26
27 public static void printstudent(student[] s) {
28 System.out.println("姓名\t性别\tjava成绩");
29 for(int i=0;i<s.length;i++) {
30 System.out.println(s[i].name+"\t"+s[i].sex+"\t"+s[i].javascore);
31 }
32 }
33 }
知识兔运行结果如下:
测试程序2:
程序代码如下:
1 /**
2 * This program demonstrates static methods.
3 * @version 1.02 2008-04-10
4 * @author Cay Horstmann
5 */
6 public class StaticTest
7 {
8 public static void main(String[] args)
9 {
10 // fill the staff array with three Employee objects
11 var staff = new Employee[3]; //构造了一个Employee 数组,并填入三个雇员对象
12
13 staff[0] = new Employee("Tom", 40000);
14 staff[1] = new Employee("Dick", 60000);
15 staff[2] = new Employee("Harry", 65000);
16
17 // print out information about all Employee objects
18 for (Employee e : staff) //调用getName 方法,getId方法和getSalary方法将每个雇员的信息打印出来
19 {
20 e.setId();
21 System.out.println("name=" + e.getName() + ",id=" + e.getId() + ",salary="
22 + e.getSalary());
23 }
24
25 int n = Employee.getNextId(); // calls static method
26 System.out.println("Next available id=" + n);
27 }
28 }
29
30 class Employee
31 {
32 private static int nextId = 1;
33
34 private String name; //实例域定义
35 private double salary;
36 private int id;
37
38 public Employee(String n, double s) //构造器定义
39 {
40 name = n;
41 salary = s;
42 id = 0;
43 }
44
45 public String getName() //实例域name的访问器方法
46 {
47 return name;
48 }
49
50 public double getSalary() //实例域salary的访问器方法
51 {
52 return salary;
53 }
54
55 public int getId() //实例域id的访问器方法
56 {
57 return id;
58 }
59
60 public void setId()
61 {
62 id = nextId; // set id to next available id
63 nextId++;
64 }
65
66 public static int getNextId() //实例域NextId的访问方法
67 {
68 return nextId; // returns static field
69 }
70
71 public static void main(String[] args) // unit test
72 {
73 var e = new Employee("Harry", 50000);
74 System.out.println(e.getName() + " " + e.getSalary());
75 }
76 }
知识兔
测试程序3:
程序代码如下:
1 /**
2 * This program demonstrates parameter passing in Java.
3 * @version 1.01 2018-04-10
4 * @author Cay Horstmann
5 */
6 public class ParamTest
7 {
8 public static void main(String[] args)
9 {
10 /*
11 * Test 1: Methods can't modify numeric parameters(方法不能修改数值参数)
12 */
13 System.out.println("Testing tripleValue:");
14 double percent = 10;
15 System.out.println("Before: percent=" + percent);
16 tripleValue(percent); //调用方法tripleSalary
17 System.out.println("After: percent=" + percent);
18
19 /*
20 * Test 2: Methods can change the state of object parameters (方法可以更改对象参数的状态)
21 */
22 System.out.println("\nTesting tripleSalary:");
23 var harry = new Employee("Harry", 50000);
24 System.out.println("Before: salary=" + harry.getSalary());
25 tripleSalary(harry); //调用方法tripleSalary
26 System.out.println("After: salary=" + harry.getSalary());
27
28 /*
29 * Test 3: Methods can't attach new objects to object parameters
30 */
31 System.out.println("\nTesting swap:");
32 var a = new Employee("Alice", 70000); //定义一个类型为var的a,并进行初始化
33 var b = new Employee("Bob", 60000);
34 System.out.println("Before: a=" + a.getName());
35 System.out.println("Before: b=" + b.getName());
36 swap(a, b); //交换函数
37 System.out.println("After: a=" + a.getName());
38 System.out.println("After: b=" + b.getName());
39 }
40
41 public static void tripleValue(double x) // doesn't work(不工作)
42 {
43 x = 3 * x;
44 System.out.println("End of method: x=" + x);
45 }
46
47 public static void tripleSalary(Employee x) // works
48 {
49 x.raiseSalary(200); //x的调用
50 System.out.println("End of method: salary=" + x.getSalary());
51 }
52 //x和y进行交换
53 public static void swap(Employee x, Employee y)
54 {
55 Employee temp = x;
56 x = y;
57 y = temp;
58 System.out.println("End of method: x=" + x.getName());
59 System.out.println("End of method: y=" + y.getName());
60 }
61 }
62
63 class Employee // simplified Employee class
64 {
65 private String name; //实例域定义
66 private double salary;
67
68 public Employee(String n, double s) //构造器定义
69 {
70 name = n;
71 salary = s;
72 }
73
74 public String getName() //实例域name的访问器方法
75 {
76 return name;
77 }
78
79 public double getSalary() ////实例域Salary的访问器方法
80 {
81 return salary;
82 }
83
84 public void raiseSalary(double byPercent)
85 {
86 double raise = salary * byPercent / 100;
87 salary += raise;
88 }
89 }
知识兔运行结果如下:
测试程序4:
程序代码如下:
1 import java.util.*;
2
3 /**
4 * This program demonstrates object construction.
5 * @version 1.02 2018-04-10
6 * @author Cay Horstmann
7 */
8 public class ConstructorTest
9 {
10 public static void main(String[] args)
11 {
12 // fill the staff array with three Employee objects (用三个employee对象填充staff数组 )
13 var staff = new Employee[3];
14
15 staff[0] = new Employee("Harry", 40000);
16 staff[1] = new Employee(60000);
17 staff[2] = new Employee();
18
19 // print out information about all Employee objects (打印有关所有员工对象的信息 )
20 for (Employee e : staff) //foreach循环
21 System.out.println("name=" + e.getName() + ",id=" + e.getId() + ",salary="
22 + e.getSalary());
23 }
24 }
25
26 class Employee
27 {
28 private static int nextId; //静态域nextId
29 private int id;
30 private String name = ""; // instance field initialization(实例字段intialization)
31 private double salary;
32
33 // static initialization block (静态intialization块)
34 static
35 {
36 var generator = new Random();
37 // set nextId to a random number between 0 and 9999 (将nextId设置为0到999之间的随机值)
38 nextId = generator.nextInt(10000);
39 }
40
41 // object initialization block (对象intialization块)
42 {
43 id = nextId;
44 nextId++;
45 }
46 // three overloaded constructors //三个重载的构造
47 public Employee(String n, double s)
48 {
49 name = n;
50 salary = s;
51 }
52
53 public Employee(double s)
54 {
55 // calls the Employee(String, double) constructor
56 this("Employee #" + nextId, s); //this用来引用当前对象
57 }
58
59 // the default constructor //错误的构造器
60 public Employee()
61 {
62 // name initialized to ""--see above
63 // salary not explicitly set--initialized to 0
64 // id initialized in initialization block
65 }
66
67 public String getName() //实例域name的访问器方法
68 {
69 return name;
70 }
71
72 public double getSalary() //实例域Salary的访问器方法
73 {
74 return salary;
75 }
76
77 public int getId() //实例域Id的访问器方法
78 {
79 return id;
80 }
81 }
知识兔运行结果如下:
测试程序5:
4-6 程序代码如下:
1 import com.horstmann.corejava.*;
2 // the Employee class is defined in that package (Employees类在该包中定义)
3
4 import static java.lang.System.*; //静态导入System类
5 /**
6 * This program demonstrates the use of packages.
7 * @version 1.11 2004-02-19
8 * @author Cay Horstmann
9 */
10 public class PackageTest
11 {
12 public static void main(String[] args)
13 {
14 // because of the import statement, we don't have to use
15 // com.horstmann.corejava.Employee here
16 var harry = new Employee("Harry Hacker", 50000, 1989, 10, 1);
17
18 harry.raiseSalary(5);
19
20 // because of the static import statement, we don't have to use System.out here
21 out.println("name=" + harry.getName() + ",salary=" + harry.getSalary());
22 }
23 }
知识兔运行结果如下:
4-7 程序代码如下:
1 package com.horstmann.corejava; //将类放入包中
2
3 // the classes in this file are part of this package (这个文件中的类就是这个包中的一部分)
4
5 import java.time.*; //java.time包的引入
6
7 // import statements come after the package statement (import语句位于package语句之后)
8
9 /**
10 * @version 1.11 2015-05-08
11 * @author Cay Horstmann
12 */
13 public class Employee
14 {
15 private String name; //实例域定义
16 private double salary;
17 private LocalDate hireDay;
18
19 public Employee(String name, double salary, int year, int month, int day) //构造器定义
20 {
21 this.name = name; //this用来引用当前对象
22 this.salary = salary;
23 hireDay = LocalDate.of(year, month, day);
24 }
25
26 public String getName() //实例域name的访问器方法
27 {
28 return name;
29 }
30
31 public double getSalary() //实例域Salary的访问器方法
32 {
33 return salary;
34 }
35
36 public LocalDate getHireDay() //实例域HireDay的访问器方法
37 {
38 return hireDay;
39 }
40
41 public void raiseSalary(double byPercent)
42 {
43 double raise = salary * byPercent / 100;
44 salary += raise;
45 }
46 }
知识兔运行结果如下:
实验总结: 通过第四周的学习,掌握类与对象的基础概念,理解类与对象的关系;以及对象与对象变量的关系; 知道了预定义类Date、LocalDate类的常用API; 以及用户自定义类的语法规则,包括实例域、静态域(static)、构造器方法、更改器方法、访问器方法、静态方法、main方法、方法参数的定义要求; 掌握对象的构造方法、定义方法及使用要求; 理解重载概念及用法;
实例域在整个类内都有效; 局部变量,只在定义它的方法内有效;final,该域无更改器;构造器可以调用另一个构造器,构造函数返回值。
我不会的也有很多,我也会认真的看书,查资料,尽量让自己掌握的更多,继续加油。