おやすみ
おやすみ
CC#
Created by おやすみ on 4/19/2024 in #help
Any way to inherit base abstract class' constructor?
Pretty new to C#. I've got this goofy code:
public abstract class Instruction
{
protected VirtualMachine globalScope;
protected Procedure scope;
protected Traceback traceback;
public Instruction(VirtualMachine globalScope, Procedure scope, Traceback traceback)
{
ArgumentNullException.ThrowIfNull(globalScope);
ArgumentNullException.ThrowIfNull(scope);
ArgumentNullException.ThrowIfNull(traceback);
this.globalScope = globalScope;
this.scope = scope;
this.traceback = traceback;
}
public abstract void Callback();
}
public class PushZero : Instruction
{
public PushZero(VirtualMachine globalScope, Procedure scope, Traceback traceback) : base(globalScope, scope, traceback) { }
...
public abstract class Instruction
{
protected VirtualMachine globalScope;
protected Procedure scope;
protected Traceback traceback;
public Instruction(VirtualMachine globalScope, Procedure scope, Traceback traceback)
{
ArgumentNullException.ThrowIfNull(globalScope);
ArgumentNullException.ThrowIfNull(scope);
ArgumentNullException.ThrowIfNull(traceback);
this.globalScope = globalScope;
this.scope = scope;
this.traceback = traceback;
}
public abstract void Callback();
}
public class PushZero : Instruction
{
public PushZero(VirtualMachine globalScope, Procedure scope, Traceback traceback) : base(globalScope, scope, traceback) { }
...
In python, if you inherit from a class that defines a constructor, it will be inherited, and when creating an instance of that subclass, that inherited constructor is called. I know, its just a single (a bit long) line, but is there something similar in C#? (or, at least, a way to make that line shorter...)
20 replies