20145214 《Java程序設計》第3周學習總結


20145214 《Java程序設計》第3周學習總結

教材學習內容總結

定義類

  • 類是對象的設計圖,對象是類的實例。類定義時使用class關鍵詞,建立實例使用new關鍵詞。

  • 程序代碼如下。

      class Clothes 
      {
      String color;
      char size;
      }
    
      public class Field
      {
      	public static void main(String[] args)
      	{
      Clothes sun = new Clothes();
      Clothes spring = new Clothes();
      
      sun.color = "red";
      sun.size = 'S';
      spring.color = "green";
      spring.size = 'M';
      System.out.printf("sun(%s,%c)%n",sun.color,sun.size);
      System.out.printf("spring(%s,%c)%n",spring.color,spring.size);}}
    
  • 運行結果如下。

  • 構造函數中,由於參數與數據類型重名,需使用this將參數值指定給參數。定義構造函數代碼如下。

      class Clothes2
      {
      String color;
      char size;
      Clothes2(String color, char size)
      {
      this.color=color;
      this.size=size;
      }
      }
    
      public class Field2
      {
      public static void main(String[] args)
      {
      Clothes2 sun = new Clothes2("red",'S');
      Clothes2 spring = new Clothes2("green",'M');
      
      System.out.printf("sun(%s,%c)%n",sun.color,sun.size);
      System.out.printf("spring(%s,%c)%n",spring.color,spring.size);
      }
      } 
    
  • 運行結果如下。

標准類

  • 兩個基本標准類:java.util.Scannerjava.math.BigDecimal

  • java.util.ScannernextInt()方法會看標准輸入中,有沒有輸入下一個字符串,有的話會嘗試將之剖析為int類型。Scanner對每個基本類型都有對應的方法。java.util.Scanner代碼如下。

      import java.util.Scanner;
    
      public class Guess
      {
      public static void main(String[] args)
      {
      Scanner scanner = new Scanner (System.in);
      int number = (int) (Math.random() * 10);
      int guess;
      
      do
      {
      	System.out.printf("GUESS A NUMBER:");
      	guess = scanner.nextInt();
      }
      while(guess != number);
      
      System.out.println("YOU ARE RIGHT!");
      
      }
      } 
    
  • 運行結果如下。

  • 使用java.math.BigDecimal可以得到更好的精度。代碼如下。

      import java.math.BigDecimal;
    
      public class DecimalDemo
      {
      public static void main(String[] args)
      {
      BigDecimal operand1 = new BigDecimal ("1.0");
      BigDecimal operand2 = new BigDecimal ("0.8");
      BigDecimal result = operand1.subtract(operand2);
      
      System.out.println(result);
      
      }
      } 
    
  • 運行結果如下。

  • 用BigDecimal比較相等時,可以直接利用調用add(),再調用equals()比較兩個BigDecimal實質上是否相同。代碼如下。

      import java.math.BigDecimal;
    
      public class DecimalDemo2
      {
      public static void main(String[] args)
      {
      BigDecimal o1 = new BigDecimal ("0.1");
      BigDecimal o2 = new BigDecimal ("0.1");
      BigDecimal o3 = new BigDecimal ("0.1");
      BigDecimal result = new BigDecimal("0.3");
      if(o1.add(o2).add(o3).equals(result))
      {
      	System.out.println("=0.3");
      }
      else
      {
      	System.out.println("!=0.3");
      }
      
      
      }
      } 
    
  • 運行結果如下。

打包基本類型

  • 打包器(Long、Integer、Double、Float、Boolean等類)的主要目的是提供對象實例作為“殼”,將基本類型打包在對象之中,就可以將基本類型當作對象操作。代碼如下。

      public class IntegerDemo
      {
      public static void main(String[] args)
      {
      int data1 = 10;
      int data2 = 20;
      Integer w1 = new Integer(data1);
      Integer w2 = new Integer(data2);
      System.out.println(data1/3);
      System.out.println(w1.doubleValue()/3);
      System.out.println(w1.compareTo(w2));
      }
      } 
    
  • 運行結果如下。

數組

  • 在Java中要聲明數組並初始值。可以用for循環依次取出數組中的值,也可用更方便的增強式for循環。數組的length屬性可以取得數組長度。

  • 增強式for循環聲明二維數組代碼如下。

      public class XY
      {
      public static void main(String[] args)
      {
      int[][] cords={
      				{1,2,3},
      				{4,5,6}
      				};
      for(int[] row : cords)
      {
      	for(int value : row)
      	{
      		System.out.printf("%2d",value);
      	}
      	System.out.println();
      }
      
      }
      } 
    
  • 運行結果如下。

操作數組對象

  • 使用new建立數組后,每個索引元素會有默認值,如果默認值不符合需求,則使用java.util.Arraysfill()方法設定新建數組的元素值。代碼如下。

      import java.util.Arrays;
    
      public class score
      {
      public static void main(String[] args)
      {
      	int[] scores = new int[10];
      	for(int score : scores)
      	{
      		System.out.printf("%2d",score);
      	}
      	System.out.println();
      	Arrays.fill(scores,60);
      	for(int score : scores)
      	{
      		System.out.printf("%3d",score);
      	}
      }
      } 
    
  • 運行結果如下。

數組復制

  • 數組復制的基本做法是另行建立新數組。另一種做法是使用arrays.copyOf()方法。

  • System.arraycopy()的五個參數分別是來源數組、來源起始索引、目的數組、目的起始索引、復制長度。

  • arrays.copyOf()數組復制代碼如下。

      import java.util.Arrays;
    
      public class Copy
      {
      	public static void main(String[] args)
      	{
      		int[] scores1 = {88,81,74,68,78,76,77,85,95,93};
      		int[] scores2 = Arrays.copyOf(scores1,scores1.length);
      		for(int score : scores2)
      		{
      			System.out.printf("%3d",score);
      		}
      		System.out.println();
    
    
      		scores2[0] = 99;
      		for(int score : scores1)
      		{
      			System.out.printf("%3d",score);
      		}
      	}	
      } 
    
  • 運行結果如下。

  • arrays.copyOf()和```System.arraycopy()``用在類類型聲明的數組時,都是執行淺層復制。

  • 深層復制代碼如下。

      class Clothes2
      {
      String color;
      char size;
      Clothes2(String color, char size)
      {
      	this.color=color;
      	this.size=size;
      }
      }
    
      public class Deep
      {
      public static void main(String[] args)
      {
      		Clothes2[] c1 = {new Clothes2("red",'S'),new Clothes2("green",'M')};
      	Clothes2[] c2 = new Clothes2[c1.length];
      	for(int i = 0; i < c1.length; i++)
      	{
      		Clothes2 c = new Clothes2(c1[i].color, c1[i].size);
      		c2[i] = c;
      	}
      	c1[0].color = "yellow";
      	System.out.println(c2[0].color);
      }
      } 
    
  • 運行結果如下

字符串

  • 在Java中,字符串是java.lang.String實例,用來打包字符數組。

  • 可以使用length()取得字符串長度,使用charAt()指定取得字符串中某個字符,使用toUppercase()將原本小寫的字符串內容轉為大寫。

  • 讓用戶輸入整數,輸入0后會計算所有整數總和並顯示,代碼如下。

      import java.util.Scanner;
    
      public class Sum
      {
      public static void main(String[] args)
      {
      Scanner scanner = new Scanner(System.in);
      long sum = 0;
      long number = 0;
      do 
      {
      	System.out.print("input numbers:");
      	number = Long.parseLong(scanner.nextLine());
      	sum += number;
      }
      while(number != 0);
      System.out.println("sum:"+sum);
      }
      } 
    
  • 運行結果如下。

  • 用 "" 寫下的字符串稱為字符串常量,使用 + 連接字符串會產生新的String實例。

  • 使用 + 連接字符串,改用StringBuilder來改善的代碼如下。

      public class One
      {
      public static void main(String[] args)
      {
      StringBuilder builder = new StringBuilder();
      for(int i = 1; i < 100; i++)
      {
      	builder.append(i).append('+');
      }
      System.out.println(builder.append(100).toString());
      }
      } 
    
  • 運行結果如下。

封裝

  • 封裝目的主要是隱藏對象細節。
  • 使用private定義私有成員,封裝了類私有數據,讓用戶必須通過你提供的操作方法,經過你定義的流程才有可能存取私有數據。

類語法

  • public為公開類,在構造函數上聲明public表示其他包中的類可以直接調用這個構造函數,在方法上聲明public表示其他包的方法可以直接調用這個方法。
  • 沒有定義任何權限關鍵字時,就是包權限。
  • 只有編譯程序自動加入的構造函數,才成為默認構造函數,如果自行撰寫無參數、沒有內容的構造函數就不稱為默認構造函數了。

重載構造函數

  • 可以定義多個構造函數,只要參數類型或個數不同。

  • 定義方法時也可以進行重載,雖然調用的方法名稱相同,但根據傳遞的自變量類型不同,會調用對應的方法。

  • 方法重載代碼如下。

      class Some
      {
      void someMethod(int i)
      	{
      	System.out.println("int is used");
      	}
      void someMethod(Integer integer)
      	{
      	System.out.println("Integer is used");
      	}
      }
      public class Overload
      {
      	public static void main(String[] args)
      	{
      		Some s = new Some();
      		s.someMethod(1);
      	}
      }
    
  • 運行結果如下。

  • 若想讓上面的程序結果顯示為“Integer被調用”則代碼修改如下。

      class Some
      {
      void someMethod(int i)
      {
      	System.out.println("int is used");
      }
      void someMethod(Integer integer)
      {
      	System.out.println("Integer is used");
      }
      }
      public class Overload2
      {
      	public static void main(String[] args)
      	{
      		Some s = new Some();
      		s.someMethod(new Integer(1));
      	}
      }
    
  • 運行結果如下。

this

  • this關鍵字可以出現在類中任何地方,在構造函數與對象數據成員同名時,可用this加以區別。

  • 代碼如下。

      class Other
      {
      System.out.println("dui xiang chu shi qu kuai");
      }
      Other()
      {
      System.out.println("Other() ");
      }
      Other(int o )
      {
      this();
      System.out.println("Other(int o ) ");
      }
      public class ObjectBlock
      {
      public static void main(String[] args)
      {
      	new Other(1);
      }
      }
    

static類成員

  • 被聲明為static的成員,不會讓個別對象擁有,而是屬於類,將類名稱作為名稱空間。

  • 在static方法或區塊中不能出現this關鍵字。

  • import static語法代碼如下。

      import java.util.Scanner;
      import static java.lang.System.in;
      import static java.lang.System.out;
      public class ImportStatic
      {
      public static void main(String[] args)
      {
      	Scanner scanner = new Scanner(in);
      	out.print("請輸入姓名:");
      	out.print("%s 你好!%n",scanner.nextLine());
      }
      }
    

不定長度自變量

  • 使用不定長度自變量時,方法上聲明的不定長度參數必須是參數列最后一個。

內部類

  • 在類中再定義類。

教材學習中的問題和解決過程

  • 問題:a.add(b).add(c).equals(result)的寫法是固定的么?

  • 解決:嘗試了a.add(b).equals(result)a.equals(result)寫法,發現程序都能編譯成功並且得到正確的結果,深入學習后發現equals()可以自行定義如何比較兩對象的內含值並且不同於 == 。操作過程及截圖如下。
    嘗試的運行結果為

    嘗試的運行結果為

代碼調試中的問題和解決過程

  • 問題1:在調試使用java.util.Scanner的代碼中打印提示語“猜數字”及“猜中了”時出現錯誤。

  • 解決:將兩句話改為英文描述時運行成功了,但是仍然不清楚為什么用中文會運行失敗。

  • 問題2:嘗試運行書本137面ObjectInitialBlock.java代碼時出現錯誤。

  • 解決:反復對照后沒有發現程序輸入的錯誤,但無法解決運行時出現的錯誤。

其他(感悟、思考等,可選)

和第二周相比,第三周的學習量明顯增加了,通過第四章和第五章的學習,我對於java中更深層次的概念有了進一步的了解,也記住了很多java語言中的使用規則,雖然還不能自己解決所有的代碼調試中出現的問題,但是經過兩周的學習,在代碼調試出現問題時不會再像最初一樣慌張,開始學會冷靜下來自己嘗試着解決。另外,本周學會了開源中國的代碼托管。

學習進度條

代碼行數(新增/累積) 博客量(新增/累積) 學習時間(新增/累積) 重要成長
目標 5000行 30篇 400小時
第一周 200/200 2/2 20/20
第二周 300/500 1/3 20/40
第三周 400/900 1/4 25/65

參考資料


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM