C# 静态类如何从对象派生?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/556203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 08:03:40  来源:igfitidea点击:

How can a static class derive from an object?

c#oop

提问by Ali

I am trying to inherit a non-static class by a static class.

我试图通过静态类继承非静态类。

public class foo
{ }

public static class bar : foo
{ }

And I get:

我得到:

Static class cannot derive from type. Static classes must derive from object.

静态类不能从类型派生。静态类必须从对象派生。

How can I derive it from object?

我怎样才能从对象中派生出来?

The code is in C#.

代码在 C# 中。

采纳答案by munificent

There's no value in deriving static classes. The reasons to use inheritance are:

派生静态类没有任何价值。使用继承的原因是:

  • Polymorphism
  • Code reuse
  • 多态性
  • 代码重用

You can't get polymorphism with static classes, obviously, because there is no instance to dynamically dispatch on (in other words, it's not like you can pass a Bar to a function expecting a Foo, since you don't havea Bar).

你不能用静态类获得的多态性,显然,因为没有实例,以动态地调度上(换句话说,它不是像你可以传递一个酒吧,以期待一个Foo的功能,因为你不做一个酒吧) .

Code reuse is easily solved using composition: give Bar a static instance of Foo.

使用组合可以轻松解决代码重用问题:给 Bar 一个 Foo 的静态实例。

回答by Svish

Doesn't all classes (static included) already derive from object? As in, by default?

不是所有的类(包括静态的)都已经从对象派生了吗?例如,默认情况下?

Also, like it says, "Static class cannot derive from type.", so I don't think what you are doing is possible. Why would you want a static class to derive from a type anyways?

另外,就像它说的那样,“静态类不能从类型派生。”,所以我认为你在做什么是不可能的。你为什么要从一个类型派生一个静态类呢?

回答by Grzenio

It can't. You have to create a normal class to derive from some other class.

它不能。您必须创建一个普通类以从其他类派生。

回答by RSlaughter

I don't think C# supports inheritance for static classes.

我认为 C# 不支持静态类的继承。

One option would be to use the singleton pattern instead

一种选择是改用单例模式

public class foo
{ }

public class bar : foo
{
    private bar instance;
    public bar GetInstance()
    {
        if(instance == null) instance = new bar();
        return instance;
    }

    private bar(){} //make default constructor private to prevent instantiation 
}

回答by GeekyMonkey

The error message is bogus. It's not saying "an" object. It's talking about the built-in type called "object" which is the base of everything in .NET.

错误信息是假的。它不是说“一个”对象。它谈论的是称为“对象”的内置类型,它是 .NET 中一切事物的基础。

It should say "static classes can not specify a base type".

它应该说“静态类不能指定基类型”。

回答by Anton Gogolev

All classes derive implicitly from Object. That said, although static classes (which by definition are just containers for static members) "derive" from object, there is nothing you can get from that fact.

所有类都隐式派生自Object. 也就是说,尽管静态类(根据定义只是静态成员的容器)从对象“派生”,但您无法从该事实中获得任何信息。

回答by teedyay

The error message is misleading.

错误消息具有误导性。

bar can't inherit from foo because foo can be instantiated and bar can't.

bar 不能从 foo 继承,因为 foo 可以实例化而 bar 不能。

回答by Jon Skeet

From the C# 3.0 specification, section 10.1.1.3:

来自 C# 3.0 规范的 10.1.1.3 节:

A static class may not include a class-basespecification (§10.1.4) and cannot explicitly specify a base class or a list of implemented interfaces. A static class implicitly inherits from type object.

静态类可能不包含基于 类的规范(第 10.1.4 节),并且不能显式指定基类或已实现接口的列表。静态类隐式继承自 type object

In other words, you can't do this.

换句话说,你不能这样做。

回答by Christopher McAtackney

Taken from http://msdn.microsoft.com/en-us/library/79b3xss3(VS.80).aspx

取自http://msdn.microsoft.com/en-us/library/79b3xss3(VS.80).aspx

The main features of a static class are:

They only contain static members.

They cannot be instantiated.

They are sealed.

They cannot contain Instance Constructors (C# Programming Guide).

静态类的主要特点是:

它们只包含静态成员。

它们不能被实例化。

它们是密封的。

它们不能包含实例构造函数(C# 编程指南)。

So, inheriting from a non-static class violates the first feature of static classes on this list by introducing non-static members to your static class.

因此,通过向静态类引入非静态成员,从非静态类继承违反了此列表中静态类的第一个特性。

回答by RS Conley

Like stated earlier the C# spec say this can't be done. You can't implement a interface with static classes either. Your best bet is to change from using a static class to using a class that uses the singleton pattern. You will have only one instance (similar to how the static class works) and you will be able to inherit behavior or implement interfaces.

如前所述,C# 规范说这是不可能的。您也不能使用静态类实现接口。最好的办法是从使用静态类更改为使用使用单例模式的类。您将只有一个实例(类似于静态类的工作方式)并且您将能够继承行为或实现接口。

You read up on Singletons here, here, and here.

您可以在此处此处此处阅读有关单身人士的信息

回答by Mark Rendle

If you're trying to stop people creating instances of the class, just add a private default constructor.

如果您试图阻止人们创建类的实例,只需添加一个私有的默认构造函数。