标题:构建器用不好,看下,说出结果并解释下!
取消只看楼主
嶄ぎ儱
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2007-4-21
 问题点数:0 回复次数:1 
构建器用不好,看下,说出结果并解释下!

class Tag
{
Tag(int marker)
{
System.out.println("Tag(" + marker + ")");
}
}

class Card
{
Tag t1 = new Tag(1);
Card()
{
System.out.println("Card()");
Tag t3 = new Tag(33);
}
Tag t2 = new Tag(2);
void f()
{
System.out.println("f()");
}
Tag t3 = new Tag(3);
}

public class Think
{
public static void main(String[] args)
{
Card t = new Card();
t.f();
}
}

搜索更多相关主题的帖子: 器用 构建 结果 解释 
2007-05-04 10:17
嶄ぎ儱
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2007-4-21
得分:0 

class Bowl {
Bowl(int marker) {
System.out.println("Bowl(" + marker + ")");
}
void f(int marker) {
System.out.println("f(" + marker + ")");
}
}

class Table {
static Bowl b1 = new Bowl(1);
Table() {
System.out.println("Table()");
b2.f(1);
}
void f2(int marker) {
System.out.println("f2(" + marker + ")");
}
static Bowl b2 = new Bowl(2);
}

class Cupboard {
Bowl b3 = new Bowl(3);
static Bowl b4 = new Bowl(4);
Cupboard() {
System.out.println("Cupboard()");
b4.f(2);
}
void f3(int marker) {
System.out.println("f3(" + marker + ")");
}
static Bowl b5 = new Bowl(5);
}

public class StaticInitialization {
public static void main(String[] args) {
System.out.println(
"Creating new Cupboard() in main");
new Cupboard();
System.out.println(
"Creating new Cupboard() in main");
new Cupboard();
t2.f2(1);
t3.f3(1);
}
static Table t2 = new Table();
static Cupboard t3 = new Cupboard();
}
那为什么这里加上Static 输出的结果又不一样呢?


别人不给你机会,自己去创造机会.
2007-05-04 12:19



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-136830-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.150354 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved