201871010124-王生涛《面向对象程序设计(java)》第四周学习总结

项目

内容

这个作业属于哪个课程

<任课教师博客主页链接>https://www.cnblogs.com/nwnu-daizh/

这个作业的要求在哪里

<作业链接地址>https://www.cnblogs.com/ws-t/

作业学习目标

掌握类与对象的基础概念,理解类与对象的关系;

掌握对象与对象变量的关系;

掌握预定义类Date、LocalDate类的常用API;

掌握用户自定义类的语法规则,包括实例域、静态域、构造器方法、更改器方法、访问器方法、静态方法、main方法、方法参数的定义要求;

掌握对象的构造方法、定义方法及使用要求;(重点);

理解重载概念及用法;

掌握包的概念及用法;

 

第一部分:总结第四章理论知识

1、面向对象程序设计概述:面向对象程序设计(OOP)是一种新的程序设计思维,这种方法更接近人类处理现实世界问题的自然表示方法。

  (1)对象是面向对象编程的核心,是具体实体,具有明确定义的状态和行为。

  (2)对象的三个主要特征:

    1)行为:可以对对象施加哪些操作或者可以使用哪些方法;

    2)状态:当施加那些操作或者方法时,对象应该如何应对;

    3)标识:如何辨别具有相同行为和状态的不同对象。

  2、类(class)

   (1)类是具有相同属性和行为的一组对象的集合;

  (2)类是描述对象的模板,它定义一类对象所拥有的数据和能完成的操作,在面向对象的程序设计中,类是构造程序的基本单位;

  (3)每个类由一组结构化的数据(称作实例域)和在其上的一组操作构成。

    3、如何识别类:

        (1)过程化程序设计,必须从顶部的main函数开始编写程序;

     (2)oop的思路,首先从设计类开始,然后在每个类里面添加方法;

  (3)识别类简单规则是在问题分析过程中寻找名词,而类的方法则对应动词。

  4、类和对象的关系:

  (1)类是对象的原型,对象是类的实例,类是同类对象的抽象;

       (2)所有属于同一个类的对象都具有相同的特征和操作。

    5、类之间的关系:

  (1) 依赖:如果一个类中的方法操作了另一个类的对象,那么这个类就依赖于另一个类;

  (2)聚合:类A的对象包含类B的对象;

  (3)继承:表示一个特定类和一个 一般类之间的关系。

                             一般来说, 如果类A继承了类B,那么类A 不仅有类B的方法与状态,还有属于自己的 状态与方法。

  6、表达类关系的UML符号:

     7、预定义类的使用:

  (1)已学过的预定义类:Math类、math类、String类、Scanner类;

  (2)要使用预定义类的方法,只需知道方法名和参数即可,无需了解它的内在实现过程;

  (3)使用预定义类需要在程序开始处用import命令导入该类所在的包路经。

    8、对象与对象变量:

  (1)在oop中,要想使用对象,就必须首先构造对象,并初始化对象的状态,然后通过对象调用类中的方法;

  (2)Java中,用构造器构造并初始化对象;

  (3)构造器是类中一个特殊的方法,该方法与类名相同。不需要提供返回值;

  (4)构造并初始化对象的格式:

    new 构造器名(参数)

    9、对象的初始化实例:

  (1)Date类,定义在Java标准类库的Java.util包中;

  (2)初始化Date类对象的实例:

    new   Date();

    System.out.println(new   Date());

  (3)可以在类中一个方法用于新创建的对象:String   s = new  Date().toString();

    10、对象变量:

  (1)如果要多次使用初始化的变量,可将初始化后的变量放在一个对象变量中;

    格式:  Date birthday = new Date();

  (2)对象变量保存对象后,可用对象变量引用对象;

    System.out.println(birthday.toString());

  (3)可将一个对象变量设置为NULL,表示该对象变量未引用任何对象。

  11、更改器方法和访问器方法:

  (1)一个类中对实例域进行修改的方法,更改器前面加set;

  (2)一个类中对实例域进行访问的方法,前缀get。

 12、用户自定义类:

  (1)类的定义包括两部分内容:声明和类体

  (2)类体由两部分构成:

            一为实体域(或成员变量)定义;二为方法定义。

  (3)域的定义:

 1)实例域:类定义时实例域部分所定义的变量;

 2)局部变量:方法体中定义的变量和方法的参数;

 3)实例域的隐藏性:局部变量与实例域名字相同时,则实例域被隐藏,即在这个方法内暂时失效。

 4)实例域的有效性:在整个类内部有效,而局部变量只在定义它的方法内有效。

 5)私有实例域的更改器和访问器:有时需要获得或设置私有实例域的值,此时需提供下面三项内容:

           a)一个私有的数据库;

           b)一个共有的域访问器方法;

           c)一个共有的域访问器方法;

6)final实例域:

     可以将实例域定义为final,此类域构建对象时时必须进行初始化;即必须确保在构造器执行之前,这个域的值被设置,并且在后面的操作中,不能再对它的值进行修改,即该域无修改器。

final修饰符多用于基本数据类型域,或不可变类的域;

7)方法定义:

  a)包括方法声明和方法体;

  b)方法的声明:名字,类型和参数等属性的说明:方法的类型描述的数返回值类型;

  c)方法体由局部变量和Java语句构成;一个类中可以有多个方法具有相同的名字,不同的类型,不同的参数,这种情况叫重载;

 13、构造器:用来构造并初始化对象。

  (1)构造器的名字必须要与它所在类名字相同;

  (2)每个包可以由一个以上的构造器;

  (3)构造器可以由一个,一个以上的参数;

  (4)构造器没有返回值;

  (5)构造器总是被new运算符调用;

14、(1)静态域:绝大多数面向对象程序设计语言中,静态域被称为类域。      

    如果将域定义为static,每个类中只有一个这样的域。而每个对象对于所有的实例域却都有自己的一份拷贝。

       (2)main方法:main方法不对任何对象进行操作。静态的main方法将执行并创建程序所需要的对象。

  (3)静态常量:静态变量用的少,但是静态常量用的多。   

         (4).静态方法:用修饰的方法叫静态方法或类方法,静态方法可通过类名或对象来调用,而其他方法只能通过对象来调用。静态方法不能操作对象,不能在静态方法中访问实例域。但静态方法可以访问自身类中的静态域。可以认为静态方法是没有this参数的方法,而在一个非静态方法中,this参数是表示这个对象/对象构造器的隐式参数。

15、对象的定义:使用一个类创建一个对象时,即创建了一个类的实例域。只有通过对象才可以访问该域的公共实例域,调用类中共有的方法;

16、类的导入:一个类可以直接使用它所在的包中的所有类,也可以使用来自其他包中的所有public类。

  (1)静态导入:import语句不仅可以导入类,还增加了静态导入方法和静态域的功能;

  (2)将类放如包中:首先用package语句指明报的名字,且该语句应为程序的第一条语句,然后才是定义类的语句;

  (3)如果源文件不使用package语句指明包名,源文件的类将属于默认包。

  (4)当一个源文件中使用了package语句时,那么这个包中所有的类文件的都必须放在与包名相匹配的目录中;

  (5)当程序中所使用的类位于不同包时,起源问价和类文件必须如下组织;

17、包作用域:

  (1)类中标记为public的部分可以被任意类使用;

  (2)类中标记为private的部分只能在类中使用;

  (3)如果没有为类、方法或实例域指定访问控制修饰符public或private,这部分可以被同一包中的所有方法访问;

  (4)如果实例域不标记为private,将默认为包可见,这样做会破坏类的封装性。

18、类路径:

  (1)类存储在文件系统的子目录中,类得路径必须与包名匹配;

  (2)在命令行方式下,设置类路径,使用-classpath选项指定类路径。

第二部分:实验部分

实验名称:实验三 类与对象的定义及使用

1.  实验目的:

(1) 熟悉PTA平台线上测试环境;

(2) 理解用户自定义类的定义;

(3) 掌握对象的声明;

(4) 学会使用构造函数初始化对象;

(5) 使用类属性与方法的使用掌握使用;

(6) 掌握package和import语句的用途。

3. 实验步骤与内容:

实验1 

采用个人账号登录https://pintia.cn/,使用绑定码620781加入PTA平台NWNU-2019CST1教学班(西北师范大学 计算机科学与工程学院 2018级计算机科学与技术),完成《2019秋季西北师范大学面向对象程序设计程序设计能力测试1》,测试时间50分钟。

实验1.1

公民身份证号码按照GB11643—1999《公民身份证号码》国家标准编制,由18位数字组成:前6位为行政区划分代码,第7位至14位为出生日期码,第15位至17位为顺序码,第18位为校验码。从键盘输入1个身份证号,将身份证号的年月日抽取出来,按年-月-日格式输出。注意:输入使用Scanner类的nextLine()方法,以免出错。

输入样例:

34080019810819327X

输出样例:

1981-08-19

程序代码:

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

import java.util.Scanner;

public class test1 {

  

    public static void main(String[] args) {

        // TODO Auto-generated method stub

        Scanner in = new Scanner(System.in);

        System.out.println("please input your ID:");

        String s1 = in.nextLine();

        String s2,s3,s4;

        s2 = s1.substring(6, 10);

        s3 =s1.substring(10, 12);

        s4 = s1.substring(12, 14);

        System.out.println(s2+"-"+s3+"-"+s4);

    }

}

 

运行结果如图:

实验1.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)

程序代码:

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

109

110

111

112

113

114

115

116

117

118

119

120

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Scanner;

 

public class Main {

    // private static Student students[];

 

    private static ArrayList<Student> list;

 

    public static void main(String[] args) {

        list = new ArrayList<>();

        Scanner in = new Scanner(System.in);

        try {

            readFile("studentfile.txt");

            System.out.println("请选择操作,1按姓名,2按学号,3退出");

            int i;

            while ((i = in.nextInt()) != 3) {

                switch (i) {

                case 1:

                    System.out.println("请输入姓名");

                    String name = in.next();

                    Student student = findStudentByName(name);

                    if (student == null) {

                        System.out.println("没找到");

                    } else {

                        System.out.println(student.toString());

                    }

                    System.out.println("请选择操作,1按姓名,2按学号,3退出");

                    break;

                case 2:

                    System.out.println("请输入学号");

                    String id = in.next();

                    Student student1 = findStudentById(id);

                    if (student1 == null) {

                        System.out.println("没找到");

                    } else {

                        System.out.println(student1.toString());

 

                    }

                    System.out.println("请选择操作,1按姓名,2按学号,3退出");

                    break;

 

                default:

                    System.out.println("输入有误");

                    System.out.println("请选择操作,1按姓名,2按学号,3退出");

                    break;

                }

 

            }

        } catch (IOException e) {

            // TODO 自动生成的 catch 块

            e.printStackTrace();

        }finally {

            in.close();

        }

 

    }

 

    public static void readFile(String path) throws IOException {

        FileReader reader = new FileReader(path);

        BufferedReader br = new BufferedReader(reader);

        String result;

        while ((result = br.readLine()) != null) {

            Student student = new Student();

            student.setName(result.substring(13));

            student.setID(result.substring(0,12));

            list.add(student);

        }

        br.close();

    }

 

    public static Student findStudentByName(String name) {

        for (Student student : list) {

            if (student.getName().equals(name)) {

                return student;

            }

        }

        return null;

 

    }

 

    public static Student findStudentById(String Id) {

        for (Student student : list) {

            if (student.getID().equals(Id)) {

                return student;

            }

        }

        return null;

 

    }

}

 

class Student {

    private String name;

    private String ID;

 

    public String getName() {

        return name;

    }

 

    public void setName(String name) {

        this.name = name;

    }

 

    public String getID() {

        return ID;

    }

 

    public void setID(String iD) {

        ID = iD;

    }

 

    @Override

    public String toString() {

        // TODO 自动生成的方法存根

        return "姓名是:" + name + "学号是:" + ID;

    }

}

运行结果如下:

 

 

 

实验2 导入第4章示例程序并测试。

测试程序1:

编辑、编译、调试运行程序4-2(教材104页);

结合程序运行结果,掌握类的定义与类对象的用法,并在程序代码中添加类与对象知识应用的注释;

尝试在项目中编辑两个类文件(Employee.java、 EmployeeTest.java ),编译并运行程序。

参考教材104页EmployeeTest.java,设计StudentTest.java,定义Student类,包含name(姓名)、sex(性别)、javascore(java成绩)三个字段,编写程序,从键盘输入学生人数,输入学生信息,并按以下表头输出学生信息表:

姓名    性别   java成绩

 

 实验2  测试程序1(10分)

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
import java.time.*;
 
/**
 * This program tests the Employee class.
 * @version 1.13 2018-04-10
 * @author Cay Horstmann
 */
public class EmployeeTest   //带有public修饰的EmployeeTest类,其包含了main方法
{
   public static void main(String[] args)
   {
      // fill the staff array with three Employee objects
      Employee[] staff = new Employee[3];   //构造了一个Employee数组,并填入了三个雇员对象
 
      staff[0] = new Employee("Carl Cracker"7500019871215);
      staff[1] = new Employee("Harry Hacker"500001989101);
      staff[2] = new Employee("Tony Tester"400001990315);
 
      // raise everyone's salary by 5%
      for (Employee e : staff)   //利用Employee类的raiseSalary方法将每个雇员的薪水提高5%
         e.raiseSalary(5);
 
      // print out information about all Employee objects
      for (Employee e : staff)
         System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay="
            + e.getHireDay());   //调用getName方法、getSalary方法以及getHireDay方法将每个雇员的信息打印出来
   }
}
 
class Employee   //Employee类
{
   private String name;   //三个雇员对象的私有实例域
   private double salary;
   private LocalDate hireDay;
 
   public Employee(String n, double s, int year, int month, int day)
   {
      name = n;
      salary = s;
      hireDay = LocalDate.of(year, month, day);
   }
 
   public String getName()
   {
      return name;
   }
 
   public double getSalary()
   {
      return salary;
   }
 
   public LocalDate getHireDay()
   {
      return hireDay;
   }
 
   public void raiseSalary(double byPercent)
   {
      double raise = salary * byPercent / 100;
      salary += raise;
   }
}

  运行结果如下:

(2)

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
import java.time.LocalDate;
 
 
public class Employee {
     
 
 
   private String name;   //实例域定义
   private double salary;  //实例域定义
   private LocalDate hireDay;  //实例域定义
 
   public Employee(String n, double s, int year, int month, int day)     //构造器的定义
   {
      name = n;
      salary = s;
      hireDay = LocalDate.of(year, month, day);
   }
 
   public String getName()    //实例域name的访问器方法
   {
      return name;
   }
 
   public double getSalary()  //实例域Salary的访问器方法
   {
      return salary;
   }
 
   public LocalDate getHireDay()  ////实例域HireDay的访问器方法
   {
      return hireDay;
   }
 
   public void raiseSalary(double byPercent)  
   {
      double raise = salary * byPercent / 100;
      salary += raise;
   }
}

  运行结果如下:

(3)代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class EmployeeTest
{
   public static void main(String[] args)
   {
      // 用三个employee对象填充staff数组
      Employee[] staff = new Employee[3];    
 
      staff[0] = new Employee("Carl Cracker"7500019871215);
      staff[1] = new Employee("Harry Hacker"500001989101);
      staff[2] = new Employee("Tony Tester"400001990315);
 
      // raise everyone's salary by 5%    给每人涨5%的工资
      for (Employee e : staff)     //进行foreach循环
         e.raiseSalary(5);
 
      // print out information about all Employee objects
      for (Employee e : staff)
         System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay="
            + e.getHireDay());
   }
}

  运行结果如下:

l  参考教材104页EmployeeTest.java,设计StudentTest.java,定义Student类,包含name(姓名)、sex(性别)、javascore(java成绩)三个字段,编写程序,从键盘输入学生人数,输入学生信息,并按以下表头输出学生信息表:

  姓名    性别        java成绩

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
public class StudentTest {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Student[] staff = new Student[3];   //构造了一个Employee数组,并填入了三个雇员对象
 
          staff[0] = new Student("lulanxi""female"99);
          staff[1] = new Student("luhaonan""male"98);
          staff[2] = new Student("luyuxuan""male"97);
          for (Student e : staff)
              System.out.println("name=" + e.getName() + ",sex=" + e.getSex() + ",javascore="
                 + e.getJavascore());   //调用getName方法、getSex方法以及getJavascore法将每个雇员的信息打印出来
    }
}
class Student   //Employee类
{
   private String name;   //三个雇员对象的私有实例域
   private String sex;
   private int javascore;
 
   public Student(String n, String s, int score)
   {
      name = n;
      sex = s;
      javascore = score;
   }
    
   public String getName()
   {
      return name;
   }
 
   public String getSex()
   {
      return sex;
   }
 
   public int getJavascore()
   {
      return javascore;
   }
}  

  运行结果如下:

实验2  测试程序2(5分)

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
/**
 * This program demonstrates static methods.
 * @version 1.02 2008-04-10
 * @author Cay Horstmann
 */
public class StaticTest
{
   public static void main(String[] args)
   {
      // fill the staff array with three Employee objects
      Employee[] staff = new Employee[3];   //构造了一个Employee数组,并填入了三个雇员对象
 
      staff[0] = new Employee("Tom"40000);
      staff[1] = new Employee("Dick"60000);
      staff[2] = new Employee("Harry"65000);
 
      // print out information about all Employee objects
      for (Employee e : staff)
      {
         e.setId();
         System.out.println("name=" + e.getName() + ",id=" + e.getId() + ",salary="
            + e.getSalary());    ////调用getName方法、getId方法以及getSalary方法将每个雇员的信息打印出来
      }
 
      int n = Employee.getNextId(); // calls static method    通过类名调用这个方法,只需要访问类的静态域
      System.out.println("Next available id=" + n);  //将其输出在控制台上
   }
}
 
class Employee     //Employee类
{
   private static int nextId = 1;     //静态方法访问自身的静态域
 
   private String name;               //私有域
   private double salary;
   private int id;
 
   public Employee(String n, double s)
   {
      name = n;
      salary = s;
      id = 0;
   }
 
   public String getName()
   {
      return name;
   }
 
   public double getSalary()
   {
      return salary;
   }
 
   public int getId()
   {
      return id;
   }
 
   public void setId()
   {
      id = nextId; // set id to next available id
      nextId++;
   }
 
   public static int getNextId()
   {
      return nextId; // returns static field
   }
 
   public static void main(String[] args) // unit test
   {
      Employee e = new Employee("Harry"50000);    
      System.out.println(e.getName() + " " + e.getSalary());   //输出在控制台上
   }
}

  运行结果如下:

实验2  测试程序3(5分)

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
/**
 * This program demonstrates parameter passing in Java.
 * @version 1.01 2018-04-10
 * @author Cay Horstmann
 */
public class ParamTest
{
   public static void main(String[] args)
   {
      /*
       * Test 1: Methods can't modify numeric parameters
       */
      System.out.println("Testing tripleValue:");
      double percent = 10;         //采用按值调用,然后再调用以下方法
      System.out.println("Before: percent=" + percent);
      tripleValue(percent);
      System.out.println("After: percent=" + percent);
 
      /*
       * Test 2: Methods can change the state of object parameters
       */
      System.out.println("\nTesting tripleSalary:");
      Employee harry = new Employee("Harry"50000);
      System.out.println("Before: salary=" + harry.getSalary());
      tripleSalary(harry);
      System.out.println("After: salary=" + harry.getSalary());
 
      /*
       * Test 3: Methods can't attach new objects to object parameters
       */
      System.out.println("\nTesting swap:");
      Employee a = new Employee("Alice"70000);   //按引用调用实现交换两个雇员对象状态的方法,但是并没改变存储在a和b中的对象引用
      Employee b = new Employee("Bob"60000);
      System.out.println("Before: a=" + a.getName());
      System.out.println("Before: b=" + b.getName());
      swap(a, b);
      System.out.println("After: a=" + a.getName());
      System.out.println("After: b=" + b.getName());
   }
 
   public static void tripleValue(double x)     // doesn't work
   {
      x = 3 * x;                //假定一个方法,将一个参数的值 增加到三倍,但是percent的值依旧是10
      System.out.println("End of method: x=" + x);
   }
 
   public static void tripleSalary(Employee x)
   // works,对象引用作为参数,实现一个雇员的薪水提高两倍的操作,当调用harry = new Employee(。。。);tripleSalary(harry);实现改变对象参数的方法
   {
      x.raiseSalary(200);
      System.out.println("End of method: salary=" + x.getSalary());
   }
 
   public static void swap(Employee x, Employee y)    //编写交换两个雇员对象状态的方法
   {
      Employee temp = x;     //swap方法的参数x和y被初始化为两个对象引用的拷贝。
      x = y;      
      y = temp;
      System.out.println("End of method: x=" + x.getName());
      System.out.println("End of method: y=" + y.getName());
   }
}
 
  class  Employee   //simplified Employee class
{
   private String name;
   private double salary;
 
   public Employee (String n, double s)
   {
      name = n;
      salary = s;
   }
 
   public String getName()
   {
      return name;
   }
 
   public double getSalary()
   {
      return salary;
   }
 
   public void raiseSalary(double byPercent)
   {
      double raise = salary * byPercent / 100;
      salary += raise;
   }
}

  

  运行结果如下:

实验2  测试程序4(5分)

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
import java.util.*;
 
/**
 * This program demonstrates object construction.
 * @version 1.02 2018-04-10
 * @author Cay Horstmann
 */
public class ConstructorTest {
   public static void main(String[] args)
   {
      // fill the staff array with three Employee objects
      Employee staff = new Employee[3];   //构建一个Employee类数组,并定义三个雇员对象
 
      staff[0] = new Employee("Harry"40000);
      staff[1] = new Employee(60000);
      staff[2] = new Employee();
 
      // print out information about all Employee objects
      for (Employee e : staff)
         System.out.println("name=" + e.getName() + ",id=" + e.getId() + ",salary="
            + e.getSalary());   //调用getName方法、getId方法以及getSalary方法将每个雇员的信息打印出来
   }
}
 
class Employee     //在执行构造器之前,先执行赋值操作,调用方法对域进行初始化,使每个雇员有一个id域
{
   private static int nextId;
 
   private int id;
   private String name = ""// instance field initialization
   private double salary;
   
   // static initialization block
   static
   {
      Employee generator = new Random();
      // set nextId to a random number between 0 and 9999
      nextId = generator.nextInt(10000);
   }
 
   // object initialization block
   {
      id = nextId;
      nextId++;
   }
 
   // three overloaded constructors
   public Employee(String n, double s)   //通常使用单个字符命名参数
   {
      name = n;
      salary = s;
   }
 
   public Employee(double s)
   {
      // calls the Employee(String, double) constructor
      this("Employee #" + nextId, s);
   }
 
   // the default constructor
   public Employee()
   {
      // name initialized to ""--see above
      // salary not explicitly set--initialized to 0
      // id initialized in initialization block
   }
 
   public String getName()
   {
      return name;
   }
 
   public double getSalary()
   {
      return salary;
   }
 
   public int getId()
   {
      return id;
   }
}

  运行结果如下:

 

实验2  测试程序5(5分)

实验4.6代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import com.horstmann.corejava.*;      //将类放入一个包中,将包的名字放在源文件的开头,包中定义类的代码之前
// the Employee class is defined in that package
 
import static java.lang.System.*;
 
/**
 * This program demonstrates the use of packages.
 * @version 1.11 2004-02-19
 * @author Cay Horstmann
 */
public class PackageTest
{
   public static void main(String[] args)
   {
      // because of the import statement, we don't have to use
      // com.horstmann.corejava.Employee here
      Employee harry = new Employee("Harry Hacker"500001989101);   //
 
      harry.raiseSalary(5);     //按值调用
 
      // because of the static import statement, we don't have to use System.out here
      out.println("name=" + harry.getName() + ",salary=" + harry.getSalary());  //将其输出在控制台上
   }
}

  实验4.6运行结果:

 实验4.7代码

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
package com.horstmann.corejava;
 
// the classes in this file are part of this package
 
import java.time.*;  
 
// import statements come after the package statement
 
/**
 * @version 1.11 2015-05-08
 * @author Cay Horstmann
 */
public class Employee    //Employee类放置在com.horstmann.corejava包中
{
   private String name;
   private double salary;
   private LocalDate hireDay;
 
   public Employee(String name, double salary, int year, int month, int day)
   {
      this.name = name;
      this.salary = salary;
      hireDay = LocalDate.of(year, month, day); 
   }
 
   public String getName()
   {
      return name;
   }
 
   public double getSalary()
   {
      return salary;
   }
 
   public LocalDate getHireDay()
   {
      return hireDay;
   }
 
   public void raiseSalary(double byPercent)
   {
      double raise = salary * byPercent / 100;
      salary += raise;
   }
}

  运行结果如下:

第三部分:实验总结

 本次实验首先是编译运行程序了解文件的输入输出,在实验三的基础上,进一步使用student类编写了一个Java程序。通过本次实验,我进一步了解到对象与类的定义以及各自的特点。了解了静态域、静态常量、静态方法、工厂方法和main方法的概念,并通过实例程序去真正理解了这些概念。接着学习了对象构造,在这一节主要就学习了参数化,初始化块。最后又学习了包,虽然在之前已经解除了包这个概念,但在这一节有学习了新的概念,比如说类的导入、静态导入、将类放入包中。大致掌握了用户自定义类的语法规则,如实例域、静态域、构造器方法、更改器方法、访问器方法、静态方法、main方法、方法参数的定义要求等。

 

 

posted @ 2019-09-23 20:19  201871010124-王生涛  阅读(89)  评论(1编辑  收藏  举报