为什么Dart不继承构造函数? [英] Why Dart does not inherit constructor?

查看:452
本文介绍了为什么Dart不继承构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自PHP世界,好奇为什么开发人员选择不添加构造函数(使用arg)继承。从我的观点,它违反了DRY原则通过重复大量的代码,取决于结构。我做了很少的研究 - PHP,Ruby,Python继承了构造函数。 Java,C#,C ++不。 C ++ 0x具有显式定义继承的新功能。

I am coming from PHP world and am curious why developers choose the way not adding constructor (with arg) to inheritance. From my view it violates DRY principle by repeating a lot of code, depending on structure. I did little research - PHP, Ruby, Python inherits constructor. Java, C#, C++ not. C++0x have new feature explicitly defining inheritances.

那么,对于没有构造函数继承和明确写入构造函数的程序员来说,有什么优势吗?

So is there any advantage for programmer not having constructor inherited and explicitly write constructor again and again?

推荐答案

我想你可能需要问Dart的设计师提出问题,但是从继承树中省略构造函数的典型原因是,具有需要发生的特定构造时操作。在java中(例如),所有对象都继承自Object,它有一个零值构造函数。

I think you'd probably have to ask Dart's designers that question, but the typical reason for omitting constructors from the inheritance tree is that a class may have specific construct-time operations that need to happen. In java (for example), all objects inherit from Object, which has a zero value constructor.

所以如果你想创建一个这样的对象:

So if you wanted to create some object like this:

public class Foo {
   private final String bar;

   public Foo(String barValue) {
       this.bar = barValue;
   }
}

当有人调用父的零参数构造函数时发生:

You'd be left with some uncertainty as to what happens when someone called the parent's zero parameter constructor:

Foo bla = new Foo(); // runtime error or is bar null?

当然,与所有语言功能一样,您可以想出这种行为可以被指定的方式,但显然java和dart的设计师不喜欢任何他们。

Of course, as with all language features, you can think of ways that this behavior could be specified, but clearly java and dart's designers didn't like any of them.

最后,我认为这一切归结为哲学:php,ruby等,去在减少打字的方向,因为java,C#,dart等,倾向于在更多的打字方向,更少的混乱的空间。

In the end, I think it all comes down to philosophy: php, ruby, etc., goes in the direction of less typing where as java, C#, dart, etc., tend to go in the direction of more typing and less room for confusion.

这篇关于为什么Dart不继承构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆