Java VS C#

C# Pros

1.   Metadata attributesare a VERY, VERY powerful feature.  Attributes allow classes to be much more expressive.  Java NEEEEEDS attributes.

2. When overriding virtual methods you have to explicitly state that you're overriding with theoverrides keyword.

Java:

1. TreeMap 按照key对象的顺序进行存储。那应该意味着key对象必须实现Comparable接口。

2. Comaprable VS Comparator

    Java类库中的许多类都实现了Comparable接口。这个接口的意义用书上的话讲就是:Lists (and arrays) of objects that implement this interface can be sorted automatically byCollections.sort (and Arrays.sort). Objects that implement this interface can be used as keys in asorted map or as elements in a sorted set, without the need to specify a comparator.

    有一些类没有实现Comparable接口,那么用集合类存储这些对象时如何实现按照顺序保存呢?Comparator接口的意义如下:A comparison function, which imposes atotal ordering on some collection of objects. Comparators can be passed to a sort method (such asCollections.sort or Arrays.sort) to allow precise control over the sort order. Comparators can also be used to control the order of certain data structures (such assorted sets or sorted maps), or to provide an ordering for collections of objects that don't have anatural ordering.

 3. Iterable VS Iterator

最重要的是搞清楚集合类的遍历为什么需要两个接口。这里面用到了面向对象的一个原则即优先采用Composition而不是Inheritance.

 Iterable只有一个方法即iterator(), 其返回值是一个实现了iterator接口的集合类。而所有的集合类都实现了iterable接口(采用Composition),而不是实现了iterator接口(采用inheritance)。这样做的好处有二:

   1)由于采用了松耦合的组成关系,使得对集合的遍历独立于集合内部对象的存储结构;

    2)对于一个第三方的包含一组对象序列的的类可以通过实现iterable接口使之可以被遍历;而通过继承集合类来实现遍历有两个不利之处:一是有一些不必要的方法也要实现;二是如果一个类已经继承了某个类则无法再继承集合类。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值