this的寿命?

38 篇文章 0 订阅
7 篇文章 0 订阅
[url=http://d.hatena.ne.jp/NyaRuRu/]NyaRuRuさん的blog[/url]真是一个宝库,有许多有启发性的日记。刚才读到这么一篇:[url=http://d.hatena.ne.jp/NyaRuRu/20060626/p4]this の寿命[/url],虽然是06年的老文,却还是让我惊讶了一番。

文中提到,“this”在实际运行中并不一定在其作用域内都存活;在作用域结束前,它有可能就已经被回收(delete this)了。文中举了这样一个例子(稍微修改过):
testGC.cs:
// compile this file with the following command:
// csc /o /d:TRACE testGC.cs

using System;
using System.Threading;
using System.Diagnostics;

class Test {
IntPtr _myHandle = IntPtr.Zero;

~Test( ) {
Trace.WriteLine( "Finalize; Release Handle" );
}

public void Foo( ) {
Trace.WriteLine( "Get Handle from OS : 123456" );
_myHandle = ( IntPtr ) 123456;
Bar( _myHandle );
}

public void Bar( IntPtr handle ) {
GC.Collect( );
Thread.Sleep( 1000 );
Trace.WriteLine( "Use Handle : " + handle.ToString( ) );
}
}

static class Program {
static void Main( string[ ] args ) {
Trace.Listeners.Add( new ConsoleTraceListener( ) );
new Test( ).Foo( );
}
}

运行的结果是:
[quote]Get Handle from OS : 123456
Finalize; Release Handle
Use Handle : 123456[/quote]
从trace结果可以看到,Test的析构器在Test.Foo()还在运行中就执行了。这与一般认识似乎相差甚远:Main()里对Test.Foo()的调用尚未完成,也就是说应该还有对这个对象的引用,所以这个Test对象应该暂时还无法被析构才对。
事实上,受到JIT的内联优化等影响,程序运行的时候还有没有对某个对象的引用,与源码中不一定总是对应的;极端情况下,在一个对象的构造器执行过程中,这个对象就可能已经被析构了。在[url=http://blogs.msdn.com/ricom/archive/2004/05/19/135332.aspx#139495]这帖[/url]有一串有趣的讨论。

Chris Brumme也写过一篇关于GC.KeepAlive()的帖子:[url=http://blogs.msdn.com/b/cbrumme/archive/2003/04/19/51365.aspx]Lifetime, GC.KeepAlive, handle recycling[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您编写一个简单的示例程序,体现鸟类继承关系。首先,我们定义一个父类 Bird,它包含鸟的一些基本属性和方法,比如翅膀、两条腿、飞行等: ```java public class Bird { protected int wings; // 翅膀数量 protected int legs; // 腿的数量 public void fly() { System.out.println("I'm flying!"); } } ``` 接下来,我们定义三个子类,分别是 Sparrow(麻雀)、Ostrich(鸵鸟)和 Eagle(鹰),它们继承了 Bird 父类的属性和方法,并且具有各自的特点: ```java public class Sparrow extends Bird { private int lifespan; // 寿命 private double weight; // 体重 public Sparrow(int lifespan, double weight) { this.lifespan = lifespan; this.weight = weight; wings = 2; legs = 2; } public void chirp() { System.out.println("Chirp chirp!"); } } public class Ostrich extends Bird { private double height; // 身高 private double speed; // 奔跑速度 public Ostrich(double height, double speed) { this.height = height; this.speed = speed; wings = 0; legs = 2; } public void run() { System.out.println("I'm running!"); } } public class Eagle extends Bird { private String prey; // 捕食对象 private int altitude; // 翔高度 public Eagle(String prey, int altitude) { this.prey = prey; this.altitude = altitude; wings = 2; legs = 2; } public void hunt() { System.out.println("I'm hunting " + prey + "!"); } } ``` 以上就是一个简单的示例程序,体现了鸟类的继承关系。在这个程序中,Bird 类是父类,Sparrow、Ostrich 和 Eagle 是它的子类,它们继承了 Bird 类的属性和方法,同时具有各自的特点。您可以根据需要进一步完善这个程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值