一个关于this和finalize的问题

lhbyjx 2007-10-20 05:06:08
最近看Thing In Java第四版 有两道题不会 请大家帮忙解答下
第一个题目:
编写具有两个(重载)构造器的类,并在第一个构造器中通过this调用第二个构造器。
我有点想不明白 this表示的是 调用方法的那个对象的引用 既然是对象的引用 一个对象只能调用一个构造器 为什么能去调用第二个?
(小弟对this一直不太理解,希望大家给我解释下)
第二个问题

class Book{
Book(){
System.out.println("Book()");
}
protected void finalize(){
System.out.println("Book()");
}
}
public class P510 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
Book b=new Book();
System.gc();
}

}
这个finalize()为什么不会执行?
System.gc()不是会强行执行终结动作吗?
怎么让finalize()可以执行?
...全文
166 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
lhbyjx 2007-10-20
  • 打赏
  • 举报
回复
第一个问题明白了
谁能解决下第二个问题啊
johnsoncr 2007-10-20
  • 打赏
  • 举报
回复
你这个程序有几个问题:
1,"this.Gouzao(i); "首先,i没有定义,也没有初始化。
然后, 调用构造器的方法是:this()而不是this.constructo()
应改成this(2);
2,“Gouzao(){
System.out.println("Gouzao()");
this.Gouzao(i);
}”在一个构造器中调用其它构造器,this必须是第一句,所以应改成:
Gouzao(){
this(2);
System.out.println("Gouzao()");
}
rossi789 2007-10-20
  • 打赏
  • 举报
回复
class Gouzao {
Gouzao() {
this(6); //this语句必须放在构造方法第一行
System.out.println("Gouzao()");
}

Gouzao(int i) {
System.out.println("Gouzao(int i)");
}

void f() {
System.out.println("123");
}
}

public class P59 {

public static void main(String[] args) {
// TODO 自动生成方法存根
new Gouzao();
}

}
yuyu622 2007-10-20
  • 打赏
  • 举报
回复
//: Flower.java
// Calling constructors with "this"

public class Flower {
private int petalCount = 0;
private String s = new String("null");
Flower(int petals) {
petalCount = petals;
System.out.println(
"Constructor w/ int arg only, petalCount= "
+ petalCount);
}
Flower(String ss) {
System.out.println(
"Constructor w/ String arg only, s=" + ss);
s = ss;
}
Flower(String s, int petals) {
this(petals);
//! this(s); // Can't call two!
this.s = s; // Another use of "this"
System.out.println("String & int args");
}
Flower() {
this("hi", 47);
System.out.println(
"default constructor (no args)");
}
void print() {
//! this(11); // Not inside non-constructor!
System.out.println(
"petalCount = " + petalCount + " s = "+ s);
}
public static void main(String[] args) {
Flower x = new Flower();
x.print();
}
} ///
lhbyjx 2007-10-20
  • 打赏
  • 举报
回复

class Gouzao{
Gouzao(){
System.out.println("Gouzao()");
this.Gouzao(i);
}
Gouzao(int i){
System.out.println("Gouzao(int i)");
}
void f(){System.out.println("123");}
}
public class P59 {


public static void main(String[] args) {
// TODO 自动生成方法存根
new Gouzao();
}

}
这个程序 帮我看下怎么调用?
johnsoncr 2007-10-20
  • 打赏
  • 举报
回复
我们可以先把构造器当做一个名字跟类名相同的方法,你在一个方法中(构造器)调用其它的方法(构造器),这应该很普遍吧?虽然可以这样理解,但是构造器又跟普通的方法不同,因为构造器,只能通过构造器来调用,且在一个构造器里面只能调用其它的构造器一次。
lhbyjx 2007-10-20
  • 打赏
  • 举报
回复
PS:有什么方法可以让finalize()肯定执行嘛?
还有2楼的回答我还是不明白
一个对象是不是只能调用有一个构造器?
如果是这样那你在第一个构造器中用this调用第二个构造器是不是就相当于 你用调用第一个构造器的对象去调用了第二个构造器 我觉得这样是错误的啊
johnsoncr 2007-10-20
  • 打赏
  • 举报
回复
to question 1:
-->"一个对象只能调用一个构造器",应该是说只能用this来调用一个构造器,而不能调用两个!
“并在第一个构造器中通过this调用第二个构造器。”这里对于第一个构造器来说,是不是只用this来调用一次第二个构造器,所以当然可以!如果在第一个构造器内用this来调用第二个构造器一次以上,那就不行!
rossi789 2007-10-20
  • 打赏
  • 举报
回复
public final class Test {
public Test() {
this("haha");
}

public Test(String... name) {
System.out.println(name[0]);
}

public static void main(String... args) {
new Test();
}
}

调用了gc()后虚拟机有时候也不一定会马上启动垃圾回收的,这个要看操作系统和CPU脸色的

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧