简体   繁体   English

如何在一个类中同时具有抽象方法和虚拟方法?

[英]How can I have both abstract and virtual methods in one class?

In the following parent class SqlStatement , how can I make Initialize() abstract but keep Execute() virtual ? 在下面的父类SqlStatement中 ,如何使Initialize() 抽象,但保持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...";
        }
    }
}

使其成为抽象类

Can't you just declare it as abstract? 您不能只是将其声明为抽象吗?

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为抽象。

public abstract class SqlStatement {
...
protected abstract void Initialize(); //abstract
....
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果我有两个接口,那么一个类可以继承两个接口吗? - If I have two interfaces, can one class inherit both? 我如何拥有一个可以是具有相同方法的任何类的对象? - How can I have one object that can be any class with the same methods? 2接口(i1,i2),2个抽象类(A:i1,B:i2),一个类(摘要:i1,i2)。 如何访问抽象类的所有方法 - 2 Interface(i1, i2), 2 abstract class(A:i1, B:i2), one class(Summary :i1,i2). How to access all methods of abstract class 仅具有虚拟抽象方法的接口和抽象 class 是同一件事吗? - Is an interface and an abstract class with just virtual abstract methods the same thing? 接口继承-从父接口隐藏方法-为什么在实现类中可以同时使用这两种方法? - Interface inheritance - hiding method from parent interface - why can I have both methods in the implementing class? 为什么我不能在 C# 中使用抽象静态方法? - Why can't I have abstract static methods in C#? 如何在C#中从两个抽象方法中仅实现抽象类中的一个方法? - How to to implementing only one methods from abstract class out of two abstract methods in C#? 如何正确使用抽象方法和虚方法? - How to correctly use abstract and virtual methods? 我怎样才能拥有一个实现接口并继承自抽象 class 的 class? (C# .NET 3.5) - How can I have a class that implements an interface and inherits from an abstract class? (C# .NET 3.5) 抽象方法是虚拟的吗? - Are abstract methods virtual?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM