试图绕过Javascript对OO的看法......和许多其他人一样,对constructor
财产产生混淆.特别是constructor
财产的重要性,因为我似乎无法使其产生任何影响.例如:
function Foo(age) {
this.age = age;
}
function Bar() {
Foo.call(this, 42);
this.name = "baz";
}
Bar.prototype = Object.create(Foo.prototype);
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`.
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,对象b
似乎有正确的构造函数call(Bar
) - 并且它继承了age属性Foo
.那么为什么许多人认为这是必要的步骤:
Bar.prototype.constructor = Bar;
Run Code Online (Sandbox Code Playgroud)
显然,Bar
构造时会调用正确的构造函数b
,因此这个原型属性有什么影响?我很想知道它实际上使构造函数属性设置'正确'有什么实际区别 - 因为我无法看到它对创建对象后实际调用的构造函数有任何影响.
有没有解决如何为静态类创建构造函数?
我需要在初始化类时加载一些数据,但我需要一个且只需要一个对象.
在C#中,当你这样做时
Class(Type param1, Type param2) : base(param1)
Run Code Online (Sandbox Code Playgroud)
是先执行的类的构造函数,然后调用超类构造函数还是先调用基础构造函数?
在一些情况下,我最常使用"混蛋注射".当我有一个"适当的"依赖注入构造函数时:
public class ThingMaker {
...
public ThingMaker(IThingSource source){
_source = source;
}
Run Code Online (Sandbox Code Playgroud)
但是,对于我打算作为公共API(其他开发团队将使用的类)的类,我永远找不到比编写具有最可能需要的依赖项的默认"bastard"构造函数更好的选择:
public ThingMaker() : this(new DefaultThingSource()) {}
...
}
Run Code Online (Sandbox Code Playgroud)
这里明显的缺点是,这会对DefaultThingSource产生静态依赖; 理想情况下,没有这种依赖性,消费者总是会注入他们想要的任何IThingSource.但是,这太难用了; 消费者希望新建一个ThingMaker并开始制作物品,然后几个月后,在需要时注入其他东西.在我看来,这只留下了几个选项:
男孩,#3肯定看起来很有吸引力.还有另一种更好的选择吗?#1或#2似乎不值得.
为什么必须通过引用传递复制构造函数的参数?
拥有使用默认参数的类构造函数是一种好习惯,还是应该使用单独的重载构造函数?例如:
// Use this...
class foo
{
private:
std::string name_;
unsigned int age_;
public:
foo(const std::string& name = "", const unsigned int age = 0) :
name_(name),
age_(age)
{
...
}
};
// Or this?
class foo
{
private:
std::string name_;
unsigned int age_;
public:
foo() :
name_(""),
age_(0)
{
}
foo(const std::string& name, const unsigned int age) :
name_(name),
age_(age)
{
...
}
};
Run Code Online (Sandbox Code Playgroud)
这两个版本似乎都有用,例如:
foo f1;
foo f2("Name", 30);
Run Code Online (Sandbox Code Playgroud)
您更喜欢或推荐哪种风格?为什么?
因为我一直在使用Spring,如果我要编写一个具有依赖项的服务,我会执行以下操作:
@Component
public class SomeService {
@Autowired private SomeOtherService someOtherService;
}
Run Code Online (Sandbox Code Playgroud)
我现在遇到了使用另一个约定来实现相同目标的代码
@Component
public class SomeService {
private final SomeOtherService someOtherService;
@Autowired
public SomeService(SomeOtherService someOtherService){
this.someOtherService = someOtherService;
}
}
Run Code Online (Sandbox Code Playgroud)
我理解这两种方法都有效.但是使用选项B有一些优势吗?对我来说,它在类和单元测试中创建了更多代码.(必须编写构造函数而不能使用@InjectMocks)
有什么我想念的吗?除了在单元测试中添加代码之外,还有其他任何自动装配的构造函数吗?这是一种更优先的依赖注入方式吗?
我希望有一个非模板类,其模板构造函数没有参数.
据我所知,它不可能拥有它(因为它会与默认构造函数冲突 - 我是对的吗?),解决方法如下:
class A{
template <typename U> A(U* dummy) {
// Do something
}
};
Run Code Online (Sandbox Code Playgroud)
也许有更好的替代方案(或更好的解决方法)?
我很惊讶地意外地发现以下工作:
#include <iostream>
int main(int argc, char** argv)
{
struct Foo {
Foo(Foo& bar) {
std::cout << &bar << std::endl;
}
};
Foo foo(foo); // I can't believe this works...
std::cout << &foo << std::endl; // but it does...
}
Run Code Online (Sandbox Code Playgroud)
我将构造对象的地址传递给它自己的构造函数.这看起来像源级别的循环定义.标准是否真的允许您在构造对象之前将对象传递给函数,还是这种未定义的行为?
鉴于所有类成员函数已经将指向其类实例的数据的指针作为隐式参数,我认为这并不奇怪.并且数据成员的布局在编译时是固定的.
请注意,我不是在问这是有用还是好主意; 我只是在修补一些关于课程的更多信息.
我刚刚在我们的生产环境中遇到了相当不愉快的经历 OutOfMemoryErrors: heapspace..
我将这个问题追溯到我ArrayList::new
在函数中的使用.
要通过声明的构造函数(t -> new ArrayList<>()
)验证这实际上比正常创建更糟糕,我编写了以下小方法:
public class TestMain {
public static void main(String[] args) {
boolean newMethod = false;
Map<Integer,List<Integer>> map = new HashMap<>();
int index = 0;
while(true){
if (newMethod) {
map.computeIfAbsent(index, ArrayList::new).add(index);
} else {
map.computeIfAbsent(index, i->new ArrayList<>()).add(index);
}
if (index++ % 100 == 0) {
System.out.println("Reached index "+index);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
运行方法newMethod=true;
将导致方法OutOfMemoryError
在索引达到30k后失败.随着newMethod=false;
程序不会失败,但一直冲击直到被杀(索引容易达到150万).
为什么在堆上ArrayList::new
创建如此多的Object[]
元素会导致OutOfMemoryError
如此之快?
(顺便说一下 - 当集合类型出现时也会发生HashSet …
constructor ×10
c++ ×4
c# ×3
class ×2
.net ×1
autowired ×1
coding-style ×1
copy ×1
inheritance ×1
java ×1
java-8 ×1
javascript ×1
oop ×1
overloading ×1
prototype ×1
spring ×1
static ×1
templates ×1