看板 java 關於我們 聯絡資訊
新手來回答新手的問題... 先不要問"為什麼要這麼用"(主要在於你要用什麼樣的pattern), 有這種example: (當然, 還會有其他寫法) package test; public class A { private String output; private A(){}; public static A createA() throws Exception{ A a = (A)Class.forName("test.A").newInstance(); a.setOutput("output"); return a; } public String getOutput() { return output; } public void setOutput(String output) { this.output = output; } } public class Test { public static void main(String[] args) throws Exception { A a = A.createA(); System.out.println(a.getOutput()); } } -------------------------------------- 這執行後就會印出"output"字樣... 參考看看囉... ※ 引述《CKPILOT (新年快樂!)》之銘言: : 不好意思 : 請問各位前輩 : 如果建構子一定要設成private : 那我該用什麼方式去建立這個物件呢? : 想不透@@" : 就已經是private了 : 怎麼去new這個物件@@" -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 60.251.172.154
CKPILOT:瞭解了^^ 非常清晰易懂,謝謝您的解惑:) 02/11 12:13