简体   繁体   English

班级名称 <Type> 或ClassName <Object> 用于抽象基类

[英]ClassName<Type> or ClassName<Object> for abstract base class

I'm relatively newbie to C# programming. 我是C#编程的新手。 I want to create Abstract Base Class that contains a few of abstract , and virtual methods. 我想创建一个包含一些abstractvirtual方法的Abstract Base Class

By the following two examples, what are their differences of usage and which is more commonly used? 通过以下两个示例,它们在用法上有什么区别,哪个更常用?

Example-1, use Type keyword. 示例1,使用Type关键字。

public abstract class DecoratorBase<Type> : ValidatableBindableBase
{
    public virtual void SetFields(Type T) { }
    public virtual void SetFieldsBack(ref Type T) { }
}

Example-2, use Object keyword. 示例2,使用Object关键字。

public abstract class DecoratorBase<Object> : ValidatableBindableBase
{
    public virtual void SetFields(Object T) { }
    public virtual void SetFieldsBack(ref Object T) { }
}

In my case, Type or Object will represent my business model POCO class. 就我而言, TypeObject将代表我的业务模型POCO类。

Sorry, if this question sound so naive to you. 抱歉,这个问题对您来说太幼稚了。 Thanks. 谢谢。

Please, don't do that! 拜托, 不要那样做!

It looks like you're a little confused about generics. 您似乎对泛型有些困惑。 When you define a generic abstract type (using < and > after the name), you're telling the compiler "I will give you some type, but I don't know which type just now. Use the name I'm giving you as a placeholder for the type later." 定义通用抽象类型时(在名称后使用<> ),您会告诉编译器“我会给您一些类型,但我现在不知道是哪种类型。请使用我为您提供的名称稍后用作该类型的占位符。” The convention is to use the upper case T . 惯例是使用大写T

For example, the following defines a generic abstract class. 例如,以下定义了一个通用抽象类。

public abstract class SomeClass<T> {}

Later, when we create a derived class, we create it with a type: 稍后,当我们创建派生类时,我们使用以下类型创建它:

public class ConcreteClass : SomeClass<int> {}

If we had methods declared in our abstract class that used the type T , our new ConcreteClass would replace references to T with int . 如果我们在抽象类中声明了使用T类型的方法,则新的ConcreteClass将用int替换对T引用。 So in your case, you should do: 因此,在您的情况下,您应该执行以下操作:

public abstract class DecoratorBase<T> : ValidatableBindableBase
{
    public virtual void SetFields(T param) { }
    public virtual void SetFieldsBack(ref T param) { }
}

You can see here, T represents the unknown type as T , instead of naming the type Type and the parameter T . 您可以在这里看到, T将未知类型表示为T ,而不是命名类型Type和参数T What you were doing is known as 'hiding', that is, When you use Type or Object in the way you were doing, it is assigning a new meaning to those words. 什么做的是被称为“隐藏”,也就是说,当你使用TypeObject在你正在做的方式,它被赋予了新的含义,这些话。 You should absolutely avoid hiding in this way. 您应该绝对避免以这种方式隐藏。

Now if you create a concrete class, say: 现在,如果您创建一个具体的类,请说:

public class Decorator : DecoratorBase<Control>
{
    public override SetFields(Control param) {}
}

You have type safety: the parameter now has to be a Control , at least for this concrete implementation. 你有类型安全:该参数现在已经是一个Control ,至少在这个具体实施。 You can define other implementations with different types, and inherit the same base methods. 您可以定义具有不同类型的其他实现,并继承相同的基本方法。

See this question for a simple understanding of how to use generics. 这个问题对于如何使用泛型一个简单的了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .Net 4.0 无法将类型为“System.Collections.Generic.List`1[ClassName]”的 object 转换为类型“ClassName” - .Net 4.0 Unable to cast object of type 'System.Collections.Generic.List`1[ClassName]' to type 'ClassName' 以类名作为返回类型的方法 - Methods with classname as a return type System.InvalidCastException: &#39;无法将类型为 &#39;ClassName&#39; 的对象转换为类型 InterfaceName&#39;。 - System.InvalidCastException: 'Unable to cast object of type 'ClassName' to type InterfaceName'.' 从类名确定包含一个类的程序集(不是类型的实例) - Determine assembly that contains a class from the classname (not an instance of a type) 使用模板和/或typeof()实例化抽象基类类型的对象 - Instantiate an Object of Abstract Base Class type using Templates and/or typeof() img类的C#类名 - C# Classname of an img class 了解对象定义:className objName =(className)objName.methodName() - understanding of object definition: className objName = (className) objName.methodName() 如何确定类型是否为抽象类的基类 - How to determine if type is a base class of an abstract class new()和抽象基类的泛型类型约束 - Generic type constraint of new() and an abstract base class 在抽象基类中使用具体实现的类型 - Use type of concrete implementation in abstract base class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM