2008年11月10日 星期一

heap 跟 stack

這東西以前可能有學過吧,不過整個忘掉了,重新復習ㄧ下

在用ClassT object的時候
是將資料放在stack上

而用ClassT* object = new ClassT()的時候
則是將資料放在heap上

stack的好處是不用清理,離開function後自動清掉
不過會怕overflow
也可以使用alloca來配置記憶體

heap則是動態的利用malloc/free或是new/delete來處理

2008年11月6日 星期四

命令模式

將request命令的物件與執行命令的物件之間鬆綁
而一個命令物件封裝了一個或一組東做
命令還可以被復原,就是實踐一個undo(),用來逆向execute()做的事情

2008年11月3日 星期一

工廠模式(factory)

簡單工廠:
不是設計模式
只是一個常用的方法
將實體化的程式移到另一個factory class

工廠方法:
將實體化留到繼承的子類別來決定
(一個抽象的方法來作實體化 , 子類別要實行這個方法)
這邊的決定不是指runtime時期決定
而是指選用不同子類別時,就等於選用不同實體
不同的實體化可以經由繼承後覆寫不同的實體化方法來進行


抽象工廠:


抽象工廠訂定一個介面,所有實體工廠都得實踐這介面

獨體模式(Singleton)

在一些只需要一個實體的場合

將建構子設為private
且用另一個static function來call 建構子
並將pointer記錄在一個static的指標

另外由於因應multi thread
有三種解決方法

1. 同步化 getInstance()

2.改成率先實體化()

3. 雙重檢查上鎖() (block synchronized)

2008年10月28日 星期二

VC 2008 設定


Project > properties > configuration properties > C/C++
General裡面有可以設定include,如果有別的.h檔要包,可以在這邊設
Precompile header 可以設定取消 Precompile header

Project > properties > configuration properties > Linker
Input 可以設定.lib , 不管使用static或dynamic的時候都要在這邊設

Tool > option > Projects and Solutions >VC++ Directories
可以設定VC2008的預設Include和Library路徑

2008年10月24日 星期五

裝飾者模式(decorator)

比較類似包裝,一層層包裝起來
被包裝者與包裝者都繼承一個interface或抽象類別
被包裝的物件因此可以直接當原本的物件來用

包裝是用合成而得來的,不是用繼承
這裡用的繼承只是讓他們有同個father

2008年10月23日 星期四

觀察者模式(Observer)

一個一對多的關係
當資料在主題這邊有改變的時候,便會通知各個有訂閱的觀察者
而主題跟觀察者的關係是用interface連接起來
所以觀察者只要implement 這個interface就好
而在runtime的時候,可以加入訂閱新的觀察者
也可以取消訂閱現有觀察者,主題完全不會受到影響
由於鬆綁(Loose Coupling)的關係
主題不需要知道觀察者implement的細節

策略模式(strategy)

基礎且實用的一個Pattern
將以後可能會改變的部份分離封裝起來
並利用interface制定相連接的規格
之後每次改變只要去implement這個interface就可以了
而不同的implement也可以在runtime的時候作切換

oo守則: 多用合成,少用繼承
因為合成比起繼承來說,有更大的彈性

oo守則: 針對interface寫程式,而不是針對implement寫程式

2008年10月15日 星期三

custom tag



Classic custom Tag

有三個Inteface : Tag, IterationTag,BodyTag

在Tag中function被呼叫的順序
setPageContext()
setParent()
setter methods 有attribute的話
doStart() 作initial,且用EVAL_BODY跟SKIP_BODY決定是否作body
doEndTag() cleanupm且用EVAL_PAGE跟SKIP_PAGE決定是否作剩下的JSP
release() 全部結束,不再用到才會呼叫

如果有attribute:
要declare variable
要設定是不是mandatory,要有default value,要有setter

IterationTag
加入doAfterBody() EVAL_BODY_AGAIN跟SKIP_BODY

BodyTag
本來body內的東西只能選擇作或不做,BodyTag可以修改它
另外新增setBodyContent(), doInitBody(),EVAL_BODY_BUFFERED

2008年10月14日 星期二

C++ 程式最佳化技巧


unsigned類型用於:
  除法和餘數
  循環計數
  數組下標
signed類型用於:
  整數到浮點的轉化

for (;;) 比 while(1)來的好

避免不必要的整數除法

++i比i++好

local function盡量設為static






2008年10月2日 星期四

JSTL

 對presentation logic也可以reuse

Tag handler分為兩種: classic tag interface & Simple tag interface

使用tag library需要URI
URI有三種:
absolute http://xx.xx:8080/xx
root relative /xx/xx
Non-root relative xx/xx

如果是用JSP syntax:
<%@ taglib prefix="test" url="sampleLib.tld"%>

如果是用XML syntax:
<jsp:root
xmlns:jsp="http://..........."
xmlns:test="sampleLib.tld"
version="2.0">
JSP PAGE.....
< /jsp:root>

由於要避免一旦改了tld檔,每個jsp的路徑都得改
所以可以在web.xml設定

如果tld檔被放在JAR檔裡面的話,要取名為taglib.tld

taglib map
分為explicit mapping,implicit mapping,well-know mapping
explicit lib:
在web.xml裡面宣告
<taglib>
<taglib-uri>
http://xx.xx/studykit
</taglib-uri>
<taglib-location>
/myLibs/studyKit.tld 或 yourLibs/sample.jar
</taglib-location>

</taglib>
< >

mapping 次序

如果有找到對應的mapping
root relative就從application找起
non-root relative就從application/WEB-INF找起
如果在web.xml中沒找到對應的mapping
absolute就產生error
root relative就從application找起
non-root relative就從jsp檔案的地方找起

tag分為四種
Empty tag
tag with attributes
tag with JSP code (body)
tag with nested tag (switch & case)


JSP Standard Tag Library(JSTL)
下面有core,xml,fmt,sql,functions等library
這邊只介紹core
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>

catch:
一般exception是送到error page,如果tag之間的code發生exception,會傳到var裡定義的variable
out:
有點像expression
set:
EL不能從各種scope中set跟remove attribute
所以可以用這個action來代替
可以使用
<c:set var="num" value="${4x4}"/>

<c:set var="num">
${4x4}
</c:set>
而用在Map或JavaBean的時候
<c:set target="customer1" property="zipcode">
44124
</c:set>

<c:set target="customer1" property="zipcode" value="44124"/>
remove:
<c:remove var="num" scope="session"/>
如果沒設scope,預設page->request->session->application
if:
用test來設定true和false,沒有 else
choose:
裡面包when跟otherwise,跟switch-case一樣
forEach:
兩種用法
第一種
用begin end step來做for迴圈
第二種
類似for (int element : collection)
只是變成
<c:forEach var="element" items="${collection}">
<c:set var="element" value="100"/>
</c:forEach>
forToken:
類似forEach,不過是用在String上,用delims隔開
參數有var,items,delims

url,import,redirect:
可以夾param在tag之間(c:param name=xx value=xx)

tld檔

taglib
tlib
jsp-version
short-name
uri 可以implicit設定uri,在JAR檔的狀況會自動產生mapping
tag 可以多個,也可以不同tag用同個class
name
tag-class
body-content
description
attribute
name
required 是否mandatory
rtexprvalue 是否可以用requestion time expression

body-content有三種
empty 中間不可以有東西,不過可以<></>
JSP 可以有text,html,script,action,或別的custom tag
tagdependent 用別的語言

JavaBean


JavaBean的條件
1.有個public且沒有argument的constructor
2.每個變數都有public getXXX和setXXX的function

如果有implement java.io.Serializable,就可以將JavaBean給serialize
副檔名會用.ser, 檔案會被放在/WEB-INF或其子目錄下
然後可以用java.beans.Beans.instantiate()來實例化

jsp:useBean
id 例如address
scope (預設是page scope)
class 例如BusinessAddress
type 例如AddressBean (可用父class或inteface)
beanName 例如businessData.John 或 AddressBean
搭配組合:
class
type
class 和 type
beanName 和 type
(beanName可以餵request time expression,class不行)

在用beanName的時候
還是會先check 設定的scope裡面有沒有要的bean(所以才需要type)
沒有的話,才去檔案裡找

只用type的話
如果依據id找到的bean不合type,會丟出ClassCastException
如果找不到,會丟出java.lang.InstantiaionException

用jsp:useBean包起來的指令,可用來作initial
這些指令只有在scope 裡找不到bean的時候,才會執行

jsp:setProperty
name 同useBean的id
property property名稱
value 手動assign
param 從request得到

value和param不能同時有
都沒有的話代表用跟property同名的param
此時也可以將property設為* , 則會從request中設所有的property
如果request中找不到,則就沒事發生

jsp:getProperty
只有name跟property
jsp:getProperty只能用來print
如果要用在程式中或request time expression
只能透過Java的function來呼叫,也就是getXXX()

由於request傳遞的parameter只能是String
所以如果JavaBean裡面不是String的話,jsp engine會自動作轉換
除此之外,一般的狀況也會作自動轉換
但對於request time expression, javaBean就不提供自動轉換的功能

而JavaBean也可以接受array
一般request裡面的parameter有多個value的時候
Javabean若是一般variable,便會只接受第一個
要是是Array,便會全部接受
(String to Char的轉換是只取String的第一個字母)
而將array get出來則只會print出array的位址
(這時就得用Java的function來呼叫)

2008年10月1日 星期三

EL in JSP

EL是用 ${vara} 來print出vara
可是EL不能declare variable
只能用現有的implicit varialbe

要用

EL的implicit variable:
pageContext
pageScope
requestScope
sessionScope
applicationScope
param
paramValues
header
headerValues
cookies
除了pagecontext以外,都是Map
它並沒有給直接的access到HttpSession等...

EL operators
${header["host"]} 跟
${header['host']} 跟
${header.host} 是一樣的

tag library descriptor
要用function的話,要用tld
裡面包括
function
name
function-class
function-signature

然後在JSP裡面用directive的taglib prefix="myString" uri="URL....."
便可以用${myString:functionName()}來呼叫

2008年9月28日 星期日

JSP


JSP有兩種Syntax: JSP page與JSP document
這兩種不能寫在同一個檔案

JSP page: Standard JSP syntax format
Directive <%@ %>
page
import,session,errorPage,isErrorPage等....
include
static的include
taglib
Declaration <%! %>
只執行一次,可在此複寫jspInit()跟jspDestory()
Scriptlet <% %>
一般code
Expression <%= %>
後面不加分號
Action <jsp:actionName />
tag lib也這樣call
Comment <%-- --%>

JSP document: XML syntax format
整個code要用
<jsp:root xmlns:jsp="http://......"
xmlns:myLib="http://......" //這個不一定要
version="....">
</jsp:root>
包起來

原本的三個directives 只剩兩個page與include
<jsp:directive.page ..... />
<jsp:directive.include ..... />
因為taglib在root中宣告

而其他指令也變成
<jsp:declarative> Code </jsp:declarative>
<jsp:sciptlet> Code </jsp:sciptlet>
<jsp:expression> Code </jsp:expression>
request time attribute expression則是 %= ...%
例如 <jsp:include page="%=pageURL%">

另外在XML格式中 static文字輸出要指令(http tag不用)
<html><body>
<jsp:text> 文字HERE </jsp:text>
</body></html>

JSP implicit variable
application
session
request
response
out
page
pageContext
config
exception
p.s. 這些implicit variable只能在_jspService()中使用
也就是只能在sciptlets或expressions中,不能在declaration中

原本jsp檔是不需要deployment的,可以直接用.jsp來access
若要使用deployment來使用一些initial parameter
就會得使用/servlet/servletName來access

除了request、session、application三個scope以外
JSP還提供了page這個scope(pageContext這個variable)
並提供一些方便的function,不用透過request等三個scope variable來取用parameter
如 void setAttribute(String name,Object object,int scope);
其中scope為 APPLICATION_SCOPE SESSION_SCOPE......等

JSP include

分為兩種, static include 與dynamic include
static include使用directive的指令,在translate time就include好
也就是<%@ include file="xxx.jsp" %> 或 < jsp:directive.include file="xxx.jsp">
這種只能include HTML, XML , JSP, 而不能include servlet
而dynamic的include就等同於request用getRequestDispatcher的forward跟include
或用pageContext.include 跟 pageContext.forward
指令為<jsp:include page="xxx.jsp" flush="true"/>
forward則沒有flush , flush不使用的話,預設為false
dynamic的include與forward則可以對servlet 來include
在<jsp:include page="xxx.jsp"> </jsp:include>裡面可以包
<jsp:param name="xxx" value="xxx"/>
來從request中加入parameter,如果原本已經有這個parameter
會將新的插在前面,後面的要用getParameterValues才出的來
另外如果新插入的也有重複,順序是跟呼叫的順序一樣
例如request傳A , include加入b,c , 結果印出來會是 b,c,a
注意!! static的是用file , dynamic是用page
< >

2008年9月27日 星期六

Secure Web Application

Key Word:
Authentication: 證實,證明(確定user的身分)
Authorization: 授權,批准(確定user的權限)
Data integrity: 防止資料在傳輸中被變更,用hash code
Confidential: 資料保密
Auditing : 紀錄log

Authentication 方法
HTTP Basic 最一般,沒加密
HTTP Digest 有加密,可是只有ie可用
HTTPS Client 用SSL加密,但需要certification
HTTP FORM-based FORM版的Basic

user的name,password,role在conf\tomcat-users.xml中定義

而authentication的方法在web.xml中定義
<login-config>
<auth-method> BASIC or FORM ... </auth-method>

如果是BASIC
<realm-name> sales </realm-name>

如果是FORM
<form-login-config>
<form-login-page> /formlogin.html </form-login-page>
<form-error-page> /formerror.html </form-error-page>
</form-login-config>

</login-config>


Declarative Security方法

<security-constraint>
<web-resource-collection>
<web-resource-name>
<url-pattern> /servlet/SaleReportServlet/* </url-pattern>
<url-pattern> /servlet/HttpReportServlet/* </url-pattern>
<http0-method> POST </http-method>

</web-resource-name>

</web-resource-collection>

待續

</security-constraint>

2008年9月5日 星期五

J2EE

JSP中
想要用


想要用custom tag library的話
熟Java就用SimpleTag
如果需要用到script,就得用classicTag
如果不想用Java,要用JSP,就用Tag file

2008年8月21日 星期四

C下MultiThread的function

int _tmain(int argc, _TCHAR* argv[])
{
// Step 1: 建立 thread 並且暫停
HANDLE hThread=(HANDLE) _beginthread(myThreadFun,0,NULL);
SuspendThread(hThread);

// Step 2: 設定thread 的 priority level
SetThreadPriority(hThread,THREAD_PRIORITY_IDLE);

// Step 3: 重新啟動 thread
ResumeThread(hThread);

// 執行工作
for(int i=0;i<1000;i++)
cout << i << " primary thread" << endl;

WaitForSingleObject(hThread,INFINITE);
return 0;
}

2008年8月13日 星期三

JAVA util套件

Pattern & Matcher
正規表示法
. * [ABC] [A-C]
/d /D /s /S 這些在用的時候要多加個/
也就是//d //D ......

Pattern ptn = Pattern.compile("JAVA [EMS]{2}.*");
Matcher mch = ptn.matcher("JAVA SE 5.0");
matches()可以return是否有完整match
find()可以return是否字串中有符合的片段
有的話可以用start()end()來return起始位置與結束位置+1
replaceAll(String str)來把裡面所有符合表示法的片段換為str
並return結果字串

而不必搭配Matcher的split()
可以利用String[] a = ptn.split(str)
來將str這個字串,用正規表示法指出的片段來分割

Formatter
Formatter f = new Formatter(System.out)可以用來獲得標準輸出
而f.format("")可以用來輸出
若constructor內餵StringBuffer則可以輸出到StringBuffer
這情況下f.format()此時會變為append的方法來加入字串
若constructor內餵File("xxx.xxx")
則會輸出到這個檔案 (這個狀況下要用try-catch)
且要用close()
%b boolean %s String %h 16進位hashcode
%c Character %d Integer %o 8進位Integer %x 16進位Integer
%e 浮點數的指數表示法 %f浮點數一般表示法 %g 依精確度格式化的浮點數
%a 16進位指數表示法的浮點數

Scanner
Scanner sc = new Scanner(System.in);
可以用來獲得鍵盤輸入
sc.next() 可以獲得字串輸入
sc.nextInt()可以獲得Integer輸入

JAVA Generics(泛型)

對於多個不同的class
若基本的邏輯相同,可利用泛型,只定義一個class


public class GenericFoo <T> {
private T foo;
public void setFoo(T foo) {this.foo = foo; }
public T getFoo() {return foo;}
}

也能用public class ListGenericFoo <T extends List>
來限制T只能是number的子類別

然而<?>與<? extends number>則是用在變數或參數的宣告
限制這變數只能裝載這些限定的型別
*不過使用?宣告的reference不能用來加入新的資訊
只能取得它的資訊或是移除它的資訊

Java Exception

Runtime Exception不需要用try catch包起來

override父類別的method時,原method若有throws
新的method可以throws原exception範圍內的exception
或不throws

2008年7月28日 星期一

JAVA import & package

import最後面一欄要到Class
或上面的Package.*
import static最後一欄則是到static的member

如果不import就要用class
每次用這個class就得packagename.classname

要把package P; 的class檔案compile 出來
要java -d . XX.java
則XX.class會產生到目前資料夾下的P資料夾

執行package為utility.myapp中的Cool.class (放在C:\java\Myclasses\utility\myapp中)
指令 -> java -classpath C:\java\Myclasses utility.myapp.Cool

設定classpath 時 ,舊的會被蓋掉?

2008年7月20日 星期日

JAVA I/O

File:
檔案的一些操作
判斷path是檔案還是資料夾
判斷存不存在

InputStream & OutputStream:
用來輸入輸出byte steam
分別有read()與write()來處理資料,得放在try catch中
int read()會return read到的值
而int read(byte[])則會把結果塞到byte[]中,並return read了幾個byte

Reader & Writer
用來輸入輸出character stream

FileInputStream & FileOutputStream
用來開啟檔案
available()可以用來看有幾個byte可以讀
FileOutputStream constructor的第二個引數是true則為append模式

FileReader & FileWriter
用來開啟檔案

BufferedInputStream & BufferOutputStream
用Buffer緩衝操作次數
flush()

BufferReader & BufferWriter
用Buffer緩衝操作次數
newline()
readline()

DataInputStream & DataOutputStream
有readInt等一堆針對不同type 的存取function

InputStreamReader & OutputStreamWriter
byte與char間的轉換
OutputStreamWriter outWriter = new OutputStreamWriter(new FileOutputStream ("a.txt"),"ISO2022CN");

PrintStream:
System.in是一個InputStream,不可單獨使用
System.out是一個PrintStream,可單獨使用

ObjectOutputStram & ObjectInputStream
要存的物件要implement Serializable
要有private static final long serialVersionUID=1;
trasient修飾的varable不會存起來

2008年7月17日 星期四

JAVA autoboxing & warpper class

若Integer的值在-128~127之間
物件會重覆使用,不過用new的話,還是會產生新的物件

覆載時autoboxing的順序
1. 允許向上轉型(int會找long、float或double)
2. 用autoboxing
3. 找varags自動變長參數

因為int 可以轉為 double
可是Integer不能轉為Double
所以Integer可以轉為double (Integer轉int再轉double)
但int不能轉為Double (int 轉Integer不能再轉Double)

字串轉數字
int x = Integer.parseInt("123");

數字轉字串
String s = String.valueOf(1);

Integer type不會自動initial (因為是物件)

泛型之間是互不相容,不用看繼承關係
反而泛型跟沒泛型的可以相容

2008年7月12日 星期六

JAVA collection

Collection、Map、Set、SortedSet、 List、SortedMap皆為interface
其他的才是class

Collection:
collection下有list與set
Set只有unique的元素
List則可以重複
Collection內存的是Object,讀出來前要先轉型,所以才有generic這種東西

Map:
而Map則是有Set的Key與Collection的value的組合

Set:
有HashSet class
而HashSet下還有LinkedHashSet class,會照add順序排列
HashSet在加入物件時
會先檢查物件的hashCode()是否跟HashSet中的元素有一樣
若一樣,再檢查是否reference到同個物件
若不是,再檢查equals()是否為true

List:
List是有Ordered的,可允許重複元素,有index
下有ArrayList、LinkedList、Vector
ArrayList跟Vector都是Random Access的,差別Vector為thread safe版
LinkedList則是Insert快,是double linked list
List要用contain的話要覆寫equals()

Map:
有key與value,key是unique的,所以是set
Map下有HashMap、LinkedHashMap、HashTable
HashTable是thread safe版的Hash Map,且不允許null Key

equals() :
通常寫法:
1.用==來判斷是否指到同個物件
2.判斷是否同個class
3.把obj轉型為這個class
4.用成員函數是否相等來判斷
Reflexive: x.equals(x) is always true.
Symmetric: F x.equals(y) <-> y.equals(x)
Transitive: if both x.equals(y) and y.equals(z) are true, then x.equals(z) is true.
Consistent: 不管算幾次都是一樣結果
null comparison: For any non-null reference obj, obj.equals(null) is always false.




hashCode()
有覆寫equals()的話也請覆寫hashCode()
eauals()為true的話,hashCode()值一定要一樣
eauals()為false的話,hashCode()值不一定要不同
一般性的規定
Consistency during execution:
Object value equality implies hash value equality:
Object value inequality places no restrictions on the hash value:

SortedMap、SortedSet
只有TreeMap與TreeSet
Implementing Comparable interface的物件並覆寫 compareTo()才可以加進去
*若compareTo() return 0的話,add就沒有作用

PriorityQueue預設自然排序,poll會從head來poll

Collection裡面只能放物件,所以它的generics也只能放物件

HashSet s的元素若要sort,可以把s丟進TreeSet的constructor
s= new TreeSet(s);

JAVA enum

內部enum才可以宣告為static(implict final)
*enum的constructor為private
enum的內容值為public static final

enum的內容其實是String

values()這個method可以return一個element array

JAVA 繼承及物件導向

在Father a = new Son()時
a.varable會call Father的
a.method會call Son的
而如果call static method,則call Father的
而由於a名義上是Father,如果a的method有throws的話
call a.method還是需要用try包起來
可是實際上run time才是去執行Son的method

implement 兩個interface時的語法 implement A, B

interface可以多重extends,class不行

同時extends和implement時要先extends再implement

final method不能被override

JAVA 雜

系統變數
使用指令java -Dxxxx.xxxx = yyyyy
可把xxxx.xxxx這個系統變數加進去,其值為yyyyy
可用System.getProperty("xxxx.xxxx")
或System.getProperties().getProperty("xxxx.xxxx")
來取值

println一個object其實是call它的toString()

DateFormate:
Date date = new Date()用來產生Date物件
Date(int a)則可以產生自1970年過了a微秒後的Date物件
DateFormat shortFormat =
DateFormat.getDateTimeInstance(
DateFormat.SHORT, DateFormat.SHORT);
System.out.println(shortFormat.format(date));
DateFormat則用來將date轉為文字
也可用DateFormat的parse()將String轉為Date
DateFormat.getDateInstance(DateFormat.LONG, Locale.TAIWAN).parse("2005年7月22日");

String、StringBuffer、 StringBuilder
String是immutable的
沒有用new來allocate的話,兩個相同內容的字串會因為有維護string pool而指到同一個object
不過"a"+"b"則不會等於"ab"
*有+ 和 concat來合併字串,不過要記得assign給新的ref
String長度用length()來查 容積是capacity()
P.S. array長度則是用length來查
collection的大小是size() 容積是capacity()
StringBuilder用append來合併字串
還提供String substring()來傳回子字串
*因為是return String,所以也一定要有ref接
StringBuffer是thread-safe版的StringBuilder

Garbege collection
建議VM進行GC的方法
System.gc()
Runtime.getRuntime().gc()

邏輯運算有分short circuit(|| &&) 與一般的版本(| & )
short circuit先做前面,若已能確定,就不會再做後面
一般的版本則會依序做前面後面

*Java的Function call是call by value
傳進去後的改變並不會影響Function外
所以傳ref進去再new會有問題

final的變數一定要手動給定初始值,之後就不能再改了

2008年7月1日 星期二

JAVA thread

Daemon threads 在main thread結束時,也會跟著結束

method synchronized 只對 non-static member有用

static yield: 讓出cpu(可是面對不同priority,可能會沒用)

static sleep (時間) throws Interrupted Exception
有block,所以要用try & catch包

final join (時間) throws Interrupted Exception
有block,所以要用try & catch包
直到目標Thread跑完,才會解除,但目標Thread還是可以把CPU讓給第三者

wait() & notify()則是屬於物件的function,不是Thread的
wait()會交出lock,可是notify()不會,要等Thread結束才會交出

volatide: 在multithread時,確定多個Thread會access到值本身而不是copy

synchronized可以修飾 method
或用synchronized(obj){}來包住區段

final變數不需要考慮同步的問題

2008年6月28日 星期六

JAVA inner class

static inner class只能存取enclosing它class內的static成員
static inner class只能放在top level class或 其他static inner class
static inner class裡面可以有static成員及non-static成員

non-static inner class裡面除了final static以外,不能有static成員
non-static inner class可以有任何的accessibility modifier


local inner class 只能存取 包含它的Method內的final變數

如果包含local inner class的context或method是static,則class也是隱含static

local inner class的reference不能在context外面declare,只能用它的superclass來接在context裡面new出來的instance

anonymous inner class 裡只有non-static members 和 final static fields

2008年6月24日 星期二

JAVA interface

class可以不實作interface的所有方法,不過要標為abstract
interface的變數隱含 public, static and final且一定要initial值

*而interface的method隱含public
所以要override他一定要設為public

JAVA constructor & initial block

constructor沒有return value (連void都沒有)
有的話,就算是同名,也不是constructor

constructor可以為private或任何modifier
不過若為private,則在外面就不能用這個constructor

constructor裡面
this()或super()只能放在第一行
而且不能同時出現在同一個constructor

在有父類別的情況下
如果兩個都沒有,compiler會偷偷插入一個super()在最前面
這時如果父類別沒有default constructor就會錯誤!

constructor不能access static member?

initial block:

static的順序
照著放置先後順序

instance block順序
super() -> block 或express 照先後順序 -> 其他constructor

2008年6月18日 星期三

開始寫Java考古題

還真是寫的很挫敗啊
一堆觀念都還不清楚

2008年6月14日 星期六

Multi Thread、Synchronized

之前都沒有撰寫過MultiThread程式的經驗
Synchronized跟Deadlock這種大學OS該學會的問題當年也沒學好
工作後才開始珍惜唸書的感覺
該減少自己時間的浪費了...迎頭趕上!

JAVA

感覺好像都繼承現有的class或實作interface
這種繼承才有物件導向的感覺

到現在才在學OO, 實在是遜了點

2008年6月2日 星期一

C++讀寫binary檔案

用fread與fwrite來讀寫檔案
call一次function只能傳有限大小
必須把要讀寫的內容分段讀寫
所以要用個counter來記錄還需要讀寫多少資料
而pointer也需要一直累加

有點像寫socket程式一樣