Re: [問題] 建構子 (新手)
新手來回答新手的問題...
先不要問"為什麼要這麼用"(主要在於你要用什麼樣的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
推
02/11 12:13, , 1F
02/11 12:13, 1F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 3 之 4 篇):