星期四, 10月 25, 2012
Nagios with openSUSE 12.2 小記
這次的OSSF 工作坊主講 Nagios with openSUSE 12.2
此次的簡報
http://ppt.cc/zG_I
Lab 文件
http://ppt.cc/kD7l
首先要感謝 OSSF 工作坊 ( http://www.openfoundry.org/tw/activities ) 的活動支持
讓我們有好的練習場地
另外更要感謝國家高速網路中心 Ezilla 團隊 ( http://ezilla.info/index.php/EzillaTW/HomePage ) 提供我們雲端的練習平台
此次我們的練習環境使用 EasyCloud 服務( http://easycloud.nchc.org.tw/ )
只要使用者是支援 HTML 5 的瀏覽器 ( Google Chrome / Firefox )就可以連接到 EasyCloud 雲端簡單龍服務.
EasyCloud 簡單龍服務前端是 HTML 5 的介面, 後端是 KVM 虛擬化服務, 使用者建立完VM之後可以透過 VNC 或是 SSH (windows 透過 Remote Desktop) 來進行虛擬機器的操作.
這個對於需要複雜環境的練習或是多主機的練習但是受於場地限制的研討會, 是個天大的福音.
使用者可以使用他們的小筆電 ( 出來參加活動應該不想練身體扛主機出來吧 ... ) 透過 EasyCloud 進行大型環境的操作練習. ^___^
剛剛完成台北場.
照片可以參考
http://www.flickr.com/groups/opensuse/pool/
再次謝謝 OSSF 工作坊 以及國網中心 Ezilla 團隊
enjoy it ~~
星期三, 10月 24, 2012
OCPJP Day 11
enum
列舉
Notes:
- Java 列舉的時候常用 static + final
- 舊式的列舉型態在runtime 時期才能知道輸入的參數不正確, 但是無法在編譯時期就知道參數不正確.
*舊式的列舉型態存在很多問題
- 列印出來的資料不具意義
- 無型別的安全性
*新式列舉型態
enum
Notes:
- enum 的地位等同於 class 或是 interface
- 列舉的名稱就是輸出的字串
*enum
與 switch
語法
Notes:
- 有型別的安全性
- 不需要寫default
ESeasons.java
public
enum ESeasons {
//直接列舉,不需要在後面加上
;
//SPRING,SUMMER,FALL,WINTER
//
下面是簡化的結果,
其實會被寫成public
static final ESeasons SPRING = new ESeasons();
SPRING("春天"),
SUMMER("夏天"),
FALL("秋天"),
WINTER("冬天");
//
private
final String name;
private
ESeasons(String name){
this.name
= name;
}
//
public
String toString(){
return
name;
}
TestEseasons.java
package
mod11;
public
class TestEseasons {
public
static void main(String args[]){
System.out.println(ESeasons.SPRING);
}
}
Lab:
使用 setupDJ
進行反組譯
Notes:
- 在linux底下我找到 jd-gui 套件來進行反組譯
- jd-gui 其實要在32bits 環境, 使用64位元環境要安裝相關套件才可以執行
- 安裝zypper install libgthread-2_0-0-32bit
先談Chapter
14 Collection
Collection
三大議題
- 順序
- 重複
- 排序
Notes:
- Collection 與陣列的不同是, 空間不夠的時候會自動長大.(應用於取得資料未知大小的時候, 因為陣列必須先固定大小)
*Collection
的特性
- Collection就是物件的群組化
- 只要是物件或是基本型別(via autoboxing)都可以使用add()方法加入Collection物件中
- 次序性
- 重複性(透過 hashcode 與 equals 來判斷)
- 排序性
*Collection
的階層關係
Notes:
Java
的Collection
分三大體系
- Set <<interface>> 沒有順序(不記住加進來的順序), 不可重複(重複的資料會自動被剔除掉)
- HashSet (Non-Thread-safe)
- LinkedHashSet (有順序,不可重複的Set)
- SortedSet <<interface>>
- TreeSet (Non-Thread-safe)(會排序的Set)(排序的方式是用第一個字母出現在萬用字元表內的順序)
- List <<interface>> 有順序, 可重複
- ArrayList (Non-Thread-safe) (在查詢的效能較好)
- Vector (Thread-safe)(多執行緒的環境使用)
- Stack (Thread-safe) (先進後出資料結構)(利用 push 與 pop 操作)
- LinkedList (Non-Thread-safe) (在資料異動上面會比較好)(double linked 雙向連結)
- Queue <<interface>> 先進先出的資料結構(First In First Out)
- Deque <<interface>>
- LinkedList
- Abstract Queue
- PriorityQueue (Non-Thread-safe) (會排序的Queue)
Notes:
- Collection.Sort 可以幫List 排序
- Arrays.Sort 可以幫陣列排序
Lab:
ArrayListDemo.java
package
mod14;
import
java.util.*;
public
class ArrayListDemo {
public
static void main(String[] args) {
List
fruits = new ArrayList();
fruits.add("Lemon");
fruits.add("Watermelon");
fruits.add("Pineapple");
fruits.add("Cherry");
fruits.add("Strawbarry");
fruits.add("Pineapple");
fruits.add("Cherry");
System.out.println(fruits);
System.out.print("----------------------------");
//Traditional
Method only for List not work in Set (Because Set doesn't have index)
int
size = fruits.size();
for
(int i=0; i < size; i++){
String
fruit = (String)fruits.get(i);
System.out.println(fruit+",
length = "+fruit.length());
System.out.print("----------------------------");
}
}
}
Notes:
- 陣列利用迴圈來取出陣列中的元素, 但是Collecton 的Set 是沒有順序, 所以無法使用 for 迴圈來取出元素, 迴圈只能使用於List (有順序, 記住放進來的物件, 有索引值)
*Iterator
介面的繼承架構
- 目的在抓出集合物件內的所有資料
- 搭配 while 迴圈
- 單向拜訪
- ListIterator
- 可以雙向拜訪
*Iterator
的特性
- Iteration 指的是取出Collection內的每個元素之程序
- Set物件的Iterator是沒有
HashSetDemo.java
import
java.util.*;
public
class HashSetDemo {
public
static void main(String[] args) {
Set
fruits = new HashSet();
fruits.add("Lemon");
fruits.add("Watermelon");
fruits.add("Pineapple");
fruits.add("Cherry");
fruits.add("Strawbarry");
fruits.add("Pineapple");
fruits.add("Cherry");
System.out.println(fruits);
//Iterator
Method 在Set這邊也適用
Iterator
it = fruits.iterator();
while(
it.hasNext() ){
String
fruit2 = ( String ) it.next();
System.out.println(fruit2+",
length = "+fruit2.length());
}
}
}
ArrayListDemo.java
import
java.util.*;
public
class ArrayListDemo {
public
static void main(String[] args) {
List
fruits = new ArrayList();
fruits.add("Lemon");
fruits.add("Watermelon");
fruits.add("Pineapple");
fruits.add("Cherry");
fruits.add("Strawbarry");
fruits.add("Pineapple");
fruits.add("Cherry");
System.out.println(fruits);
System.out.print("----------------------------");
//Traditional
Method only for List not work in Set (Because Set doesn't have index)
//傳統的方式只能用於List
但是不能用於Set
int
size = fruits.size();
for
(int i=0; i < size; i++){
String
fruit = (String)fruits.get(i);
System.out.println(fruit+",
length = "+fruit.length());
System.out.print("----------------------------");
//Iterator
Method 兩個都可以套用 (List
與 Set)
Iterator
it = fruits.iterator();
//使用hasNext()
判斷有沒有下一筆
while(
it.hasNext() ){
String
fruit2 = ( String ) it.next();
System.out.println(fruit2+",
length = "+fruit2.length());
}
}
}
}
*泛型
(Generics)
- 提供編譯時期(compile-time)的型別安全檢查
- 省去不必要的轉型
陣列:
String[]
fruits = new String[5];
fruits[0]
= "Lemon";
fruits[1]
= "Watermelon";
fruits[2]
= new Date(); // 這個是不允許的
fruits[3]
= 100; //
這個是不允許的
//取出
String
s1 = fruits[0];
Collection:
ArrayList<String>
fruits = new ArrayList<String>();
fruits.add("Lemon");
fruits.add("Apple");
fruits.add(new
Date()); // 如果上面有使用泛型
<String>,
那這個就不允許
fruits.add(100);
// 如果上面有使用泛型
<String>,
那這個就不允許
//取出
String
s1 = (String) fruits.get(0); //
如果上面有使用泛型
<String>,
那就不需要轉型 (String)
Notes:
- 泛型規定只能放那個型別, 來節省轉型的作法
- 整個Collection體系都支援泛型
*for
each 的語法
- 簡化collection拜訪的程序
- 程式碼更簡潔更安全
- 適用於陣列
- 使用巢狀迴圈時更加簡單更安全
- 去除Iterator的缺點
Iterator
容易出錯
- Iterator變數在每個迴圈出現三次, 這增加程式碼出錯的可能性
星期二, 10月 23, 2012
OCPJP Day 10
20121022
Day 10
Lab:
interface 實作
Shape.java
package
lab10;
public
interface Shape {
//宣告
getArea()
以及
getPerimeter()
public
double getArea();
public
double getPerimeter();
}
Circle.java
public
class Circle implements Shape {
//宣告一個
半徑的 double
private
double radius;
//使用建構子來傳入半徑
public
Circle(double radius) {
this.radius
= radius;
}
//因為用interface,
所以所有的子類別都要實作方法,
改寫方法在netbeans可以按滑鼠右鍵
//
Mouse right click --> Insert Code --> Implement Method
@Override
public
double getArea() {
//PI
* radius * radius 改成自己要的方法
return
Math.PI*this.radius*this.radius;
}
@Override
public
double getPerimeter() {
//
PI * 2*radius
return
Math.PI*2*this.radius;
}
}
Rectangle.java
public
class Rectangle implements Shape {
//宣告長以及寬度
private
double width;
private
double height;
//建立一個建構子來傳入長度以及寬度
public
Rectangle(double width, double height) {
this.width
= width;
this.height
= height;
}
//因為用interface,
所以所有的子類別都要實作方法,
改寫方法在netbeans可以按滑鼠右鍵
//
Mouse right click --> Insert Code --> Implement Method
@Override
public
double getArea() {
//
面積等於height
* width
return
this.height * this.width;
}
@Override
public
double getPerimeter() {
//周長等於
(width
+ heigh) *2
return
(this.width+this.height)*2;
}
}
GeometricTool.java
public
class GeometricTool {
public
static void main(String args[]){
//新增兩個物件並傳入數值
Rectangle
r = new Rectangle(3,4);
Circle
c = new Circle(5);
GeometricTool
gt = new GeometricTool();
//呼叫
getShapeInfo
並傳回面積還有周長
gt.getShapeInfo(r);
gt.getShapeInfo(c);
}
//
public
void getShapeInfo(Shape s){
//這邊利用
getClass().getSimpleName()
取出名稱 Circle
或是 Rectangle
//如果使用
getClass().getName()
會出現 lab10.Circle
或是 lab10.Rectangle
//回傳面積以及周長
System.out.println(s.getClass().getSimpleName()+"Area
= "+s.getArea()+", Perimeter ="+s.getPerimeter());
}
}
Notes:
- Math.PI – 圓周率
*Chapter
11 static 與 enum
*static
語法
- static 是個修飾子(modifier)
- 用於類別中的屬性, 方法或是巢狀類別(Nested class)
- static成員又被稱為類別成員(Class member), 像是類別屬性(Class attribute) 或是類別方法(Class method)
- 宣告為static的屬性與方法, 不在專屬某實體物件(instance), 而是屬於類別本身(當類別載入到記憶體的時候就已經存在了)
*static
屬性
- 又稱為類別屬性
- 用來呈現使用同一類別所建立的物件之間的共享資料(像是全域變數 global variable)
Notes:
- 類別圖上面, 底線代表 static
- static 要考慮存取控制
*static
方法
- static 方法不再具有this 的隱含變數
- static 方法, 不可以直接存取實體變數(instance attributes)
*static
initializer (block)
- 是個特殊的方法,只在類別載入時執行, 而且只執行一次
- 用來初始化static屬性
- 執行一些應用程式的前置初始化作業
- 在一個類別裏面, static block 沒有數量的限制, 執行的先後順序, 按照出現在類別裏面的先後順序
Account.java
package
mod11;
public
class Account {
//
private
static double interestRate;
static{
//
System.out.print("Static
initialzer");
}
public
static double getInterestRate() {
return
interestRate;
}
public
static void setInterestRate(double aInterestRate) {
//
use aInterestRate not InterestRate
interestRate
= aInterestRate;
}
private
int balance;
//
public
int getBalance() {
return
balance;
}
public
void setBalance(int balance) {
this.balance
= balance;
}
public
static void main(String args[]){
//Test
static initialzer
System.out.println(Account.getInterestRate());
}
}
*static
import 語法
- Java SE5 新語法.
- 匯入某個類別的靜態成員
*final
語法
- final 是修飾子(modifier)
- 用於
- 類別
- 無法再衍生子類別
- 方法
- 不能被覆寫 override
- 變數
- 常數
*final
變數
- final 變數即為一個常數
- final 變數只能夠設定一次
- final 區域變數(區域常數)使用前必須在方法本體內先設定初始值
- static final 的屬性必須直接設定初始值或是透過 static initializer, 否則會編譯錯誤.
Employee2.java
public
class Employee2 {
//
private
final int MYCONST1 = 100; //Compile-Time
Constant
private
final int MYCONST2; //Blank-final instance
variable
//
must define MYCONST2 value, because Blank-final instance variable
public
Employee2(int a ){
MYCONST2
= a;
}
//每個建構子都要先定義
Blank
final instance variable
public
Employee2(){
this.MYCONST2=500;
}
}
Notes:
- final 容易搞混的狀況
//下面這行是將
d
這個變數設定為 final,
不能改變
final
MyDate d = new MyDate(22,10,2012);
d.setDay(20);
// 因為是 d
設定為 final
所以這個方式是可以的,
沒有說不能改參考的位置
d.setMonth(3);
// 因為是 d
設定為 final
所以這個方式是可以的,
沒有說不能改參考的位置
d
= new MyDate(25,12,2012); //這個不行因為
d
已經被 final
*System
類別
Notes:
- System.currentTimeMillies() 回傳執行當下的時間, 單位為毫秒
- 從 1970/1/1 00:00:00 開始計算
- 可以用 Date() 來顯示, 例如System.out.println(new Date(System.currentTimeMillis()));
*String
類別
- String物件是不可變的(immutable)
- String物件經運算後, 會產生新物件
- 運算API
- concat() 處理字串串接
- replace() 置換字串中某字元
- substring() 擷取字串片斷
- trim() 截斷字串頭尾空白
- toLowerCase() 轉換為小寫
- toUpperCase() 轉換為大寫
- String的比較
- equalsIgnoreCase() 比較兩個字串的內容, 且不分大小寫
*String
Pool
stringPool.java
public
class stringPool {
public
static void main(String args[]){
String
a = "Hello";
String
b = "Hello";
//
Java use String Pool 來存放資料,
所以兩個都存放在
String
Pool, 所以以下結果會是true
System.out.println(
a == b);
//使用
new
的方式就不會存放到
String
Pool, 而是放在記憶體
String
c = new String("Hello");
String
d = new String("Hello");
//所以使用 c
== d 會比較兩個記憶體位置,
答案會是 false
System.out.println(
c == d );
//所以要比較字串的內容要使用 equals()
來比較
System.err.println(
c.equals(d));
}
}
*StringBuffer
與 StringBuilder
- StringBuffer物件內容是可改變的
- StringBuffer 是Thread-safe
- StringBuilder 是Java SE 5.0 的新類別
- StringBuilder API 與 StringBuffer相同
- StringBuilder 不是Thread-safe
訂閱:
文章 (Atom)

