英文:
How to change initial state of base parameter from sub class
问题
我正在构建一个应用程序,该应用程序运行"任务"。所有任务都继承自BaseTask
,其中包含一些默认逻辑/函数,应该对所有任务可用,比如Start
、Stop
、Enable
和Disable
函数。
基类还应包含一个active
属性,用于表示任务是否处于活动状态,因为只有"active"任务才能启动。
为了给您一个关于代码的概念:
// 由于我不知道如何声明active参数以使其起作用,我在代码中用注释来描述这个问题。
public abstract class BaseJob {
private Status jobStatus;
// 在这里定义了一些默认为false的active属性
public BaseJob() {
jobStatus = active ? Status.initialized : Status.inactive;
}
public void Start() {
if (active) {
Run();
}
}
public void Stop() {
if (jobStatus == Status.running) {
// 停止任务的逻辑
}
}
public void Enable() {
// 将"active"设置为true
jobStatus.initialized
}
public void Disable() {
Stop();
// 将"active"设置为false
jobStatus = Status.inactive
}
public void Run() {
..
task();
..
jobStatus = Status.running;
}
public abstract void task() { }
}
public class JobA: BaseJob {
public override void task() {
// 具体工作的逻辑
}
}
现在的问题是:
- 如何在
BaseJob
中定义默认值为false的active
属性,但以一种方式,可以在子类中"覆盖"它的默认值。请考虑以下情况:- 我想要使用
BaseJob
函数Enable
和Disable
来更改active
的值。 - 我希望确保
active
属性只能通过相关的BaseJob
函数来更改,因为它们可能包含不仅仅是切换"active"布尔值的更多逻辑。
- 我想要使用
我已经尝试了不同的方法,但在任何情况下都遇到了问题。
- 在
BaseJob
中使用private bool active
不起作用,因为它不能从子类中访问和覆盖(这是正确的吗?:-D)。 - 使用抽象的getter和setter无法完成任务,因为它不能在
BaseJob
类中包含默认值。 - 受保护的getter和setter或布尔类型也无法工作,因为我不知道如何从子类中覆盖默认值。
- 在
BaseJob
中使用protected virtual bool active
也不起作用。我收到的错误是"The modifier 'virtual' is not valid for this item"。
我看到的可能解决方案是:
可能我需要覆盖构造函数以设置默认值,不是吗?我实际上想避免这样做,但我认为这是唯一的选择?
英文:
I'm building an application which runs "tasks". All tasks inherrit BaseTask
which contains some default logic/functions which should be available for all tasks like Start
, Stop
, Enable
and Disable
functions.
The base class also should contain some active
property which tells if the task is active as only "active" tasks can be started.
To give an idea about the code:
// As i have no idea how to declare the active param to make it work, i describe this with a comment in my code.
public abstract class BaseJob {
private Status jobStatus;
// some active property defined here which is false by default
public BaseJob() {
jobStatus = active ? Status.initialized : Status.inactive;
}
public void Start() {
if (active) {
Run();
}
}
public void Stop() {
if (jobStatus == Status.running) {
// logic to stop the job
}
}
public void Enable() {
// set "active" to true
jobStatus.initialized
}
public void Disable() {
Stop();
// Set "active" to false
jobStatus = Status.inactive
}
public void Run() {
..
task();
..
jobStatus = Status.running;
}
public abstract void task() { }
}
public class JobA: BaseJob {
public override void task() {
// The logic of the specific job
}
}
Now the questions:
- How to define the active property in the
BaseJob
which is false by default but in such a way i can "override" its default value in a child class. Take into account:- That I want to change the active value with the
BaseJob
functionsEnable
andDisable
- That I want to guarantee that the
active
propert can only be changed by using the relatedBaseJob
functions as they may contain more logic than just toggle the "active" bool.
- That I want to change the active value with the
I already tried different ways but in eighter case i run into another issue.
private bool active
in the BaseJob does not work as its not accessible and overrideable (is this a correct work? :'-D ) from the child class.- Using an abstract getter/setter cannot do the job as it cannot contain a default value in the BaseJob class
- protected getter and setter or bool is not working as i have no idea how to override the default value from the child class.
protected virual bool active
in theBaseJob
is not working. `The modifier "virtual" is not valid for this item" is the error i receive.
Possible solution i see:
Probably i need to override the constructor which sets the default value isn't it? I actually want to avoid this but i think this is the only option?
答案1
得分: 1
I think you can just define a protected virtual property to represent the initial active state. The active field is private so that only the base class has the possibility changing it.
您可以只需定义一个受保护的虚拟属性来表示初始活动状态。活动字段是私有的,因此只有基类有可能更改它。
public abstract class BaseJob
{
private bool _active;
protected virtual bool IsActiveByDefault => false;
public BaseJob()
{
_active = IsActiveByDefault;
}
}
Child classes can override the property to change the default state without touching the private active field.
子类可以重写属性以更改默认状态,而无需触及私有的活动字段。
public class JobA: BaseJob
{
protected override bool IsActiveByDefault => true;
}
英文:
I think you can just define a protected virtual property to represent the initial active state. The active field is private so that only the base class has the possibility changing it.
public abstract class BaseJob
{
private bool _active;
protected virtual bool IsActiveByDefault => false;
public BaseJob()
{
_active = IsActiveByDefault;
}
}
Child classes can override the property to change the default state without touching the private active field.
public class JobA: BaseJob
{
protected override bool IsActiveByDefault => true;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论