简体   繁体   中英

Static method, Abstract method, Interface method comparison?

When I choose these methods? I can not decide which one I must prefer or when will i use one of them? which one give best performance?

First Type Usage

public abstract class _AccessorForSQL
{
   public virtual bool Save(string sp, ListDictionary ld, CommandType cmdType);
   public virtual bool Update();
   public virtual bool Delete();
   public virtual DataSet Select();
}

class GenAccessor : _AccessorForSQL
{
    DataSet ds;
    DataTable dt;
    public override bool Save(string sp, ListDictionary ld, CommandType cmdType)
    {
    }

    public override bool Update()
    {
        return true;
    }

    public override bool Delete()
    {
        return true;
    }

    public override DataSet Select()
    {
        DataSet dst = new DataSet();
        return dst;
    }
}

Second Type Usage

Also I can write it below codes:

public class GenAccessor
{
     public Static bool Save()
     { 
     }

     public Static bool Update()
     {
     }

     public Static bool Delete()
     {
     }
}

Third Type Usage

Also I can write it below codes:

public interface IAccessorForSQL
{
    bool Delete();
    bool Save(string sp, ListDictionary ld, CommandType cmdType);
    DataSet Select();
    bool Update();
}

public class _AccessorForSQL : IAccessorForSQL
{
    private DataSet ds;
    private DataTable dt;

    public virtual bool Save(string sp, ListDictionary ld, CommandType cmdType)
    {            
    }
}

I can use first one below usage:

GenAccessor gen = New GenAccessor();
gen.Save();

I can use second one below usage:

GenAccessor.Save();

Which one do you prefer? When will I use them? which time i need override method? which time I need static method?

static methods are for methods which are independent of object state. Typically I would use them for utility methods and pure mathematical kind of functions. eg computeAverage(int[] values);

abstract/interface methods are pretty much the same thing. interface methods have the feel of pure contract. abstract methods are more version tolerant. If you have a contract and it can possibly have different implementations I would go with these.

static methods are more performant because they don't need to do virtual table lookup.

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