张兴盼-201871010131《面向对象程序设计(Java)》第七周学习总结

 张兴盼-201871010131《面向对象程序设计(Java)》第七周学习总结

项目

内容

这个作业属于哪个课程

http://www.cnblogs.com/nwnu-daizh/

这个作业的要求在哪里

https://www.cnblogs.com/nwnu-daizh/p/11654436.html

作业的学习目标

(1) 掌握四种访问权限修饰符的使用特点;

(2) 掌握Object的用途及常用API

(3) 掌握ArrayList类的定义方法及用法;

(4)掌握枚举类定义方法及用途;

(5)结合本章实验内容,理解继承与多态性两个面向对象程序设计特征,并体会其优点。

实验内容和步骤

实验1“System.out.println(...);”语句处按注释要求设计代码替换...,观察代码录入中IDE提示,以验证四种权限修饰符的用法

程序代码如下:

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

class Parent {

    private String p1 = "这是Parent的私有属性";

    public String p2 = "这是Parent的公有属性";

    protected String p3 = "这是Parent受保护的属性";

    String p4 = "这是Parent的默认属性";

    private void pMethod1() {

        System.out.println("我是Parent用private修饰符修饰的方法");

    }

    public void pMethod2() {

        System.out.println("我是Parent用public修饰符修饰的方法");

    }

    protected void pMethod3() {

        System.out.println("我是Parent用protected修饰符修饰的方法");

    }

    void pMethod4() {

        System.out.println("我是Parent无修饰符修饰的方法");

    }

}

class Son extends Parent{

    private String s1 = "这是Son的私有属性";

    public String s2 = "这是Son的公有属性";

    protected String s3 = "这是Son受保护的属性";

    String s4 = "这是Son的默认属性";

    public void sMethod1() {

        System.out.println(  );//分别尝试显示Parent类的p1、p2、p3、p4值

        System.out.println("我是Son用public修饰符修饰的方法");

    }

    private void sMethod2() {

        System.out.println("我是Son用private修饰符修饰的方法");

    }

    protected void sMethod() {

        System.out.println("我是Son用protected修饰符修饰的方法");

    }

    void sMethod4() {

        System.out.println("我是Son无修饰符修饰的方法");

    }  

}

public class Demo {

    public static void main(String[] args) {

        Parent parent=new Parent();

        Son son=new Son();

        System.out.println(  ); //分别尝试用parent调用Paren类的方法、用son调用Son类的方法 

    }

} 

运行结果如下:

分别尝试用parent调用Paren类的方法

 

 

 

 

 

 

 

 

son调用son

 

 

下面是将parent类放入以自己名字命名的包里面

 

 

  这个实验主要是让我们了解关于四种访问权限的修饰符:

位置

private

默认

protected

public

同一个类

同一个包里的类

不同包内的子类

不同包并且不是子类

 

 

实验2导入第5章以下示例程序,测试并进行代码注释。

测试程序1

a)运行教材程序5-85-95-10,结合程序运行结果理解程序(教材174-177页);

b) 删除程序中Employee类、Manager类中的equals()hasCode()toString()方法,背录删除方法,在代码录入中理解类中重写Object父类方法的技术要点。

Employee类代码如下:

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

package equals;

 

/**

 * This program demonstrates the equals method.

 * @version 1.12 2012-01-26

 * @author Cay Horstmann

 */

public class EqualsTest

{

   public static void main(String[] args)

   {

      var alice1 = new Employee("Alice Adams", 75000, 1987, 12, 15);

      var alice2 = alice1;

      var alice3 = new Employee("Alice Adams", 75000, 1987, 12, 15);

      var bob = new Employee("Bob Brandson", 50000, 1989, 10, 1);

 

      System.out.println("alice1 == alice2: " + (alice1 == alice2));

 

      System.out.println("alice1 == alice3: " + (alice1 == alice3));

 

      System.out.println("alice1.equals(alice3): " + alice1.equals(alice3));

 

      System.out.println("alice1.equals(bob): " + alice1.equals(bob));

 

      System.out.println("bob.toString(): " + bob);

 

      var carl = new Manager("Carl Cracker", 80000, 1987, 12, 15);

      var boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);

      boss.setBonus(5000);

      System.out.println("boss.toString(): " + boss);

      System.out.println("carl.equals(boss): " + carl.equals(boss));

      System.out.println("alice1.hashCode(): " + alice1.hashCode());

      System.out.println("alice3.hashCode(): " + alice3.hashCode());

      System.out.println("bob.hashCode(): " + bob.hashCode());

      System.out.println("carl.hashCode(): " + carl.hashCode());

   }

}

 

EqualsTest类代码如下:

package equals;

 

public class Manager extends Employee  //扩展了一个子类Manager

{

   private double bonus; //创建一个私有属性

 

   public Manager(String name, double salary, int year, int month, int day)//定义变量

   {

      super(name, salary, year, month, day);//调用了父类的构造器

      bonus = 0;

   }

 

   public double getSalary()

   {

      double baseSalary = super.getSalary();//更改器

      return baseSalary + bonus;

   }

 

   public void setBonus(double bonus)

   {

      this.bonus = bonus;

   }

 

   public boolean equals(Object otherObject)

   {

      if (!super.equals(otherObject)) return false;

      var other = (Manager) otherObject;

      // super.equals checked that this and other belong to the same class  用super.equals检查这个类和其他类是否属于同一个类

      return bonus == other.bonus;

   }

 

   public int hashCode()

   {

      return java.util.Objects.hash(super.hashCode(), bonus);

   }

 

   public String toString()//吧其他类型的数据转换为字符串类型的数据

   {

      return super.toString() + "[bonus=" + bonus + "]";

   }

}

EqualsTest类代码如下:

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

package equals;

 

import java.time.*;

import java.util.Objects;

 

public class Employee

{

   private String name;//private定义了一个只能在该类中访问的字符串变量

   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;

   }

 

   public boolean equals(Object otherObject)

   {

      // a quick test to see if the objects are identical  快速测试这些对象是否相同

      if (this == otherObject) return true;

 

      // must return false if the explicit parameter is null   如果显示参数为空,必须返回false

      if (otherObject == null) return false;

 

      // if the classes don't match, they can't be equal   如果第几个类不匹配,则他们不相同

      if (getClass() != otherObject.getClass()) return false;

 

      // now we know otherObject is a non-null Employee   //其他对象为非空Employee类

      var other = (Employee) otherObject;

 

      // test whether the fields have identical values   //测试是不是有相同值

      return Objects.equals(name, other.name)

         && salary == other.salary && Objects.equals(hireDay, other.hireDay);

   }

 

   public int hashCode()

   {

      return Objects.hash(name, salary, hireDay);

   }

 

   public String toString() //把其他类型的数据转换为字符串类型的数据

   {

      return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireDay="

         + hireDay + "]";

   }

}

Manager类代码如下:

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

package equals;

 

public class Manager extends Employee  //扩展了一个子类Manager

{

   private double bonus; //创建一个私有属性

 

   public Manager(String name, double salary, int year, int month, int day)//定义变量

   {

      super(name, salary, year, month, day);//调用了父类的构造器

      bonus = 0;

   }

 

   public double getSalary()

   {

      double baseSalary = super.getSalary();//更改器

      return baseSalary + bonus;

   }

 

   public void setBonus(double bonus)

   {

      this.bonus = bonus;

   }

 

   public boolean equals(Object otherObject)

   {

      if (!super.equals(otherObject)) return false;

      var other = (Manager) otherObject;

      // super.equals checked that this and other belong to the same class  用super.equals检查这个类和其他类是否属于同一个类

      return bonus == other.bonus;

   }

 

   public int hashCode()

   {

      return java.util.Objects.hash(super.hashCode(), bonus);

   }

 

   public String toString()//吧其他类型的数据转换为字符串类型的数据

   {

      return super.toString() + "[bonus=" + bonus + "]";

   }

}

 

其运行结果如下:

 

 

 删除程序中Employee类、Manager类中的equals()hasCode()toString()方法,背录删除方法后的代码:

Employee

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

package equals;

  

import java.time.*;

import java.util.Objects;

  

public class Employee

{

   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;

   }

  

@Override

public int hashCode() {   //重写hashCode方法,使相等的两个对象获取的HashCode也相等

    // TODO Auto-generated method stub

    return Objects.hash(name, salary, hireDay);

}

  

@Override

public boolean equals(Object obj) {

    // TODO Auto-generated method stub

    if (this == obj) return true;     //快速测试几个类的根是否相同,即是否是同一个超类。这个if语句判断两个引用是否是同一个,如果是同一个,那么这两个对象肯定相等。

    if (obj == null) return false;   //如果显示参数为空,则返回false

    if (getClass() !=obj.getClass()) return false;   //用getClass()方法得到对象的类。如果几个类不匹配,则它们不相等

  

    //其他对象是非空Employee类

    //在以上判断完成,再将得到的参数对象强制转换为该对象,考虑到父类引用子类的对象的出现,然后再判断对象的属性是否相同

    Employee other = (Employee) obj;

    //测试字段是否具有相同的值

    return Objects.equals(name, other.name) && salary == other.salary && Objects.equals(hireDay, other.hireDay);

 }

  

  

@Override

public String toString() {  //把其他类型的数据转为字符串类型的数据(toString方法可以自动生成)

    // TODO Auto-generated method stub

    return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireDay=" + hireDay + "]";

}

  

}

Manager类重写之后代码如下:

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

package equals;

  

public class Manager extends Employee     //子类:Manager类继承Employee类

{

   private double bonus;     //创建私有属性bouns

  

   public Manager(String name, double salary, int year, int month, int day)

   {

      super(name, salary, year, month, day);    //子类直接调用超类中已创建的属性

      bonus = 0;      //给bouns赋初值为空

   }

  

   public double getSalary()//访问器

   {

      double baseSalary = super.getSalary();

      return baseSalary + bonus;

   }

  

   public void setBonus(double bonus)   //更改器

   {

      this.bonus = bonus;

   }

  

   public boolean equals(Object otherObject)   //快速测试几个类的根是否相同,即是否是同一个超类

   {

      if (!super.equals(otherObject)) return false;

      Manager other = (Manager) otherObject;

       //使用super.equals检查这个类和其他是否属于同一个类

      return bonus == other.bonus;

   }

  

   public int hashCode()    //重写hashCode方法,使相等的两个对象获取的HashCode也相等

   {

      return java.util.Objects.hash(super.hashCode(), bonus);

   }

  

   public String toString()    //把其他类型的数据转为字符串类型的数据(toString方法可以自动生成)

   {

      return super.toString() + "[bonus=" + bonus + "]";

   }

}

EmployeeTest类代码如下:

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

package equals;

  

/**

 * This program demonstrates the equals method.

 * @version 1.12 2012-01-26

 * @author Cay Horstmann

 */

public class EqualsTest

{

   public static void main(String[] args)

   {

      Employee alice1 = new Employee("Alice Adams", 75000, 1987, 12, 15);

      Employee alice2 = alice1;

      Employee alice3 = new Employee("Alice Adams", 75000, 1987, 12, 15);

      Employee bob = new Employee("Bob Brandson", 50000, 1989, 10, 1);

  

      System.out.println("alice1 == alice2: " + (alice1 == alice2));

  

      System.out.println("alice1 == alice3: " + (alice1 == alice3));

  

      System.out.println("alice1.equals(alice3): " + alice1.equals(alice3));

  

      System.out.println("alice1.equals(bob): " + alice1.equals(bob));

  

      System.out.println("bob.toString(): " + bob);

  

      Manager carl = new Manager("Carl Cracker", 80000, 1987, 12, 15);

      Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);

      boss.setBonus(5000);

      System.out.println("boss.toString(): " + boss);

      System.out.println("carl.equals(boss): " + carl.equals(boss));

      System.out.println("alice1.hashCode(): " + alice1.hashCode());

      System.out.println("alice3.hashCode(): " + alice3.hashCode());

      System.out.println("bob.hashCode(): " + bob.hashCode());

      System.out.println("carl.hashCode(): " + carl.hashCode());

   }

}

程序运行结果如下图:

 

 

测试程序2

a elipse IDE中调试运行程序5-11(教材182页),结合程序运行结果理解程序;

b 掌握ArrayList类的定义及用法;

c 在程序中相关代码处添加新知识的注释;

e)设计适当的代码,测试ArrayList类的set()get()remove()size()等方法的用法。

程序代码如下:

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

package arrayList;

 

import java.util.*;

 

/**

 * This program demonstrates the ArrayList class.

 * @version 1.11 2012-01-26

 * @author Cay Horstmann

 */

public class ArrayListTest

{

   public static void main(String[] args)

   {

      // fill the staff array list with three Employee objects  用三个Employee类填充staff数组列表

      var staff = new ArrayList<Employee>();   //动态数组,可以灵活设置数组的大小

 

      staff.add(new Employee("Carl Cracker", 75000, 1987, 12, 15));

      staff.add(new Employee("Harry Hacker", 50000, 1989, 10, 1));

      staff.add(new Employee("Tony Tester", 40000, 1990, 3, 15));

 

      // raise everyone's salary by 5%   将每个人的薪水提高5%

      for (Employee e : staff)

         e.raiseSalary(5);

 

      // print out information about all Employee objects   打印出所有Employee类的信息

      for (Employee e : staff)

         System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay="

            + e.getHireDay());

   }

}

 程序运行结果如下:

 

 

实验2:测试程序3   

编辑、编译、调试运行程序5-12(教材189页),结合运行结果理解程序;

掌握枚举类的定义及用法;

在程序中相关代码处添加新知识的注释;

程序代码如下:

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

package enums;

 

import java.util.*;

 

/**

 * This program demonstrates enumerated types.

 * @version 1.0 2004-05-24

 * @author Cay Horstmann

 */

public class EnumTest 

   public static void main(String[] args)

   { 

      var in = new Scanner(System.in);

      System.out.print("Enter a size: (SMALL, MEDIUM, LARGE, EXTRA_LARGE) ");

      String input = in.next().toUpperCase();

      Size size = Enum.valueOf(Size.class, input);  //静态values方法返回枚举的所有值的数组

      System.out.println("size=" + size);

      System.out.println("abbreviation=" + size.getAbbreviation());

      if (size == Size.EXTRA_LARGE)

         System.out.println("Good job--you paid attention to the _.");     

   }

}

//定义枚举类型

enum Size

{

   SMALL("S"), MEDIUM("M"), LARGE("L"), EXTRA_LARGE("XL");

 

   private Size(String abbreviation) { this.abbreviation = abbreviation; }

   public String getAbbreviation() { return abbreviation; }

 

   private String abbreviation;

 

}//调用构造函数

 

运行结果如下:

 

 

删除Size枚举类程序代码如下:

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

package enums;

 

import java.util.*;

 

/**

 * This program demonstrates enumerated types.

 * @version 1.0 2004-05-24

 * @author Cay Horstmann

 */

public class EnumTest 

   public static void main(String[] args)

   { 

      var in = new Scanner(System.in);

      System.out.print("Enter a size: (SMALL, MEDIUM, LARGE, EXTRA_LARGE) ");

      String input = in.next().toUpperCase();

      Size size = Enum.valueOf(Size.class, input);  //静态values方法返回枚举的所有值的数组

      System.out.println("size=" + size);

      System.out.println("abbreviation=" + size.getAbbreviation());

      if (size == Size.EXTRA_LARGE)

         System.out.println("Good job--you paid attention to the _.");     

   }

}

 

enum Size

{

   SMALL("S"), MEDIUM("M"), LARGE("L"), EXTRA_LARGE("XL");

 

   private Size(String abbreviation) { this.abbreviation = abbreviation; }

   public String getAbbreviation() { return abbreviation; }

 

   private String abbreviation;

}//调用构造函数

 程序运行结果如下:

 

 

测试程序4:录入以下代码,结合程序运行结果了解方法的可变参数用法

1

2

3

4

5

6

7

8

9

10

11

12

13

public class TestVarArgus { 

    public static void dealArray(int... intArray){ 

        for (int i : intArray) 

            System.out.print(i +" "); 

           

        System.out.println(); 

    }       

    public static void main(String args[]){ 

        dealArray(); 

        dealArray(1); 

        dealArray(1, 2, 3); 

    } 

}

程序运行结果如下:

 

 

实验3:编程练习:参照输出样例补全程序,使程序输出结果与输出样例一致。

程序代码如下:

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

public class Demo {

    public static void main(String[] args) {

        Son son = new Son();

        son.method();

    }

}

 

class Parent {

    Parent() {

        System.out.println("Parent's Constructor without parameter");

    }

 

    Parent(boolean b) {

        System.out.println("Parent's Constructor with a boolean parameter");

    }

 

    public void method() {

        System.out.println("Parent's method()");

    }

}

 

class Son extends Parent {

    //补全本类定义

    Son() {

    super(true);    //调用父类有参构造

        System.out.println("Son's Constructor without parameter");

    }

    public void method() {

        System.out.println("Son's method()");

        super.method();

    }

}

  程序运行结果如下:

 

 

第三部分:实验总结

  

    在本次实验中,我们是在前两周学习的基础上融会贯通,又加深并掌握了关于继承的相关知识和实验技能.但是在完成老师布置的自主实验时,因为知识掌握能力的不足,写程序的时候思维逻辑并不是很清晰不过还是完成了题目要求的内容。通过这一个多月Java的学习,自己对程序语言又有了更深的理解,每次试验后,自己的反思和老师的点评,还有学长和同窗的指教都给了我很大的提升

 

 

 

 

 

 

 

 

 

posted @ 2019-10-14 19:40  201871010131-张兴盼  阅读(147)  评论(1编辑  收藏  举报