1. 程式人生 > >馬昕璐201771010118《面向對象程序設計(java)》第七周學習總結

馬昕璐201771010118《面向對象程序設計(java)》第七周學習總結

列表 影響 原因 對象相等 檢查 9.png 對象 賬號登錄 結果

第一部分:理論知識學習部分

Java用於控制可見性的4個訪問權限修飾符:

1.private(只有該類可以訪問)

2.protected(該類及其子類的成員可以訪問,同一個包中的類也可訪問)

3.public(該類或非該類均可)

4.默認(相同包中的類可以訪問)

技術分享圖片

使用訪問修飾符的原因:實現受限信息隱藏

信息隱藏目的:

1. 對類中任何實現細節的更改不會影響使用該類的代碼

2. 防止用戶意外刪除數據.

3. 易於使用類

3. Object:所有類的超類

Object類是Java中所有類的祖先——每一個類都由它擴展而來。在不給出超類的情況下,Java會自動把Object作為要定義類的超類。

equals方法:測試某個對象是否同另一個對象相等。它在Object類中的實現是判斷兩個對象是否具有相同的引用。如果兩個對象具有相同的引用,它們一定是相等的

hashCode方法:導出某個對象的散列碼。散列碼是任意整數,表示對象的存儲地址。兩個相等對象的散列碼相等。

toString方法:返回一個代表該對象域值的字符串。toString方法返回字符串的格式:類名,然後在方括號中列舉域值。通過 getClass().getName()獲得類名的字符串。

枚舉類

聲明枚舉類

public enum Grade { A, B, C, D, E };

枚舉類是一個類,它的隱含超類是java.lang.Enum。枚舉類不能有public修飾的構造函數,構造函數都是隱含private,編譯器自動處理。

枚舉值隱含都是由public、static、final修飾的,無須自己添加這些修飾符。

比較兩個枚舉類型的值時,永遠不需要調用 equals方法,直接使用"=="進行相等比較

二.實驗內容和步驟

實驗1 補充以下程序中主類內main方法體,以驗證四種權限修飾符的用法。

public class TEST1 {

private String t1 = "這是TEST1的私有屬性";

public String t2 = "這是TEST1的公有屬性";

protected String t3 = "這是TEST1受保護的屬性";

String t4 = "這是TEST1的默認屬性";

private void tese1() {

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

}

public void tese2() {

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

}

protected void tese3() {

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

}

void tese4() {

System.out.println("我是TEST1無修飾符修飾的方法");

}

}

public class TEST2 extends TEST1{

private String e1 = "這是TEST2的私有屬性";

public String e2 = "這是TEST2的公有屬性";

protected String e3 = "這是TEST2受保護的屬性";

String e4 = "這是TEST2的默認屬性";

public void demo1() {

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

}

private void demo2() {

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

}

protected void demo3() {

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

}

void demo4() {

System.out.println("我是TEST2無修飾符修飾的方法");

}

}

public class Main {

public static void main(String[] args) {

TEST2 test2 = new TEST2();

/*以下設計代碼分別調用 demo1 demo2 demo3 demo4 test1 test2 test3 test4方法和t1 t2 t3 t3 e1 e2 e3 e4屬性,結合程序運行結果理解繼承和權限修飾符的用法與區別*/

}

}

package test1;

public class Main {
    public static void main(String[] args) {
        TEST2 test2=new TEST2();
        test2.demo1();
        test2.demo3();
        test2.demo4();
        test2.tese2();
        test2.tese3();
        test2.tese4();
        System.out.println(test2.e2);
        System.out.println(test2.e3);
        System.out.println(test2.e4);
        System.out.println(test2.t2);
        System.out.println(test2.t3);
        System.out.println(test2.t4);
    }
}
package test1;

public class TEST1 {
    private String t1="這是TEST1的私有屬性";
    public String t2="這是TEST2的公有屬性";
    protected String t3="這是TEST1受保護的屬性";
    String t4="這是TEST1的默認屬性";
    private void tese1() {
        System.out.println("我是TEST1用private修飾符修飾的方法 ");
    }
    public void tese2() {
        System.out.println("我是TEST1用public修飾符修飾的方法");
    }
    protected void tese3() {
        System.out.println("我是TEST1用protected修飾符修飾的方法");
    }
    void tese4() {
        System.out.println("我是TEST1無修飾符修飾的方法");
    }
}
package test1;

public class TEST2 extends TEST1 {
    private String e1="這是TEST2的私有屬性";
    public String e2="這是TEST2的公有屬性";
    protected String e3="這是TEST2受保護的屬性";
    String e4="這是TEST2的默認屬性";
    public void demo1() {
        System.out.println("我是TEST2用public修飾符修飾的方法");
    }
    private void demo2() {
        System.out.println("我是TEST2用private修飾符修飾的方法");
    }
    protected void demo3() {
        System.out.println("我是TEST2用protected修飾符修飾的方法");
    }
    void demo4() {
        System.out.println("我是TEST2無修飾符修飾的方法");
    }
}

技術分享圖片

實驗2 第五章測試程序反思,繼承知識總結。

測試程序1:

? 編輯、編譯、調試運行教材程序5-8、5-9、5-10(教材174頁-177頁);

? 結合程序運行結果,理解程序代碼,掌握Object類的定義及用法;

實驗代碼:

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

   public boolean equals(Object otherObject)
   {
      // 快速檢查對象是否相同 
      if (this == otherObject) return true;

      // 如果顯式參數為空,則必須返回false
      if (otherObject == null) return false;

      // 如果類不匹配,它們就不能相等
      if (getClass() != otherObject.getClass()) return false;

      // 現在我們知道另一個對象是非空雇員
      Employee other = (Employee) otherObject;

      // 測試字段是否具有相同的值
      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
            + "]";
   }
}
package equals;

public class Manager extends Employee
{
   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;
      Manager other = (Manager) otherObject;
      // 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 + "]";
   }
}

技術分享圖片

測試程序2:

? 編輯、編譯、調試運行教材程序5-11(教材182頁);

? 結合程序運行結果,理解程序代碼,掌握ArrayList類的定義及用法;

實驗代碼:

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)
   {
      // 用三個雇員對象填充工作人員數組列表
      ArrayList<Employee> staff = new ArrayList<>();

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

      // 把每個人的工資提高5%
      for (Employee e : staff)
         e.raiseSalary(5);

      // 打印所有員工對象的信息
      for (Employee e : staff)
         System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay="
               + e.getHireDay());
   }
}
package arrayList;

import java.time.*;

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

技術分享圖片

測試程序3:

? 編輯、編譯、調試運行程序5-12(教材189頁);

? 結合運行結果,理解程序代碼,掌握枚舉類的定義及用法;

實驗代碼:

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)
   {  
      Scanner 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);
      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");
29    private Size(String abbreviation) { this.abbreviation = abbreviation; }
   public String getAbbreviation() { return abbreviation; }

   private String abbreviation;
}

技術分享圖片

實驗3采用個人賬號登錄https://pintia.cn/,完成《2018秋季西北師範大學面向對象程序設計(Java)(ch1-ch5)測試題2》,測試時間60分鐘;

實驗4: 課後完成實驗3未完成的測試內容。

實驗總結:通過上課過程中老師關於四個修飾符與三個類的講解與實驗,我更深層次的了解了四個修飾符的權限範圍,了解了三個特殊類的含義及使用方法。代碼的邏輯結構更清晰,對我們的算法設計思維也有所幫助。同時,在自己自主學習過程中依然有碰到困難,通過自己看書查資料有所了解,在細節上的掌握不足,還需實踐提高。

馬昕璐201771010118《面向對象程序設計(java)》第七周學習總結