简体   繁体   中英

c# Is there a way that my base class and derived class can implement Interface methods separately?

I know the question is confusing, but it is hard to think of a good title for this problem, so I'll describe more here.

I have an interface A with 2 methods, IsValidRow() and IsFileValid() I have a base class B and a derived class C The goal here is that I want the base class to implement IsFileValid() , so it can be inherited by all the classes derived off the base class and each derived class to implement IsValidRow() .

The problem is that IsFileValid() calls IsValidRow() inside. If I do

B:A

A requires IsValidRow() to be implemented in B.

Currently my derived class C inherits from the base class and the interface atm.

I don't mind restructuring everything as long as the requirements for the 2 methods are fulfilled (One will be implemented once in a base class or something and inherit across, while the other one will be implemented in each derived class)

interface IFileValidator
{
    Pair<bool, string> IsValidRow(List<string> row);

    bool IsFileValid(string file);
}

class FileValidator : IFileValidator
{
    public bool IsFileValid(string file)
    {
        // calls IsValidRow()
        IsValidRow();
    }
 }

class FacilitiesCalendarValidator : FileValidator, IFileValidator
{

    public Pair<bool, string> IsValidRow(List<string> row)
    {
            //stuff
    }
 }

It is possible for both cases.

For optionally overridable methods, declare the base class methods as virtual and override in the child class.

For methods that must have implementation provided by a child class, mark the class and the method as abstract .

You don't have to override a virtual method in a child class so the implementation is inherited unless you explicitly decide to override .

For example:

 interface IInterface
 {
     int Transform(int a, int b);
     int AnotherTransform(int a, int b);
 }

 abstract class A : IInterface
 {
    public virtual int Transform(int a, int b)
    {
        return a + b;
    }

    public abstract int AnotherTransform(int a, int b);
 }

 class B : A
 {
    public override int Transform(int a, int b)
    {
        return a - b;
    }

    public override int AnotherTransform(int a, int b)
    {
       return a * b;
    }
 }

You can mark the method IsValidRow() as abstract :

namespace test
{
    interface A
    {
        bool IsFileValue();
        bool IsValidRow();
    }

    abstract class B : A
    {
        public bool IsFileValue()
        {
            return IsValidRow();
        }

        public abstract bool IsValidRow();
    }

    class C : B
    {
        public override bool IsValidRow()
        {
            return true;
        }
    }
}

I have a solution for you is to use Interface Segregation principle . what does that mean is to declare two interfaces . The first one have one method which is IsFileValid() and the second interface implement the first interface and has another method which is IsFileValid() like this

  Interface A { void IsFileValid(); }
  Interface B : A { void IsValidRow();}

and the base class implement interface A and all the derived classes implement the interface B like this.

public class Base : A
{
      void IsFileValid(){ // immplementaion details}
}

public class Child : Base, B
{
      void IsValidRow(){// immplementaion details}    
}

Happy Coding

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