define interface method with different parameters in C#

后端 未结 4 1081
生来不讨喜
生来不讨喜 2021-01-18 14:23
interface parentInterface
{
   public String methodA(/*define parameters name and dataType*/);
}

and

public class childA : parentIn         


        
4条回答
  •  一整个雨季
    2021-01-18 15:17

    Make a New Parameter

    This can often be solved by using a class or struct to use as single parameter rather than the built-in Types.

    The Interface

    You know what to expect from a class when it implements a familiar interface. We know that all classes implementing the IEnumerable interface can be used in a foreach loop. By convention, the name of the interface is "I" followed by a description of an ability. It is typical for the name to end with the suffix "-able".

    -able   Suffix forming adjectives meaning:
               1   -able to be [as in] calculable.
               2   -having the quality of [as in] comfortable.

    Oxford English Dictionary

    Let's rename parentInterface and MethodA() to give a clear example of how this normally works (and to avoid negative sanctions):

    public interface ITreatable
    {
        Treatment GetTreatment();
    }
    

    Well, finding the cure may not be so easy, even if the object represents a treatable illness. Here's some examples:

    public class TheFlu : ITreatable
    {
        public Treatment GetTreatment(int year)
        {
            // return some object, Treatment, based on the flu season.
        }
    }
    
    public class Hangover : ITreatable
    {
        public Treatment GetTreatment()
        {
            return Treatment.Empty; // no parameters necessary.
        }
    }
    
    public class Insomnia : ITreatable
    {
        public Treatment GetTreatment(FamilyHistory occurances, LabResult lab)
        {
            // return Some Treatment object that can be different based on the
            // calculated risk from the arguments.
        }
    }
    

    What We're Really Missing Here

    I don't know biology, but the concept is still the same. You have a group of ITreatable illness objects that need to have a GetTreatment() method; however, they use different criteria for making calculations. We need Symptoms.

    public class Symptoms
    {
        public FamilyHistory History;
        public DateTime Time;
        public LabResult Lab;
        public BloodTest BloodTest;
        public TimeSpan SymptomTime;
        public IsCritical IsCritical;
    }
    

    Now, the objects can parse the symptoms in their own method, and our interface will look like this:

    public interface ITreatable
    {
        Treatment GetTreatment(Symptoms symptoms);
    }
    

提交回复
热议问题