关于C#中的抽象类及其继承 [英] About abstract class and its inheritance in C#

查看:224
本文介绍了关于C#中的抽象类及其继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽象类说员工,它包含几个方法,其中一些是抽象的,一些非抽象方法,现在我的子类说管理器继承了这个抽象类,但只实现了一些抽象方法,并非所有.do我们需要声明这个子类是否抽象?如果子类只想实现父类的几个抽象方法呢?请详细回答。



我尝试过:


$ b $毕竟刚想过它所以发布了它。新手学习c#

解决方案

为什么你没有做一个小测试课,看看会发生什么?



像这样的东西,专注于评论方法:

  public  摘要  BaseAbstract 
{
public BaseAbstract( ){}
public abstract void Method();
}

public class 派生:BaseAbstract
{
public Derived(){}

// < span class =code-comment>尝试使用commentet方法进行编译并取消注释。
/ *
public override void Method()
{
}
* /

}





结论:派生自抽象类的类需要实现所有抽象方法,除非派生类也是抽象的。



您可以在此处找到一些易于阅读和非常简短的信息: 10.5.6摘要方法(C#) [ ^ ]

我希望它有所帮助。

i have a abstract class say employee and it contains several methods some of them are abstract and some non-abstract methods and now my child class say manager inherits this abstract class but only implements few abstract methods not all .do we need to declare this child class as abstract or not? what if child class want to implement only few abstract methods of parent class? please answer in a detailed way .

What I have tried:

i just have had thought about it so posted it . newbie learning c#

解决方案

And why you don't made a small test class and see what happens?

Something like this, Focus on the commented method:

public abstract class BaseAbstract
{
    public BaseAbstract() { }
    public abstract void Method();
}

public class Derived : BaseAbstract
{
    public Derived() { }

    // Try to compile with commentet "Method" and uncommented.
    /*
    public override void Method()
    {
    }
    */
}



Conclusion: Classes derived from an abstract class need to implement all abstract methods, unless the derived class is also abstract.

Some easy to read and very short information you find here: 10.5.6 Abstract methods (C#)[^]
I hope it helps.


这篇关于关于C#中的抽象类及其继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆