2005年一月月 发布的文章

深入Java底层

在一个朋友的书架上发现王森著的《Java深度历险》一书,看了书的前言了解该书是关于Java底层技术内幕的。怀着好奇心浏览了一下,谈不上有太多收获,但也记下了一些自认为有益的两点。

* Java xxx

我们在命令行下敲入:“java xxx”后会发生什么呢?

流程如下:
1.找到JRE;
2.找到JVM.dll;
3.启动JVM,并进行初始化;
4.产生Bootstrap Loader;
5.载入ExtClassLoader;(Ext – Extended)
6.载入AppClassLoader;
7.加载xxx类。

书中提到Bootstrap Loader、ExtClassLoader和AppClassLoader构成了Java的“类加载器继承体系–class loader hierarchy”,其中Bootstrap Loader是由C++编写的,其他两个是由Java写的。之所以成为“继承体系”是因为这三个loader之间是有联系的。Bootstrap Loader负责加载ExtClassLoader,后者ExtClassLoader就将其parent置为Bootstrap Loader。AppClassLoader较为特殊,虽然由Bootstrap载入,但是其parent却置为ExtClassLoader。其原因是为了实现“委托模型”。简述“委托模型”就是当类加载器有加载类的需求时,会先请求其parent使用其搜索路径帮助加载,如果其parent找不到,才使用自己的搜索路径进行加载。如上述所说当ExtClassLoader想载入AppClassLoader类时它首先请求其parent “Bootstrap Loader”帮忙,Bootstrap Loader将AppClassLoader载入后,由于这个载入是ExtClassLoader请求的,所以AppClassLoader的parent还是置为ExtClassLoader而不是Bootstrap Loader。

* 类的加载流程
类加载的时候遵循一个原则:“类加载器会依类的继承体系从上至下依次加载”。举个例子:“如果C继承了B并实现了接口I,而B有继承自A”,则类加载器在加载C时,加载的次序会是A->B->I->C,(注:interface会如同class一样被Java编译器编译为独立的.class文件)

其实我对底层的东西并未给与太多的关注,如果在哪个项目中需要我去了解底层的话,我会去很好的学习。

Mix-in in Ruby

Matz的一篇PPT“Object-Oriented scripting in Ruby”中,Matz提到Ruby提供一种语言机制Mix-in,在其PPT中如是描述的“No Multiple Inheritance,but Mix-in”、“Mix-in is as strong as multiple inheritance,but simple”。

* “module” in Ruby
在C++、Java、C#中都有namespace的概念,是用来隔离代码,防止代码冲突的。在Ruby中是采用“module”这个关键字来完成namespace功能的。

//considering the following code:
//MicrosoftCompiler.rb
module Microsoft
    def Microsoft.compile
        print "This is microsoft's compiler", "\n"
    end
end

//SunCompiler.rb
module Sun
    def Sun.compile
        print "This is Sun's compiler", "\n"
    end
end
    
//TestCompiler.rb
require "D:\\Ruby\\MicrosoftCompiler.rb"
require "D:\\Ruby\\SunCompiler.rb"

Microsoft.compile
Sun.compile

//output:
>ruby TestComplier.rb
This is microsoft's compiler
This is Sun's compiler
>Exit code: 0

module除了体现namespace的概念外,从上面代码中我们还可以看到在两个module中定义的method和调用这个method时都在其前面加上其所在module的名字了。这样定义的函数叫module method。但是不是所有要使用module中函数都要显式的加上module名字呢?答案:不是。请往下看。

* Mix-in :Another wonderful use of “module”
由module method的定义和调用方式让我们联想到class method(注意不是class instance method),也让我们从module联想到class了。但是A module是不能像class那样拥有实例的,module毕竟还不是class,但是我们可以在class的定义中“include”一个module,这样会发生什么呢?

//considering the following code:
module Mammal  # 哺乳动物
    def suckle     # 哺乳
        print "I can suckle my baby" ,"\n"
    end
end

module Flyable    #可飞行的
    def fly    #飞行    
        print "I can fly", "\n"
    end
end

class Chiropter    #蝙蝠
    include Mammal        #蝙蝠是哺乳动物
    include Flying        #蝙蝠可以飞行
end

aChiropter = Chiropter.new
aChiropter.suckle
aChiropter.fly

//output:
>ruby TestMixin.rb
I can suckle my baby
I can fly
>Exit code: 0

如上面代码得出的结果我们可以看出,一旦一个class中include了一个module,那么所有module中的methods就好像成为class’s instance methods一样,这就是mix-in,看起来被mix-in的modules的行为更像是这个class的super class,通过这种方法我们可以间接实现多重继承。但是注意看看被mixin的module中的函数是如何定义的?和前面代表namespace概念的module中的method定义不同的是在于定义时method name前不见了module name,这样定义出来的method叫module instance method,虽然module不能实例化,但是一旦被mixin到某个class中,这些module instance method的使用就等价于class instance method了。

总结:
1.Module的两种用法
 a) 体现namespace概念;
 b) 被mixin到class中间接的实现类的多重继承。
2.Module中的两种method定义和用法
module module_name
    def module_name.module_method_name
        # module method , for representing namespace concept
    end

    def module_instance_method_name
        # module instance method
        # Once the module has been mixed in,
        #the method will be equal to a class instance method
    end
end

如发现本站页面被黑,比如:挂载广告、挖矿等恶意代码,请朋友们及时联系我。十分感谢! Go语言第一课 Go语言精进之路1 Go语言精进之路2 商务合作请联系bigwhite.cn AT aliyun.com

欢迎使用邮件订阅我的博客

输入邮箱订阅本站,只要有新文章发布,就会第一时间发送邮件通知你哦!

这里是 Tony Bai的个人Blog,欢迎访问、订阅和留言! 订阅Feed请点击上面图片

如果您觉得这里的文章对您有帮助,请扫描上方二维码进行捐赠 ,加油后的Tony Bai将会为您呈现更多精彩的文章,谢谢!

如果您希望通过微信捐赠,请用微信客户端扫描下方赞赏码:

如果您希望通过比特币或以太币捐赠,可以扫描下方二维码:

比特币:

以太币:

如果您喜欢通过微信浏览本站内容,可以扫描下方二维码,订阅本站官方微信订阅号“iamtonybai”;点击二维码,可直达本人官方微博主页^_^:
本站Powered by Digital Ocean VPS。
选择Digital Ocean VPS主机,即可获得10美元现金充值,可 免费使用两个月哟! 著名主机提供商Linode 10$优惠码:linode10,在 这里注册即可免费获 得。阿里云推荐码: 1WFZ0V立享9折!


View Tony Bai's profile on LinkedIn
DigitalOcean Referral Badge

文章

评论

  • 正在加载...

分类

标签

归档



View My Stats