2015年8月21日 星期五

Java 物件的利用

Java 物件的新增與使用:

Shirt 類別程式,請參考另一篇文件

public class Demo2{

  public static void main(String[] args){
  
    Shirt myShirt = new Shirt();
    Shirt yourShirt = new Shirt();
    
    myShirt = yourShirt;
    
    myShirt.colorCode = 'R';
    yourShirt.colorCode = 'G';
    
    System.out.println("Shirt color: " + myShirt.colorCode);
  
  }
}

String 類別的利用:
public class StringDemo{

 public static void main(String[] args){
 
  String myString = "Hello";
  myString = myString.concat("! This is my First program!");
  myString = myString + "\n Please fill the screen !!".trim();
  
  myString = myString + "\nHAHA..".toLowerCase();
  System.out.println(myString);
  
  myString = (myString + "\nHAHA..").toLowerCase();
  System.out.println(myString);
  
  
  int stringLength = myString.length();
  System.out.println("Length : " +  stringLength);
  
  String partString = myString.substring(6,20);
  System.out.println(partString);
  
  boolean endWord = myString.endsWith("ha..");
  System.out.println("The results: " + endWord);
 
 }
}

StringBuilder 類別的利用:

public class StringBuilderDemo{

 public static void main(String[] args){
 
  StringBuilder myString = new StringBuilder("Hello,");
  myString.append(" Java!");
  
  String h1 = "Today is Monday!";
  String h2 = "World is a good place!!";
  
  myString.insert(6, h1);
  
  System.out.println("Total length: " + myString.length());
  
  myString.delete(6,12);
  myString.replace(13,19,h2);
  
  System.out.println(myString);
 }
}