作者cyberwizard (Gavin)
看板java
标题Re: [问题] 建构子问题
时间Wed Mar 14 18:01:00 2012
public class test {
static int c;
public static void main(String[] args) {
int a = 1;
int b = 2;
//new test(a, b);
System.out.printf("两数字相加 %d, %d 答案为 %d %n ", a, b, c);
}
public test(int a, int b){
c =a + b;
}
}
去掉不必要得部份,这样执行会得到
两数字相加 1, 2 答案为 0
把注解去掉,会得到
两数字相加 1, 2 答案为 3
虽然物件建构子当作方法用很怪,但是功能还是正常的
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.123.85.140
1F:推 jodoken:建构值当方法用很奇怪吗? 03/14 18:34
2F:推 LaPass:非常奇怪.... 03/14 19:28
3F:推 mars90226:建构子就是在你需要那个物件时才呼叫阿~ 03/14 22:15