简体   繁体   中英

Get property into abstract class

Here's what I have right now:

public abstract class SampleClass
{
    public abstract void ShowAll();
    public abstract void ExecuteById(int id);
    public string sampleString{get;}
}

public sealed class FirstClass : SampleClass
{
    public override string sampleString
    {
        get { return "AA"; }
    }


    public override void ShowAll()
    {
        ......
    }

    public override void ExecuteById(int id)
    {
        ......
    }
}

public sealed class SecondClass : SampleClass
{
    public override string sampleString
    {
        get { return "BB"; }
    }

    public override void ShowAll()
    {
    ......
    }

    public override void ExecuteById(int id)
    {
    ......
    }
}

Now I add a function like this to each of the classes

public void runStoredProcedure(string sampleString)
{
    SQLClass.ExecuteStoredProcedure(sampleString)
}

One of the ways of doing that is to just add this as an abstract to sampleClass and as an ovverride to FirstClass and SecondClass. Altough the only difference is the passing parameter (samplestring).

The question: is it possible to place this function somewhere(probably in SampleClass) and pass overriden string to this function from First/Second classes to avoid overhead?

Yes. What you usually want to do is declare the things that children must define as abstract (so the inheriting classes have to override them). Properties can be abstract too:

public abstract class MyBase {
    public void Execute() {
        Console.WriteLine(MyString);
    }

    // Notice that this has no body, because it is abstract.  Also, I don't need a setter.
    protected abstract String MyString { get; } 
}

public class MyChild : MyBase {
    protected String MyString { get { return "Foo"; } }
}

public class MyOtherChild : MyBase {
    protected String MyString { get { return "Bar"; } }
}

then you can do:

var myChild = new MyChild();

// Prints "Foo"
myChild.Execute();

var myOtherChild = new MyOtherChild();

// Prints "Bar"
myOtherChild.Execute();

Sure. That is the point of an abstract class.

Change your access modifier on runStoredProcedure (in the base class) to protected. that way you can access it from your derived classes.

protected void runStoredProcedure(string sampleString)
{
    SQLClass.ExecuteStoredProcedure(sampleString)
}

Ok I think I know what you are saying so let me restate just to be clear.

Base Class A has PropertyA Sub Class B overrides PropertyA Sub Class C overrides PropertyA

You want to be able to call the same Method from both B and C passing the correct value from Property A based on the class?

Inside your Base Class A add your method

MyMethod(string value)
{
    // Do Stuff
}

Inside your Sub Class

MyMethod(this.PropertyA);

That being said why are you attempting to do this? When you inherit from the base class the Property A belongs to the SubClass and you can set it to whatever value you want inside the subclass. In cases like this I usually set the values in the Sub Class or use an interface.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM