解决 继承方法问题

约翰007

新成员
已加入
2021年1月19日
留言内容
2
编程经验
5-10
你好

我有两个类ClassA和ClassB。 ClassB继承自ClassA。我希望ClassA中的Sub1处理某些情况,但是对于其他情况,我想让它留给ClassB中的Sub2处理。我不确定如何设置。我的基本代码在下面,但显然不正确。任何帮助,将不胜感激。

谢谢

问候

C#:
    public class ClassA
    {
        public string xyz = "abc";
        虚空 Sub0()
        {
            Sub1(xyz);
        }

        public 虚空 Sub1(string xyz)
        {
            if (true)
            {
                //Handle it here if condition is true else leave it for Sub2 in ClassB to handle
            }
            else
            {
                //Let it handled in Sub2 in ClassB
                Sub2(xyz);
            }
        }

        public abstract 虚空 Sub2(string xyz);
        // I am getting this on above line; 'ClassA.Sub2(string)' is abstract but it is contained in non-abstract type 'ClassA'
        // How can I fix it? I don't want to make ClassA abstract
    }

    class ClassB : ClassA
    {
        public override 虚空 Sub2(string xyz)
        {
            // Handle it here
        }
    }
 
Last edited:
Solution
那么,您是否要直接实例化基类?如果是这样,请执行以下操作:
C#:
public class BaseClass
{
    public 虚空 Method1()
    {
        if (someBooleanExpression)
        {
            // ...
        }
        else
        {
            Method2();
        }
    }

    public virtual 虚空 Method2()
    {
        throw new InvalidOperationException("This method should be invoked on derived classes only.");
    }
}

public class DerivedClass : BaseClass
{
    public override 虚空 Method2()
    {
        // ...
    }
}
如果没有,请执行以下操作:
C#:
public abstract class BaseClass
{
    public 虚空 Method1()
    {
        if (someBooleanExpression)
        {
            // ...
        }
        else
        {...

金西尼

C#论坛主持人
工作人员
已加入
2011年4月23日
留言内容
3,498
地点
悉尼,澳大利亚
编程经验
10+
ClassB继承自ClassA。
No it's not. That terminology is wrong. ClassB inherits ClassA and ClassB is derived from ClassA. A member declared in ClassA and not overridden in ClassB is inherited from ClassA.
 

金西尼

C#论坛主持人
工作人员
已加入
2011年4月23日
留言内容
3,498
地点
悉尼,澳大利亚
编程经验
10+
An abstract method is one that you declare but don't implement. Because an abstract member is not implemented, you cannot create an instance of that class directly. For that reason, the class must be declared abstract as well. If you want to be able to instantiate the base class directly then it cannot be declared abstract which means that the method cannot be declared abstract either. In that case, you'd have to declare it virtual and provide some sort of default implementation. If that method is not supposed to be called on the base class then that implementation should throw an InvalidOperationException.
 

金西尼

C#论坛主持人
工作人员
已加入
2011年4月23日
留言内容
3,498
地点
悉尼,澳大利亚
编程经验
10+
那么,您是否要直接实例化基类?如果是这样,请执行以下操作:
C#:
public class BaseClass
{
    public 虚空 Method1()
    {
        if (someBooleanExpression)
        {
            // ...
        }
        else
        {
            Method2();
        }
    }

    public virtual 虚空 Method2()
    {
        throw new InvalidOperationException("This method should be invoked on derived classes only.");
    }
}

public class DerivedClass : BaseClass
{
    public override 虚空 Method2()
    {
        // ...
    }
}
如果没有,请执行以下操作:
C#:
public abstract class BaseClass
{
    public 虚空 Method1()
    {
        if (someBooleanExpression)
        {
            // ...
        }
        else
        {
            Method2();
        }
    }

    public abstract 虚空 Method2();
}

public class DerivedClass : BaseClass
{
    public override 虚空 Method2()
    {
        // ...
    }
}
 
最佳 底部