【问题标题】:How can I have both abstract and virtual methods in one class?如何在一个类中同时拥有抽象方法和虚拟方法?
【发布时间】:2010-12-23 06:13:01
【问题描述】:

在下面的父类SqlStatement中,如何让Initialize() abstract但保持Execute() > 虚拟

using System;
using System.Collections.Generic;

namespace TestSql28374
{
    class Program
    {
        static void Main(string[] args)
        {
            object item = new object();
            List<string> properties = new List<string>();

            SqlCreateStatement sqlCreateStatement = new SqlCreateStatement(properties);
            sqlCreateStatement.Execute();

            SqlInsertStatement sqlInsertStatement = new SqlInsertStatement(item, properties);
            sqlInsertStatement.Execute();

            Console.ReadLine();
        }
    }

    public class SqlStatement
    {
        protected List<string> properties;
        protected object item;
        protected string sql;

        public SqlStatement(List<string> properties) 
        {
            this.properties = properties;
        }

        protected virtual void Initialize() //should be abstract
        { }

        public virtual void Execute()
        {
            Console.WriteLine("Sending to database: " + sql);
        }
    }

    public class SqlCreateStatement : SqlStatement
    {

        public SqlCreateStatement(List<string> properties)
            : base(properties)
        {
            Initialize();
        }

        protected override void Initialize()
        {
            sql = "CREATE TABLE...";
        }
    }

    public class SqlInsertStatement : SqlStatement
    {
        public SqlInsertStatement(object item, List<string> properties)
            : base(properties)
        {
            this.item = item;

            Initialize();
        }

        protected override void Initialize()
        {
            sql = "INSERT INTO...";
        }
    }
}

【问题讨论】:

    标签: c# oop virtual abstract-class


    【解决方案1】:
    public abstract class SqlStatement {
    ...
    protected abstract void Initialize(); //abstract
    ....
    }
    

    【讨论】:

      【解决方案2】:

      你不能把它声明为抽象的吗?

      public abstract class SqlStatement
          {
              protected List<string> properties;
              protected object item;
              protected string sql;
      
              public SqlStatement(List<string> properties) 
              {
                  this.properties = properties;
              }
      
              protected abstract void Initialize();
      
              public virtual void Execute()
              {
                  Console.WriteLine("Sending to database: " + sql);
              }
          }
      

      【讨论】:

      • 如何在继承链中运行 SqlStatement::Execute?
      • 取决于?你的意思是立即还是在祖先中?立即调用 Execute,在你需要 base.Execute 的祖先中。
      • 是的,虚拟调度无法到达 SqlStatatement::Execute
      【解决方案3】:

      将 SqlStatement 声明为抽象。

      【讨论】:

        【解决方案4】:

        把它变成一个抽象类

        【讨论】:

        • 没有接口阻止实体。摘要允许两者。
        • 不,这就是它的乐趣! :)
        • 是的,在我问这个问题之前,我认为抽象类也不能有方法体,但它有效,谢谢!
        猜你喜欢
        • 2021-10-18
        • 1970-01-01
        • 2010-10-11
        • 2011-10-25
        • 2015-06-13
        • 2014-03-18
        • 2011-07-18
        • 2023-03-15
        • 1970-01-01
        相关资源
        最近更新 更多