1

If I have a class like this:

public abstract class ActionBase
{
    public abstract bool RunRemote();
    public abstract void RunLocal();
    public void Run()
    {
        if (RunRemote())
        {
            var connection = new SQLiteConnection("Data Source=Data.db;Version=3;");
            connection.Open();

            var cmd = new SQLiteCommand("UPDATE Actions SET Complete = 1 WHERE Id = @Id", connection);
            cmd.Parameters.Add(new SQLiteParameter("@Id", Id));
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            connection.Dispose();
        }
        RunLocal();
    }
}

What I want is to only expose Run() as public, but abstract and virtual cannot be marked as private. Is there a clean way of doing this (e.g. not using delegates etc.)

Thanks,

Joe

4

2 回答 2

6

你想要protected。这将允许基类或派生类访问方法。

protected abstract bool RunRemote();
protected abstract void RunLocal();
于 2013-10-07T21:34:22.457 回答
2
    public abstract class ActionBase
    {
        protected abstract bool RunRemote();
        protected abstract void RunLocal();

        public void Run()
        {
            if (RunRemote())
            {
                // ....
            }
            RunLocal();
        }
    }

或某事。

于 2013-10-07T21:38:07.503 回答