C# 抽象方法:内部公共和虚拟?

发布于 2024-09-28 14:29:43 字数 110 浏览 9 评论 0原文

C# 中的抽象方法在内部是公共的还是虚拟的?

默认情况下,所有方法都是私有的,如果抽象方法是私有的,则派生类将无法使用它,从而产生错误“虚拟或抽象成员不能是私有的”

Are abstract methods internally public and virtual in c#?

All methods are, by default, private and if an abstract method is private, it will not be available to derived class, yielding the error "virtual or abstract members cannot be private"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我的痛♀有谁懂 2024-10-05 14:29:43

我认为你问的问题与大多数人想象的不同(换句话说,你似乎理解 abstract 的含义)。

您不能声明私有抽象方法 - 编译器会发出错误。这两个类都不会编译:

class Foo
{
    private abstract void Bar();
}

class Baz
{
    // This one is implicitly private - just like any other 
    // method declared without an access modifier
    abstract void Bah();
}

编译器阻止您声明无用的方法,因为私有抽象成员不能在派生类中使用,并且对声明类没有实现(因此没有用处)。

需要注意的是,编译器应用于抽象成员的默认访问修饰符(如果您自己没有指定)仍然是 private,就像方法不是抽象的情况一样。

I think you are asking a different question than most people think (in other words it seems like you understand what abstract means).

You cannot declare a private abstract method - the compiler issues an error. Both of these classes will not compile:

class Foo
{
    private abstract void Bar();
}

class Baz
{
    // This one is implicitly private - just like any other 
    // method declared without an access modifier
    abstract void Bah();
}

The compiler is preventing you from declaring a useless method since a private abstract member cannot be used in a derived class and has no implementation (and therefore no use) to the declaring class.

It is important to note that the default access modifier applied to an abstract member by the compiler (if you do not specify one yourself) is still private just like it would be if the method was not abstract.

﹏半生如梦愿梦如真 2024-10-05 14:29:43

摘要只是一种表达方式:“我在这里,但还没人告诉我要做什么。”由于没有人实施该成员,所以必须有人这样做。为此,您必须继承该类并覆盖该成员。

为了能够覆盖某些内容,必须将其声明为abstractvirtual,并且必须至少可供继承者访问,即必须标记为protected、<代码>内部或<代码>公共。

Abstract is just a way to say: "I am here, but no one has told me what I'm going to do yet." And since no one has implemented that member yet someone must do that. To do that you have to inherit that class, and override that member.

To be able to override something it has to be declared either abstract or virtual, and must at least be accessible to the inheritor, i.e. must be marked protected, internal or public.

别再吹冷风 2024-10-05 14:29:43

抽象方法不能是私有的并且是虚拟的。他们至少必须受到保护。

Abstract methods cannot be private and are virtual. They must be at least protected.

七度光 2024-10-05 14:29:43

凭借 Jon Skeet 的论点(默认访问权限是什么C# 中的修饰符?)

C# 中所有内容的默认访问权限是“您可以为该成员声明的最受限制的访问权限”

它必须是“受保护的”

正如 Pieter 所指出的,默认值始终是私有的,因此:

abstract class Foo
{
    abstract void Bar(); 
} 

给出编译器错误

虚拟或抽象成员不能是私有的

By virtue of Jon Skeet's argument here (What are the Default Access Modifiers in C#?)

The default access for everything in C# is "the most restricted access you could declare for that member"

It must be "protected"

As pointed out by Pieter default is always private, so:

abstract class Foo
{
    abstract void Bar(); 
} 

Gives compiler error

virtual or abstract members cannot be private

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文