Tag: 构造函数

我如何强制在我的抽象类的所有子类中定义构造函数

我有一个定义抽象方法的抽象类A. 这意味着,对于一个class级来说,这个抽象的方法必须得到执行。 我想我所有的子类实现一个2 int作为参数的构造函数。 声明一个构造函数会破坏我的目的,因为我想在子类中定义构造函数,而且我不知道任何有关实现的信息。 而且我不能声明构造函数是抽象的; 有没有办法做到这一点 ? 我想要的例子: 假设我正在定义Matrix类的API。 在我的问题,matrix不能改变他们的尺寸。 要创buildmatrix,我需要提供它的大小。 因此,我希望我的所有实现者都能提供构造函数的大小作为参数。 这个构造函数是由问题驱动的,而不是由于实现问题。 只要方法的所有语义都保持不变,实现可以用它们做任何事情。 假设我想提供我的抽象类中的invert()方法的基本实现。 这种方法将创build一个新的matrix与this倒转的维度。 更具体地说,就像在抽象类中定义的那样,它将使用一个带有两个整数的构造函数创build一个与此相同类的新实例。 因为它不知道实例将使用reflection(getDefinedConstructor),我想要一个保证,我会得到它的方式,这将是有意义的实现。

Java:访问具有types参数的私有构造函数

这是关于java私有构造函数的这个问题的后续。 假设我有以下class级: class Foo<T> { private T arg; private Foo(T t) { // private! this.arg = t; } @Override public String toString() { return "My argument is: " + arg; } } 我将如何使用reflection来构造new Foo("hello") ? 回答 基于jtahlborn的回答 ,下面的工作: public class Example { public static void main(final String[] args) throws Exception { Constructor<Foo> constructor; constructor = […]

在C#中调用重写的构造函数和基础构造函数

我有两个类,富和酒吧,有这样的构造: class Foo { Foo() { // do some stuff } Foo(int arg) { // do some other stuff } } class Bar : Foo { Bar() : base() { // some third thing } } 现在我想介绍一个Bar的构造函数,它需要一个int,但是我想要Bar()中发生的东西以及Foo(int)中的东西运行。 像这样的东西: Bar(int arg) : Bar(), base(arg) { // some fourth thing } 有没有办法在C#中做到这一点? 到目前为止,我所做的最好的工作是将Bar()完成的工作放到一个函数中,这个函数也被Bar(int)调用,但是这样很不雅观。

一个构造函数作为代表 – 它可能在C#中?

我有一个类如下: class Foo { public Foo(int x) { … } } 我需要传递给某个方法一个委托像这样: delegate Foo FooGenerator(int x); 是否有可能作为FooGenerator值直接传递构造函数,而不必键入: delegate(int x) { return new Foo(x); } ? 编辑:对于我个人的使用,这个问题是指.NET 2.0,但3.0 +提示/响应也是受欢迎的。

如何从子类调用基类的__init__方法?

如果我有一个Python类: class BaseClass(object): #code and the init function of the base class 然后我定义一个孩子类,如: class ChildClass(BaseClass): #here I want to call the init function of the base class 如果基类的初始化函数需要一些参数,我将它们作为子类的初始化函数的参数,那么如何将这些parameter passing给基类呢? 我写的代码是: class Car(object): condition = "new" def __init__(self, model, color, mpg): self.model = model self.color = color self.mpg = mpg class ElectricCar(Car): def __init__(self, battery_type, model, […]

JUnit:使用构造函数而不是@Before

我正在使用JUnit 4.我看不到在构造函数中初始化或使用由@Before注释的专用init函数之间的@Before 。 这是否意味着我不必担心呢? 有没有什么情况下@Before不仅仅是在构造函数中进行初始化?

如何强制删除一个python对象?

我很好奇python中__del__的细节,什么时候以及为什么它应该被使用,什么不应该被使用。 我已经学会了这样一个艰难的方式,它不是真正的天真地期望从析构函数中得到什么,因为它不是__new__ / __init__的对立面。 class Foo(object): def __init__(self): self.bar = None def open(self): if self.bar != 'open': print 'opening the bar' self.bar = 'open' def close(self): if self.bar != 'closed': print 'closing the bar' self.bar = 'close' def __del__(self): self.close() if __name__ == '__main__': foo = Foo() foo.open() del foo import gc gc.collect() 我在文档中看到, 不保证__del__()方法在解释器退出时仍然存在的对象被调用。 […]

为什么不能枚举构造函数在Java中被保护或公开?

整个问题在标题中。 例如: enum enumTest { TYPE1(4.5, "string1"), TYPE2(2.79, "string2"); double num; String st; enumTest(double num, String st) { this.num = num; this.st = st; } } 构造函数没有使用默认或private修饰符,但给了我一个编译器错误,如果给定的public或protected修饰符。

什么是受保护的构造函数的实际用法?

为什么会有人声明一个构造函数被保护? 我知道构造函数被宣布为私有的,目的是不允许它们在堆栈上创build。

Java构造函数风格:检查参数不为null

如果你有一个接受一些参数的类但是没有一个被允许为null那么最佳实践是什么? 以下是显而易见的,但是有一点不明确: public class SomeClass { public SomeClass(Object one, Object two) { if (one == null || two == null) { throw new IllegalArgumentException("Parameters can't be null"); } //… } } 这里的exception让你知道哪个参数是空的,但是构造函数现在很丑陋: public class SomeClass { public SomeClass(Object one, Object two) { if (one == null) { throw new IllegalArgumentException("one can't be null"); } if […]