類定義時使用class關鍵詞,創建實例要使用new關鍵詞。引用書上例子:衣服的型號與顏色java
程序示例git
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); } }
-運行結果spring
使用class
關鍵字定義類,在Field.java
中定義了兩個類,一個是非公開的Clothes
類,另外一個是公開(public)的Field
類(文檔中的主文檔名必須與公開類名稱一致)。Clothes
類中定義了color
和size
兩個變量,叫做定義兩個值域成員或定義兩個數據對象成員。Clothes sun = new Clothes()
爲將sun
名稱參考至新建對象,其中Clothes sun
叫做聲明參考名稱、參考變量或直接叫參考,=
是指定,new
關鍵字是創建對象實例。有幾個類就會產生幾個.class
文檔。小程序
java.util.Scanner
取得用戶輸入//使用標準類 package cc.openhome; import java.util.Scanner; public class Guess { public static void main(String[] args) { Scanner scanner=new Scanner(System.in);//創建Scanner實例 int number=(int)(Math.random()*10); int guess; do { System.out.print("猜數字(0~9):"); guess = scanner.nextInt();//取得下一個整數 }while(guess!=number); System.out.println("猜中了...XD"); } }
結果截圖:
數組
package cc.openhome; import java.math.BigDecimal; public class Compare { public static void main(String[] args) { BigDecimal a=new BigDecimal("0.1"); BigDecimal b=new BigDecimal("0.1"); BigDecimal c=a; System.out.println(a==b); System.out.println(a==c); System.out.println(a.equals(b)); } }
package cc.openhome; public class IntegerDemo { public static void main(String[] args) { int data1=10; int data2=20; Integer wrapper1=new Integer(data1); Integer wrapper2=new Integer(data2); System.out.println(data1/3); System.out.println(wrapper1.doubleValue()/3); System.out.println(wrapper1.compareTo(wrapper2)); } }
數組打印:數組就像是一個有不少小隔間的容器,每一個小隔間能夠放入一個元素,例如在數組中存入六個數字,並打印出來app
代碼和運行結果以下:dom
//不規則二維數組 package cc.openhome; public class IrregularArray { public static void main(String[] args) { int[][] arr=new int[2][];//聲明arr參考的對象會有兩個索引 arr[0]=new int[]{1,2,3,4,5};//arr[0]是長度爲5 的一維數組,想在new數組中一併指定初始值,沒必要指定數組長度。 arr[1]=new int[]{1,2,3};//arr[1]是長度爲3的一維數組。 for(int[] row:arr){ for(int value:row){ System.out.printf("%2d",value); } System.out.println(); } } }
數組複製:System.arraycopy()的五個參數分別是來源數組、來源起始索引、目的數組、目的起始索引、複製長度。Arrays.copyOf()能夠創建新數組,第二個參數是指定創建的新數組長度。ide
代碼和運行結果以下:模塊化
package cc.openhome; import java.util.Arrays; public class CopyArray { 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;//不影響score1參考的數組對象 for(int score:scores1){ System.out.printf("%3d",score); } System.out.println(); for(int score:scores2){ System.out.printf("%3d",score); } } }
由字符組成的文字符號稱爲字符串。因爲在java中是對象,因此可使用length()取得字符串長度,使用charAt()指定取得字符串中某個字符,索引從0開始,使用toUpperCase將本來小寫的字符串內容轉爲大寫的字符串內容。字符串的本質是打包字符數組的對象,是java.lang.String
類的實例。length()
是取得字符串長度,charAt()
是取得字符串中某個字符,toUpperCase()
是將本來小寫的字符串內容轉爲大寫的字符串內容。用 "" 寫下的字符串稱爲字符串常量,使用 + 鏈接字符串會產生新的String實例。以""包括的字符串,只要內容相同(序列、大小寫相同),不管在程序代碼中出現幾回,JVM都只會創建一個String實例,並在字符串池中維護。
用""寫下的字符串稱爲字符串常量。函數
代碼和運行結果以下:
package cc.openhome; 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("輸入數字: "); number=Long.parseLong(scanner.nextLine()); sum+=number; }while(number!=0); System.out.println("總和:"+sum); } }
package cc.openhome; class CashCard{ String number; int balance; int bonus; CashCard(String number,int balance,int bonus){ this.number=number; this.balance=balance; this.bonus=bonus; } } public class CardApp { public static void main(String[] args) { CashCard[] cards={ new CashCard("A001",500,0), new CashCard("A002",300,0), new CashCard("A003",1000,1), new CashCard("A004",2000,2), new CashCard("A005",3000,3) }; for(CashCard card:cards){ System.out.printf("(%s,%d,%d)%n",card.number,card.balance,card.bonus); } } }
格式:
修飾符 返回值類型 函數名(參數類型 形式參數1,參數類型 形式參數2,...) { 執行語句; return 返回值; }
返回值類型
函數運行後結果的數據類型,return
結束函數,返回值
返回給調用處,形式參數
變量,存儲調用函數時傳遞給函數的實際參數,實際參數
傳遞給形式參數的具體數值。程序示例
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); } }
使用Long、Integer、Double、Float、Boolean、Byte等類來打包基本類型,將基本類型看成對象操做。J2SE5.0後能夠自動裝箱、拆箱。
數組一被定義即有默認初始值。
for(int score:scores) { System.out.printf("學生分數:%d %n",scores); }
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(); } } }
被聲明爲static的成員,不會讓個別對象擁有,而是屬於類,被聲明爲static的方法,是將類名稱做爲名稱空間,能夠經過類名稱和"."運算符來調用static。
因爲static成員是屬於類,而非個別對象,因此在static成員中使用this,會是一種語意上的錯誤。
靜態變量必須在初始化以前進行賦值,不然會報錯。靜態方法一般都是被常用的方法,不須要每次都從新實例化,提升工做效率。
package cc.openhome;//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.printf("%s 你好! %n",scanner.nextLine()); } }
課本上的代碼敲上去以後在終端卻運行不了,出現了下圖中的錯誤。可是在IDEA中卻能夠運行。
代碼提交過程截圖:
代碼量截圖:
CH03 填空:編譯p60 Comparison.java的命令爲(javc –d xxx Comparison.java )
代碼行數(新增/累積) | 博客量(新增/累積) | 學習時間(新增/累積) | 重要成長 | |
---|---|---|---|---|
目標 | 5000行 | 20篇 | 400小時 | |
第一週 | 20/20 | 1/1 | 20/20 | 安裝了各類程序 |
第二週 | 106/126 | 1/2 | 25/45 | 掌握了託管代碼 |
第三週 | 197/200 | 1/3 | 30/30 | 大致瞭解java的對象和對象封裝 |