本文介绍了覆盖 Salesforce Apex 中抽象类的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 apex 中有一个抽象类,其中有几个我想在子类中覆盖的属性.根据文档,属性同时支持覆盖和虚拟访问修饰符.但是,当我尝试在父类或子类中使用它们中的任何一个时,我收到一条错误消息,指出变量不能标记为虚拟/覆盖.这是导致此错误的代码的传真:

I have an abstract class in apex with several properties that I would like to override in a child class. According to the documentation, properties support both the override and virtual access modifiers. However, when I try to use either of them in either the parent or child class, I get an error saying that variables cannot be marked as virtual/override. Here is a facsimile of the code that causes this error:

public abstract class Row{
    public virtual double value{
        get{return value==null ? 0 : value;}
        set;
    }
}

public class SummaryRow extends Row{
    private list<Row> childRows;
    public override double value{
        get{
            totalValue = 0;
            for(Row childRow:childRows){
                totalvalue += childRow.value;
            }
            return totalValue;
        }
    }
}

是否不支持此功能,或者我缺少什么?

Is this functionality not supported, or is there something that I am missing?

提前致谢.

推荐答案

不幸的是,据我所知,这是文档中的一个错误.我只能将 overridevirtual 修饰符应用于方法.当然,您可以通过手动编写属性 getter/setter 方法来获得所需的效果:

Unfortunately, as far as I know that is a mistake in the documentation. I've only been able to apply the override and virtual modifiers to methods. You can, of course, get the desired effect by manually writing your property getter/setter methods:

public abstract class TestRow {
    public Double value;

    public virtual Double getValue() {
        return value==null ? 0 : value;
    }

    public void setValue(Double value) {
        this.value = value;
    }
}

public class SummaryTestRow extends TestRow {
    private list<TestRow> childRows;

    public override Double getValue() {
        Double totalValue = 0;
        for(TestRow childRow : childRows){
            totalValue += childRow.value;
        }

        return totalValue;
    }
}

这篇关于覆盖 Salesforce Apex 中抽象类的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 20:59