Tag: 构造函数

构造函数可以返回什么值来避免返回?

当使用new关键字调用构造函数时,Javascript中的return语句可以返回非this值的确切情况是什么? 例: function Foo () { return something; } var foo = new Foo (); 如果我没有弄错,如果something是一个非函数原语,就会返回。 否则返回something 。 它是否正确? IOW,什么值可以引起(new Foo () instanceof Foo) === false ?

对象数组初始化没有默认的构造函数

#include <iostream> class Car { private: Car(){}; int _no; public: Car(int no) { _no=no; } void printNo() { std::cout<<_no<<std::endl; } }; void printCarNumbers(Car *cars, int length) { for(int i = 0; i<length;i++) std::cout<<cars[i].printNo(); } int main() { int userInput = 10; Car *mycars = new Car[userInput]; for(int i =0;i < userInput;i++) mycars[i]=new Car[i+1]; printCarNumbers(mycars,userInput); return […]

抽象类可以有一个构造函数吗?

抽象类可以有一个构造函数吗? 如果是这样的话,怎么可以用来做什么用途呢?

@ViewScoped在每个回发请求上调用@PostConstruct

这看起来不正确。 我正在做一些清理我的代码,我只是注意到了这一点。 每个Ajax请求都会触发我的@ViewScoped bean的构造函数和@PostConstruct 。 即使是一个简单的数据库分页也是如此。 我知道 @ViewScoped比@RequestScoped更长,并且不应该在每个请求中重build。 只有在通过GET重新加载完成页面之后。

什么是初始化块?

我们可以把代码放在构造函数或方法或初始化块中。 什么是初始化块的使用? 每个Java程序都必须拥有它吗?

我如何在Java中调用另一个构造函数?

是否有可能从另一个(在同一个类中,而不是从一个子类)调用一个构造函数? 如果是的话如何? 调用另一个构造函数的最好方法是什么(如果有几种方法可以这样做的话)?

Javascript构造函数属性的意义是什么?

试图弯曲JavaScript的承担OO …和其他许多人一样,混淆了constructor属性。 特别是constructor属性的意义,因为我似乎无法使其产生任何效果。 例如: function Foo(age) { this.age = age; } function Bar() { this.name = "baz"; } Bar.prototype = new Foo(42); var b = new Bar; alert(b.constructor); // "Foo". That's OK because we inherit `Foo`'s prototype. alert(b.name); // "baz". Shows that Bar() was called as constructor. alert(b.age); // "42", inherited from `Foo`. 在上面的例子中,对象b似乎有正确的构造函数( Bar […]

有一个构造函数返回一个Promise是不好的做法吗?

我正在尝试为博客平台创build一个构造函数,并且里面有许多asynchronous操作。 这些范围从抓取目录中的post,parsing它们,通过模板引擎发送它们等等。 所以我的问题是,让我的构造函数返回一个承诺,而不是他们所谓的newfunction的对象是不明智的。 例如: var engine = new Engine({path: '/path/to/posts'}).then(function (eng) { // allow user to interact with the newly created engine object inside 'then' engine.showPostsOnOnePage(); }); 现在,用户也可能不提供补充Promise链接: var engine = new Engine({path: '/path/to/posts'}); // ERROR // engine will not be available as an Engine object here 这可能会造成问题,因为用户可能会困惑为什么 engine 在施工后不可用。 在构造函数中使用Promise的原因是有道理的。 我希望整个博客在施工阶段后能够正常运行。 但是,在调用new之后,似乎几乎不能立即访问对象。 我讨论过使用engine.start().then()或engine.init()这样的方法来代替Promise。 但是那些也似乎是臭的。 […]

我可以从另一个构造函数(做构造函数链)在C ++中调用一个构造函数?

作为一个C#开发人员,我习惯于通过构造函数来运行: class Test { public Test() { DoSomething(); } public Test(int count) : this() { DoSomethingWithCount(count); } public Test(int count, string name) : this(count) { DoSomethingWithName(name); } } 有没有办法在C ++中做到这一点? 我试着调用类名称并使用“this”关键字,但都失败了。

在构造函数中调用虚拟成员

我从ReSharper得到关于从我的对象构造函数调用虚拟成员的警告。 为什么这是不该做的事?