201871010135 張玉晶《面向對象程序設計(java)》第七週學習總結

201871010135  張玉晶《面向對象程序設計(java)》第七週學習總結html

     

項目java

內容編程

這個做業屬於哪一個課程數組

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

這個做業的要求在哪裏測試

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

做業學習目標spa

  1. 掌握四種訪問權限修飾符的使用特色;
  2. 掌握Object類的用途及經常使用API;
  3. 掌握ArrayList類的定義方法及用途;
  4. 掌握枚舉類定義方法及用途;
  5. 結合本章實驗內容,理解繼承與多態性兩個面向對象程序設計特徵,並體會其優勢。

1、實驗目的與要求設計

  (1) 掌握四種訪問權限修飾符的使用特色;code

  (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類的p一、p二、p三、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)在同一包不一樣類中能夠訪問,不一樣包中不能訪問。以及泛型數組列表的學習,其中傳的參數是可變的,注意的是參數的類型; 還有枚舉類的學習。

相關文章
相關標籤/搜索