- class MyString //自行建立一個字串類別
- {
- private String mString; //一個字串屬性,用來儲存產生的物件中的字串內容
-
- public void set(String ss) //設定方法,設定mString的內容
- {
- this.mString = ss;
- }
- public String get() //取用方法,讀取mString的內容
- {
- return this.mString;
- }
- }
- public class MyStringTest
- {
- public static void main(String[] args)
- {
- String stringA,stringB; //建立兩個String物件:stringA,stringB
- stringA = "hello"; //將stringA的內容設定為 "hello"
- stringB = stringA; //令stringB等於stringA
- if(stringA == stringB) //判斷stringA是否等於stringB並印出結果
- System.out.println("stringA = stringB");
- else
- System.out.println("stringA != stringB");
-
- stringA = "bye bye"; //將stringA的內容改為 "bye bye"
- if(stringA == stringB) //判斷stringA是否等於stringB並印出結果
- System.out.println("stringA = stringB");
- else
- System.out.println("stringA != stringB");
-
-
- MyString myStringA,myStringB; //建立兩個MyString物件:myStringA,myStringB
- myStringA = new MyString(); //建立myStringA的實體
- myStringA.set("hello"); //將myStringA的內容設定為 "hello"
-
- myStringB = myStringA; //令myStringB等於myStringA
- if(myStringA == myStringB) //判斷myStringA是否等於myStringB並印出結果
- System.out.println("myStringA = myStringB");
- else
- System.out.println("myStringA != myStringB");
-
- myStringA.set("bye bye");
- if(myStringA == myStringB) //判斷myStringA是否等於myStringB並印出結果
- System.out.println("myStringA = myStringB");
- else
- System.out.println("myStringA != myStringB");
- }
- }
複製代碼 |