201871010135 张玉晶《面向对象程序设计(java)》第七周学习总结

201871010135  张玉晶《面向对象程序设计(java)》第七周学习总结

     

项目

内容

这个作业属于哪个课程

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

这个作业的要求在哪里

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

作业学习目标

  1. 掌握四种访问权限修饰符的使用特点;
  2. 掌握Object类的用途及常用API;
  3. 掌握ArrayList类的定义方法及用途;
  4. 掌握枚举类定义方法及用途;
  5. 结合本章实验内容,理解继承与多态性两个面向对象程序设计特征,并体会其优点。

1、实验目的与要求

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

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

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

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

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

2、实验内容和步骤

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

程序代码如下:

 1 class Parent {
 2     private String p1 = "这是Parent的私有属性";
 3     public String p2 = "这是Parent的公有属性";
 4     protected String p3 = "这是Parent受保护的属性";
 5     String p4 = "这是Parent的默认属性";
 6     private void pMethod1() {
 7         System.out.println("我是Parent用private修饰符修饰的方法");
 8     }
 9     public void pMethod2() {
10         System.out.println("我是Parent用public修饰符修饰的方法");
11     }
12     protected void pMethod3() {
13         System.out.println("我是Parent用protected修饰符修饰的方法");
14     }
15     void pMethod4() {
16         System.out.println("我是Parent无修饰符修饰的方法");
17     }
18 }
19 class Son extends Parent{
20     private String s1 = "这是Son的私有属性";
21     public String s2 = "这是Son的公有属性";
22     protected String s3 = "这是Son受保护的属性";
23     String s4 = "这是Son的默认属性";
24     public void sMethod1() {
25         System.out.println(a);//分别尝试显示Parent类的p1、p2、p3、p4值
26         System.out.println("我是Son用public修饰符修饰的方法");
27     }
28     private void sMethod2() {
29         System.out.println("我是Son用private修饰符修饰的方法");
30     }
31     protected void sMethod() {
32         System.out.println("我是Son用protected修饰符修饰的方法");
33     }
34     void sMethod4() {
35         System.out.println("我是Son无修饰符修饰的方法");
36     }    
37 }
38 public class Demo {
39     public static void main(String[] args) {
40         Parent parent=new Parent();
41         Son son=new Son();
42         System.out.println(b);    //分别尝试用parent调用Paren类的方法、用son调用Son类的方法    
43     }
44 }

   

对代码中的 a b 处做如下填空:

      1.

                   

          2.

                     

              3.

                     

            4.   

                      

   

 由以上可知,不管方法或属性,只要是private修饰的 ,都只能在本类中用; 子类继承父类的方法与属性。   

     

    将子类,父类,主类分开,各建一个类   :

                       

       

1.子类中调用父类的方法和属性:                        

            

2.  主类中调用Parent类的属性和方法

   

3. 主类中调用Son类的属性和方法

   

  由以上可知: 用public修饰的属性和方法是公用的,用friendly和protected修饰的属性和方法,只有在同一个包中时能访问,特别:protected修饰的,在有继承关系时,可在不同的包中访问。

  四种权限修饰符的访问范围:     

访问范围 private friendly(默认) protected public
同一个类 可以访问 可以访问 可以访问 可以访问
同一个包内的类 不可以访问 可以访问 可以访问 可以访问
不同包内的类 不可以访问 不可以访问 可以访问

可以访问

不同包并且不是子类 不可以访问 不可以访问 不可以访问 可以访问

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

测试程序1:

   EqualsTest.java    :

 1 package equals;
 2 
 3 /**
 4  * This program demonstrates the equals method.
 5  * @version 1.12 2012-01-26
 6  * @author Cay Horstmann
 7  */
 8 public class EqualsTest
 9 {
10    public static void main(String[] args)
11    {
12       Employee alice1 = new Employee("Alice Adams", 75000, 1987, 12, 15);
13       Employee alice2 = alice1;
14       Employee alice3 = new Employee("Alice Adams", 75000, 1987, 12, 15);
15       Employee bob = new Employee("Bob Brandson", 50000, 1989, 10, 1);
16 
17       System.out.println("alice1 == alice2: " + (alice1 == alice2));
18 
19       System.out.println("alice1 == alice3: " + (alice1 == alice3));
20 
21       System.out.println("alice1.equals(alice3): " + alice1.equals(alice3));
22 
23       System.out.println("alice1.equals(bob): " + alice1.equals(bob));
24 
25       System.out.println("bob.toString(): " + bob);
26 
27       Manager carl = new Manager("Carl Cracker", 80000, 1987, 12, 15);
28       Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);
29       boss.setBonus(5000);
30       System.out.println("boss.toString(): " + boss);
31       System.out.println("carl.equals(boss): " + carl.equals(boss));
32       System.out.println("alice1.hashCode(): " + alice1.hashCode());
33       System.out.println("alice3.hashCode(): " + alice3.hashCode());
34       System.out.println("bob.hashCode(): " + bob.hashCode());
35       System.out.println("carl.hashCode(): " + carl.hashCode());
36    }
37 }

  运行结果如下: 

     

 Employee.java

 1 package equals;
 2 
 3 import java.time.*;
 4 import java.util.Objects;
 5 
 6 public class Employee
 7 {
 8    private String name;
 9    private double salary;
10    private LocalDate hireDay;
11 
12    public Employee(String name, double salary, int year, int month, int day)
13    {
14       this.name = name;
15       this.salary = salary;
16       hireDay = LocalDate.of(year, month, day);
17    }
18 
19    public String getName()
20    {
21       return name;
22    }
23 
24    public double getSalary()
25    {
26       return salary;
27    }
28 
29    public LocalDate getHireDay()
30    {
31       return hireDay;
32    }
33 
34    public void raiseSalary(double byPercent)
35    {
36       double raise = salary * byPercent / 100;
37       salary += raise;
38    }
39 
40    public boolean equals(Object otherObject)
41    {
42       // 快速测试,看看这些对象是否相同
43       if (this == otherObject) return true;
44 
45       // 如果显示参数为空,则必须返回false 
46       if (otherObject == null) return false;
47 
48       // 如果类不匹配,它们就不相等
49       if (getClass() != otherObject.getClass()) return false;
50 
51       // 现在我们知道 otherObject 是一个非空雇员
52       Employee other = (Employee) otherObject;
53 
54       // 测试字段是否具有相同的值
55       return Objects.equals(name, other.name) 
56          && salary == other.salary && Objects.equals(hireDay, other.hireDay);
57    }
58 
59    public int hashCode()
60    {
61       return Objects.hash(name, salary, hireDay); 
62    }
63 
64    public String toString()
65    {
66       return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireDay=" 
67          + hireDay + "]";
68    }
69 }

运行结果如下:

     

 Manager.java

 1 package equals;
 2 
 3 public class Manager extends Employee//子类Manager继承父类Employee
 4 {
 5    private double bonus;
 6 
 7    public Manager(String name, double salary, int year, int month, int day)
 8    {
 9       super(name, salary, year, month, day); //利用super调用父类构造器
10       bonus = 0;
11    }
12 
13    public double getSalary()
14    {
15       double baseSalary = super.getSalary();
16       return baseSalary + bonus;
17    }
18 
19    public void setBonus(double bonus)
20    {
21       this.bonus = bonus;
22    }
23 
24    public boolean equals(Object otherObject)
25    {
26       if (!super.equals(otherObject)) return false;
27       Manager other = (Manager) otherObject;
28       // super.equals checked that this and other belong to the same class
29       return bonus == other.bonus;
30    }
31 
32    public int hashCode()
33    {
34       return java.util.Objects.hash(super.hashCode(), bonus);
35    }
36 
37    public String toString()
38    {
39       return super.toString() + "[bonus=" + bonus + "]";
40    }
41 }

运行结果如下:

    

测试程序2:

    ArrayListTest.java

 1 package arrayList;
 2 
 3 import java.util.*;
 4 
 5 /**
 6  * This program demonstrates the ArrayList class.
 7  * @version 1.11 2012-01-26
 8  * @author Cay Horstmann
 9  */
10 public class ArrayListTest
11 {
12    public static void main(String[] args)
13    {
14       // 用三个Employee 对象填充数组列表
15       ArrayList<Employee> staff = new ArrayList<Employee>();
16 
17       staff.add(new Employee("Carl Cracker", 75000, 1987, 12, 15));
18       staff.add(new Employee("Harry Hacker", 50000, 1989, 10, 1));
19       staff.add(new Employee("Tony Tester", 40000, 1990, 3, 15));
20 
21       // 把每个人的薪水提高%5
22       for (Employee e : staff)
23          e.raiseSalary(5);
24 
25       // 打印出关于Employee 类的所有信息
26       for (Employee e : staff)
27          System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay=" 
28             + e.getHireDay());
29    }
30 }

 运行结果如下:

    

测试程序3:

 1 package enums;
 2 
 3 import java.util.*;
 4 
 5 /**
 6  * This program demonstrates enumerated types.
 7  * @version 1.0 2004-05-24
 8  * @author Cay Horstmann
 9  */
10 public class EnumTest
11 {  
12    public static void main(String[] args)
13    {  
14       Scanner in = new Scanner(System.in);
15       System.out.print("Enter a size: (SMALL, MEDIUM, LARGE, EXTRA_LARGE) ");
16       String input = in.next().toUpperCase();
17       Size size = Enum.valueOf(Size.class, input);
18       System.out.println("size=" + size);
19       System.out.println("abbreviation=" + size.getAbbreviation());
20       if (size == Size.EXTRA_LARGE)
21          System.out.println("Good job--you paid attention to the _.");      
22    }
23 }
24 
25 enum Size
26 {
27    SMALL("S"), MEDIUM("M"), LARGE("L"), EXTRA_LARGE("XL");
28 
29    private Size(String abbreviation) { this.abbreviation = abbreviation; }
30    public String getAbbreviation() { return abbreviation; }
31 
32    private String abbreviation;
33 }

运行结果如下:

  

 

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

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);  

    }  

}

1.   代码如下:

 1 public class TestVarArgus {  
 2         public static void dealArray(int... intArray)//运用泛型数组列表,传参数,(int... intArray)表示参数的个数是可变的,但都为整型
 3         {  
 4             for (int i : intArray)  
 5                 System.out.print(i +" ");  
 6               
 7             System.out.println();  
 8         }        
 9         public static void main(String args[]){  
10             dealArray();  
11             dealArray(1);  
12             dealArray(1, 2, 3);  
13         }  
14     }

运行结果如下:

   

 2. 代码如下:

 1     public class TestVarArgus {  
 2         public static void dealArray(String... stringArray)//运用泛型数组列表,传参数,参数为字符串型
 3         {  
 4             for (String i : stringArray)  
 5                 System.out.print(i +" ");  
 6               
 7             System.out.println();  
 8         }        
 9         public static void main(String args[]){  
10             dealArray("friend");  
11             dealArray("zhang qi" );  
12             dealArray("wang xue","qi xue");  
13         }  
14     }

 运行结果如下: 

   

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

程序运行结果如下:

Parent's Constructor with a boolean parameter

Son's Constructor without parameter

Son's method()

Parent's method()

  代码如下:

 1 package zyj;
 2 
 3 public class Demo {
 4     public static void main(String[] args) {
 5         Son son = new Son();
 6         son.method();
 7     }
 8 }
 9 class Parent {
10     Parent() {
11         System.out.println("Parent's Constructor without parameter");
12     }
13     Parent(boolean b) {
14         System.out.println("Parent's Constructor with a boolean parameter");
15     }
16     public void method() {
17         System.out.println("Parent's method()");
18     }
19 }
20 class Son extends Parent {
21 //补全本类定义
22     Son(){
23         super(false);
24         System.out.println("Son's Constructor without parameter");
25     }
26     public void method() {
27         System.out.println("Son's method()");
28         super.method();
29     }
30 }

运行结果如下:

      

 实验总结:在本次实验中,验证了四种权限修饰符的范围,private只能在自身的类中访问protected在同一包不同类中能访问,但是在不同包中不能访问,(有继承关系但在不同的包中的两个类可以访问),  public是在不同的包和不同的类都可以访问默认修饰符(friendly)在同一包不同类中可以访问,不同包中不能访问。以及泛型数组列表的学习,其中传的参数是可变的,注意的是参数的类型; 还有枚举类的学习。

猜你喜欢

转载自www.cnblogs.com/zyja/p/11666162.html