顯示具有 Java SE::物件與封裝 標籤的文章。 顯示所有文章
顯示具有 Java SE::物件與封裝 標籤的文章。 顯示所有文章

2015年9月16日 星期三

Java 權限修飾詞

Java 權限修飾詞: private、default、protected、public
package demo;

public class Foo {
    private int result = 20;
    protected int getResult(){
        return result;
    }
}

package test;

import demo.Foo;

public class Bar extends Foo{
    private int sum = 10;
    public void reportSum(){
        sum += getResult();
        System.out.println("Sum :" + sum );
    }
}

public class Demo {
    public static void main(String[] args){
    
        Bar abc = new Bar();
        abc.reportSum();
    } 
}

2015年9月14日 星期一

傳值呼叫

Java 傳值方式:Call by Value
針對基本數值:
int x = 3;
int y = x;
// x 將數值直接 copy 給 y

針對物件:
public class Demo {
    public static void main(String[] args){
    
        Employee x = new Employee();
        System.out.println("Salary: " + x.salary);
        foo(x);
        System.out.println("New Salary: " + x.salary);
    }

    // x -> e ,直接 copy 記憶體位址值
    public static void foo(Employee e) {
        //e = new Employee();
        e.setSalary(5000.00);
    }
}

class Employee {
    double salary = 1000.00;

    void setSalary(double d) {
        this.salary = d;
    }  
}

2015年9月2日 星期三

Java 建構式

Java 建構式與預設建構式:
public class Shirt{

....omit................

//constructors
 Shirt(){}
 
 Shirt(char colorCode){
  setColorCode(colorCode);
 }
 
 Shirt(int shirtID, char colorCode){
  this(colorCode);
  setShirtID(shirtID);
 }

....omit.........

}

執行建構式的方式:
public class Demo{

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

Java 封裝的方法

Encapsulation 的方式:
public class Shirt{

 private int shirtID = 0;
 private String description = "-----";
 
 //public char colorCode = 'U';//Un-encapsulation
 
 private char colorCode = 'U' ; //R=red,G=green,B=blue,W=white
 private double price = 0.0;
 
 public char getColorCode(){
  return colorCode;
 }
 
 public void setColorCode(char newChar){
  switch(newChar){
   case 'R':
   case 'G':
   case 'B':
   case 'W':
     this.colorCode = newChar;
     break;
   default:
   System.out.println("This ColorCode is wrong!");
  }  
 }

 //顯示 Shirt 相關資料
 public void displayInformation(){
 
  System.out.println("Shirt ID: " +  shirtID);
  System.out.println("Description: " + description);
  System.out.println("Shirt Color: " + colorCode);
  System.out.println("Shirt's price: " + price);
 }
}

執行該物件的方法:
public class Demo{

 public static void main(String[] args){
 
  Shirt myShirt = new Shirt();
  
  //myShirt.colorCode = 'B';
  myShirt.setColorCode('B');
    
  System.out.println("ColorCode: " + myShirt.getColorCode());
 
 }
}

2015年8月31日 星期一

Static 的用法

Java 中的 Static 用法:

import java.util.Scanner;

public class blenderDemo2{
 static int juice = 0;
 
 public static void makeJuice(int fruit){
  
  switch(fruit){
   case 1:
    juice += 150;
    break;
   case 2:
    juice += 300;
    break;
   case 3:
    juice += 400;
    break;
  }   
 }

 public static void main(String[] args){
 
  Scanner scanner = new Scanner(System.in);
  //blenderDemo2 myBlender = new blenderDemo2();
  
  System.out.print("請選擇水果種類:1) 蘋果  2) 香蕉 3) 芒果");
  int fruit = scanner.nextInt();
    
  switch(fruit){
   case 1:
    blenderDemo2.makeJuice(1);
    System.out.printf("蘋果汁:%d CC", blenderDemo2.juice);
    
   case 2:
    blenderDemo2.makeJuice(2);
    System.out.printf("香蕉牛奶:%d CC", blenderDemo2.juice);
    
   case 3:
    blenderDemo2.makeJuice(3);
    System.out.printf("芒果冰沙:%d CC", blenderDemo2.juice);
    
  }
 }
}

2015年8月26日 星期三

Java 陣列的使用

Java 一維陣列的應用:
import java.util.Scanner;

public class OneArrayDemo{

 public static void main(String[] args){
 
  Shirt[] myShop = new Shirt[10];
  Scanner scanner = new Scanner(System.in);
  String temp1;
  
  myShop[0] = new Shirt();
   
  System.out.print("Please input the description: ");
  temp1 = scanner.nextLine();
  myShop[0].description = temp1;
  
  System.out.print("Please input the ShirtID: ");
  myShop[0].shirtID = scanner.nextInt();
    
  System.out.print("Please input the colorCode: ");
  temp1 = scanner.next();
  myShop[0].colorCode = temp1.charAt(0);
  
  System.out.print("Please input the price: ");
  myShop[0].price = scanner.nextDouble();
  
  myShop[0].displayInformation();
  
 }
}

Shirt 請參考另一篇文章!

二位陣列使用方式:
import java.util.Scanner;

public class TwoArrayDemo{

 public static void main(String[] args){
  
  int courses = 3;
  int students = 10;
  int[][] scores = new int[courses][students];
  
  System.out.println("請輸入數學科分數:");
  TwoArrayDemo.inputScore(1,students,scores);
  System.out.println();
  System.out.println("請輸入國文科分數:");
  TwoArrayDemo.inputScore(2,students,scores);
  System.out.println();
  System.out.println("請輸入英文科分數:");
  TwoArrayDemo.inputScore(3,students,scores);
  System.out.println();
  System.out.println("每位學生的平均分數:");
  TwoArrayDemo.avgScore(courses,students,scores);
 }
 
 public static void inputScore(int course,int students, int[][] scores){
  Scanner scanner = new Scanner(System.in);
  int sum = 0;
  for (int i = 0; i < students; i++){
   System.out.printf("請輸入第 %d 位學生成績:",(i+1));
   scores[course-1][i] = scanner.nextInt();
   sum += scores[course-1][i];
  }
  System.out.println("本科平均分數:" + ((double)sum)/students);
 }
 
 public static void avgScore(int courses,int students,int[][] scores){
  
  int temp = 0;
  for (int i = 0 ; i < students ; i++){
   for (int j = 0 ; j < courses ; j++){
    temp += scores[j][i];
   }
   System.out.printf("第 %d 位學生平均分數: %.2f\n",i, ((double)temp)/courses);
   temp = 0;
  }
 }
}


ArrayList 的使用方式:
import java.util.*;

public class ArrayListDemo{

 public static void main(String[] args){
 
  ArrayList myList = new ArrayList();

  myList.add("Peter");
  myList.add("James");
  myList.add("Piggy");
  myList.add("Janeson");
  myList.add("Tordy");
  
  myList.remove(0);
  myList.remove(myList.size()-1);
  myList.remove("Piggy");
  
  System.out.println(myList);
 }
}

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

2015年8月19日 星期三

Java 物件內的變數宣告

Java 物件內的變數分成 Fields 與 Local Variables ,兩者有不同的地方:
public class Shirt{

 //這是 Fields
 public int shirtID = 0;
 public String description = "-----";
 public char colorCode = 'U' ; //R=red,G=green,B=blue,W=white
 public double price = 0.0;
 
 //顯示 Shirt 相關資料
 public void displayInformation(){
 
  System.out.println("Shirt ID: " +  shirtID);
  displayDescription(this.description);
  System.out.println("Shirt Color: " + colorCode);
  System.out.println("Shirt's price: " + price);
 }

 public static void displayDescription(String d1){
   //這是Local Variables
   String displayString = "";
   displayString = "Shirt description: " + d1;
   System.out.println(displayString);
 } 
}
Java 物件的變數名稱,可以利用物件來指派與修改!
public class Demo{

 public static void main(String[] args){
 
  Shirt myShirt = new Shirt();
  myShirt.shirtID = 100;
  myShirt.colorCode = 'B';
  myShirt.price = 45.12;
  myShirt.description = "45周年紀念衫";
  
  myShirt.displayInformation();
 
 }
}

2015年8月17日 星期一

Java 物件概要

Java 類別的形成:
public class Shirt{

 public int shirtID = 0;
 public String description = "-----";
 public char colorCode = 'U' ; //R=red,G=green,B=blue,W=white
 public double price = 0.0;
 
 //顯示 Shirt 相關資料
 public void displayInformation(){
 
  System.out.println("Shirt ID: " +  shirtID);
  System.out.println("Description: " + description);
  System.out.println("Shirt Color: " + colorCode);
  System.out.println("Shirt's price: " + price);
 }
}



Java 物件的利用:
public class Demo{

 public static void main(String[] args){
 
  Shirt myShirt = new Shirt();
  myShirt.displayInformation();
 
 }
}