考虑以下代码:
abstract class AbstractFoo {
}
class Foo extends AbstractFoo {
}
class Bar {
getFooConstructor(): typeof AbstractFoo {
return Foo;
}
}
let bar = new Bar();
let FooConstructor = bar.getFooConstructor();
let foo = new FooConstructor();
它无法编译,出现以下错误:
错误TS2511:无法创建抽象类的实例。
初始化继承类中的常量或其他字段的最佳方法是什么?我意识到这个例子中有很多语法错误,但是这是清楚地解释我想要做什么的最好的例子。
public abstract class Animal {
public abstract const string Name; // #1
public abstract const bool CanFly;
public abstract double Price; // price is not const, because it can be modified
public void Fly() {
if (!CanFly)
在抽象类中有一个构造函数好吗?
创建抽象类的构造函数是一种很好的编程实践吗?因为抽象类不能被初始化,所以它们的子类被初始化。
下面是我的类结构。
public abstract class Scheduler
{
private Storyboard timer;
protected Scheduler()
{
// initialize the timer here.
timer = new Storyboard();
this.PollInterval = 60;
}
}
public class Ta
C#中的类怎么可能没有定义构造函数呢?例如,我有一个类
internal class TextStyle
{
internal string text = "";
internal Font font = new Font("Arial", 8);
internal Color color = Color.Black;
}
在代码中,这个类被实例化为
TextStyle textParameters = new TextStyle();
我被这个代码示例搞糊涂了:
我搞不懂为什么要先调用draw的子方法。我很困惑,因为当RoundGlyph在main中实例化时,超级构造函数不是在实例化父对象之前实例化父对象吗?
class Glyph {
void draw() {
System.out.println("test"); // method will be called once you create a Glyph object, because when we create a round glyph before creating a glyph the super constru
我试图解决如何在任何子类中访问/使用抽象类的字段/方法,并且在web上看到了这个示例。
abstract class Instrument {
protected String name;
abstract public void play();
}
abstract class StringedInstrument extends Instrument {
protected int numberOfStrings;
}
中给出的具体示例子类
public class ElectricGuitar extends StringedInstrument {
我有一个关于FxCop的问题和警告:Abstract types should not have constructors。
这对于许多抽象类(可能是所有的,我还没有检查过)都显示出来了。在我看来,它们中的大多数都没有新的方法,所以我假设是编译器添加了一个默认的方法。因此,为了删除它,我添加了一个私有默认构造器(Private Sub New()),这意味着所有继承类都无法构建,并返回错误:Class 'InheritingClass' has no accessible 'Sub New' and cannot be inherited.
这看起来很奇怪,因为
在“Java:完整的参考”一书中
// Demonstrate when constructors are called.
// Create a super class.
class A {
A() {
System.out.println("Inside A's constructor.");
}
}
// Create a subclass by extending class A.
class B extends A {
B() {
System.out.println("Insi
我正在创建一个抽象的Font类,我希望强制子类声明一个带有单个int参数(字体大小)的构造函数。我试着这样做如下。
public abstract class Font {
...
public abstract Font(int size);
...
}
但是我的编译器声明:
Error:(20, 19) java: <path omitted>/Font.java:20: modifier abstract not allowed here
这并不是世界末日--这并不是绝对必要的,我只是想让Java编译器强迫我记住实现那个构造函数。我只是想知道为什么不允许这样做?
我想知道当有人在继承自抽象类的类的构造函数中调用java super()方法时会发生什么。正如我们所知,抽象类不能被实例化,但super调用抽象类的构造函数。 让我们举个例子。 public abstract class Figure { // cant be instantiated
private type firstAttribute;
private type secondAttribute;
public Figure(type firstAttribute, type secondAttribute) {
this.fisrtAttrib
我有以下Windows运行时解决方案设置:
在我的EXE引用的DLL中
public abstract class BaseClass {
public BaseClass(Guid id, string name) {
//...
}
}
在EXE中
public sealed class DerivedClass : BaseClass {
}
在DLL中,还有另一个类使用在运行时指定的BaseClass的子类型。我想,由于基类有构造函数,所以我可以像这样实例化它:
//this._userType holds the type of DerivedC
这样的事情是可能的吗?
abstract class AbstractSuperClass {
private Entity entity;
public AbstractSuperClass(Entity entity) {
this.entity = entity;
}
public abstract void operate();
}
public class SubClass extends AbstractSuperClass {
public void operate() {
this.entity.doVoidMethod(); // i
public class ParentBaseClass
{
public ParentBaseClass()
{
// Parameter less constructor
}
public ParentBaseClass(string type)
{
// single parameter constructor
}
public ParentBaseClass(Entity model)
{
// entity type constructor
}
public
基类中的Virtual可以引用/访问/使用来自子类的静态变量吗?
在代码中可能更容易解释。在下面的示例中,是否可以让虚拟方法Arc::parseFile()查找data_arc
public class Base {
public static string DATA_3D = "data_3d";
public virtual void parseFile(string fileContents) {
// Example of file contents: "data_3d (0,0,0),(1,1,1),(2,2,2)
如何从第二个和第三个获得子构造函数?既然公共抽象优先不起作用?
public abstract class First {
public Point position;
public First() {} //how do i make this constructor like an abstract?
// so that it will go get Second constructor or Third
}
public class Second extends First {
public Second (
我在跟踪c# complete reference。下面是演示Constructor and inheritance的示例,
class TwoDShape {
double pri_width;
double pri_height;
// Properties for Width and Height.
public double Width {
get { return pri_width; }
set { pri_width = value < 0 ? -value : value; }
}
public double Height {