类不能继承和实例化 [英] Class can not inherit and instantiate

查看:130
本文介绍了类不能继承和实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,但我想限制不继承。所以我可以使用密封关键字和

不实例化我可以使用抽象关键字,但两个关键字一次不可用如何处理这些scnerio?

解决方案

唯一一个永远不会被你的代码直接实例化的类是一个'静态类,当然第一次有引用时,'静态类将创建。并且,你不能继承'静态类。



这符合你的要求吗?



Singleton Class将由您实例化一次;你可以标记它'密封以防止从它继承。并且,请注意,可以使用反射来调用Singleton的私有构造函数:虽然很难想象有人疯狂到想要这样做:)。



Singleton的善是它可以实现接口,可以从另一个类派生,可以在委托中传递。有些人认为单身人士是邪恶的,但是,由于他们几乎虔诚地强烈关注。



抽象类是设计的继承自;它们无法实例化,或者密封了access-modifier;他们可以从另一个类继承,包括另一个'抽象类,并且不能实现接口。



你可以疯狂地实现各种继承方案,比如这个恐怖:

  public   interface   Interface1  
{
string theString {set;得到; }
}

public interface Interface2
{
int theInteger {set;得到; }
}

public class Class1 Interface1
{
public string theString {set;得到; }

public Class1()
{
theString =hello0;
}
}

public abstract class abstractClass1 Class1 Interface2
{
public string theString {set;得到; }

public int theInteger {set;得到; }

public abstractClass1()
{
theString =hello1;
}
}

public abstract class abstractClass2 abstractClass1
{
public string theString {set;得到; }

public abstractClass2()
{
theString =hello2;

theInteger = 108;
}
}

public Class3 abstractClass2
{
public Class3()
{
theString =hello3;

theInteger = 0;
}
}

// 在某些方法中尝试此操作,或者在EventHandler中尝试:
Class3 class3 = new Class3();

string str = class3 .theString;

int i = class3 .theInteger;

但是,为什么要实现这种奇怪的关系呢?


你正在寻找一个单身人士 - 这是一个过度使用的设计模式,你应该避免它,但如果你坚持:

http://csharpindepth.com/Articles/General/Singleton.aspx

I have a class but i want to restrict not to inherit.so i can use sealed keyword and
not to instantiate i can use abstract keyword but both keywords are not useable at a time how to handle these scnerio?

解决方案

The only type of Class that will never be instantiated directly by your code is a 'static Class, of course a 'static class will be created the first time there is a reference to it. And, you cannot inherit from a 'static Class.

Does that meet your requirements ?

The Singleton Class will be instantiated by you, once; you can mark it 'Sealed to prevent inheritance from it. And, note that one could use reflection to call the private constructor of a Singleton: although it's hard to imagine anybody crazy enough to want to do that :).

The "goodness" of a Singleton is that it can implement Interfaces, can be derived from another Class, could be passed in a delegate. There are those who consider Singletons as "evil," however, for reasons they are almost "religiously" intense about.

Abstract Classes are designed to be inherited from; they cannot be instantiated, or have the access-modifier 'sealed; they can inherit from another class, including another 'abstract Class, and cannot implement Interfaces.

You can drive yourself crazy implementing all kinds of inheritance scenarios, like this horror:

public interface Interface1
{
    string theString { set; get; }
}

public interface Interface2
{
    int theInteger { set; get; }
}

public class Class1 : Interface1
{
    public string theString { set; get; }

    public Class1()
    {
        theString = "hello0";
    }
}

public abstract class abstractClass1 : Class1, Interface2
{
    public string theString { set; get; }

    public int theInteger { set; get; }

    public abstractClass1()
    {
        theString = "hello1";
    }
}

public abstract class abstractClass2 : abstractClass1
{
    public string theString { set; get; }

    public abstractClass2()
    {
        theString = "hello2";

        theInteger = 108;
    }
}

public class Class3 : abstractClass2
{
    public Class3()
    {
        theString = "hello3";

        theInteger = 0;
    }
}

// try this in some method, or EventHandler:
Class3 class3 = new Class3();

string str = class3.theString;

int i = class3.theInteger;

But, why would you ever implement such strange relationships ?


You are looking for a Singleton -- it's an overused Design Pattern, and you should probably avoid it, but if you insist:
http://csharpindepth.com/Articles/General/Singleton.aspx


这篇关于类不能继承和实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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