主题:【原创】JAVA和C#,武当和少林之争! -- Highway
问我什么是multiple inheritance。我回答完后,问有什么危险。然后再问:JAVA中没有multiple inheritance,那么如果要实现,该怎么做?我对JAVA不熟。哪位可以帮我解释一下?谢谢。
Java我不熟,自己玩儿过一会,但从未正经做过项目,
不敢乱发言。只是从C/C++/C#的角度来探讨一下。
带实现的Interface就不是Interface,而是concrete class了。
因此我觉得是更像STL中的Fuctor,function当作object用。
同意兄台这个看法,delegate非常容易使用所以容易被滥用。
事实上这也是普遍的情况,越好用的东西越容易被乱用。
Q:Why doesn't the Java language support multiple inheritance?
A: Whenever you find yourself asking why Java has or does not have some feature, consider the design goals behind the Java language. With that in mind, I started my search by skimming through "The Java Language Environment" by James Gosling and Henry McGilton (Sun Microsystems), a white paper published in May 1996 that explains some of the reasoning behind Java's design.
As the white paper states, the Java design team strove to make Java:
* Simple, object oriented, and familiar
* Robust and secure
* Architecture neutral and portable
* High performance
* Interpreted, threaded, and dynamic
The reasons for omitting multiple inheritance from the Java language mostly stem from the "simple, object oriented, and familiar" goal. As a simple language, Java's creators wanted a language that most developers could grasp without extensive training. To that end, they worked to make the language as similar to C++ as possible (familiar) without carrying over C++'s unnecessary complexity (simple).
In the designers' opinion, multiple inheritance causes more problems and confusion than it solves. So they cut multiple inheritance from the language (just as they cut operator overloading). The designers' extensive C++ experience taught them that multiple inheritance just wasn't worth the headache.
Instead, Java's designers chose to allow multiple interface inheritance through the use of interfaces, an idea borrowed from Objective C's protocols. Multiple interface inheritance allows an object to inherit many different method signatures with the caveat that the inheriting object must implement those inherited methods.
Multiple interface inheritance still allows an object to inherit methods and to behave polymorphically on those methods. The inheriting object just doesn't get an implementation free ride. For an excellent discussion of interface inheritance, read Wm. Paul Rogers's "Reveal the Magic Behind Subtype Polymorphism"
简言之,任何一个Java Class,只能extend一个Class(一个父亲),但是它可以implement多个Interface。这样Java实现了OO得多态性,而又避免了C++ multiple inheritance造成的问题(Complex, hard to read, understand and maintain)
delegate这种type safe的function pointer更好用。
我是从java开始走向面向对象的设计的,个人感情更喜欢用interface。我觉得delegate就是interface在某些设计中的简化使用,主要是在事件处理上方便简洁很多。而大多数设计方案中,使用interface会更加清晰。interface当然可以用纯的abstract class代替,但java把它单独分离出来,还是想强调“面向接口编程”的重要性吧。C#中也保留interface应该能说明问题。
莫非在技术上也有“屁股决定脑袋”的说法不成?呵呵。
这实在是个不太好探讨的话题。就全局议可能泛泛而谈,就局部讲则可能过于精深。
自己是个半路出家的,没有什么精深的见解,也只能泛泛的说一说。
这可能和打仗时选用兵器一样,大刀还是长矛,特点不同,玩法当然不同。
就个人而言,在关心其技术特点的同时,则更看重从工程开发角度的分析。
当然,系统构成、客户需求、成本预算等等,最终也会左右开发语言的选定。
谢谢。
functors in C++, I think the best so far is boost::lambda. It is so cool. It almost equals to closures.
That's what I meant -- not sure why I wanted to "verbalize" it.
functor也是,只不过定义来自lambda体系而已。其实都是interface。你想你定义delegate的时候也要给出signature,这实际上就是定义了一个单成员的interface。然后实际用这个delegate的时候,你要给出符合该signature的某一个具体函数,那实际上就是给出一个该interface的实现。
从这里可以看出,delegate虽然方便,但是有一点局限性,就是传递的函数显然是stateless,相比之下,以实现interface的class形式出现,就可以是stateful,基于stateful的callback对于鼓励OO设计大有好处。
顺便问楼主个问题,关于T-SQL的。
第一个关于变量定义,如何定义类似于PL/SQL
里面那种 字段%type 的变量?
第二个,编写了一个insert的trigger,但是要用DTS进行数据批量插入,但是DTS不能fire trigger。有什么好办法啊,BCP就可以了么?
献朵花!
不过Functor可以stateful,delegate function也可以stateful。
至于第二个问题,我认为是无论是用DTS,还是用BCP,缺省情况下都不会激发你的Trigger,如果想触发,试验一下下面这个方法:
在DTS Package的Property中,uncheck Use fast load选项。不过这样速度会下降很多,请三思而后行!
先写一下。刚才忙活了半天,用BCP试了一下,果然不行。老大说默认不可以,BCP有参数可以激活TRIGGER么?
第一个问题是这样的。PL/SQL中不是有个%TYPE么,可以定义一个变量,让其绑定某个表的某个字段,也就是说它的类型和这个字段的类型相同,这样如果字段类型改了,就不用再去改程序中的变量定义了。一般都是用于SELECT的时候,存放某字段的值。据说T-SQL也支持这个功能,但是看帮助没找到相应的语法,故而来问一下……
用了BCP,加了参数-h "FIRE_TRIGGERS"。终于可以触发了