简体   繁体   English

是否有在C#中创建匿名子类的语法?

[英]Is there a syntax for creating an anonymous subclass in C#?

Can I create instance of abstract class in C#/.net like in Java ? 我可以像在Java中一样在C#/ .net中创建抽象类的实例吗?

Additional Info 附加信息

I think a lot of us does not understand what do I mean? 我想我们很多人都不懂我的意思? So, In java I can create abstract class like this : 所以,在java中,我可以像这样创建抽象类:

Simple abstract class : 简单的抽象类:

/**
 * @author jitm
 * @version 0.1
 */
public abstract class TestAbstract
{
    public abstract void toDoSmth();
}

Code where I've create instance of abstract class 代码我在哪里创建抽象类的实例

/**
 * @author jitm
 * @version 0.1
 */
public class Main {
    public static void main(String[] args) {
        TestAbstract testAbstract = new TestAbstract() {
            @Override
            public void toDoSmth() {
                System.out.println("Call method toDoSmth");
            }
        };
    }
}

Can I to do in c# something like this ? 我可以用c#做这样的事吗?

Neither in Java nor in C# you can create an instance of an abstract class. 无论是在Java还是在C#中,您都可以创建抽象类的实例。 You will always need to create a concrete class that inherits from the abstract class. 您将始终需要创建一个继承自抽象类的具体类。 Java lets you do it without naming the class, using anonymous classes. Java允许您在不使用匿名类命名类的情况下执行此操作。 C# does not give you that option. C#没有给你这个选项。

(Edited to show a delegate as a replacement. I don't have access to VS here, so it may not compile, but this is the idea ) (编辑显示代表作为替代。我在这里没有访问VS,所以它可能无法编译,但这是个主意)

Usually in Java when you use an abstract class with a single abstract method (SAM) what you are really trying to achieve is to pass some code as a parameter. 通常在Java中使用带有单个抽象方法(SAM)的抽象类时,您真正想要实现的是将一些代码作为参数传递。 Let's say you need to sort an array of objects based on the class name, using Collections.sort(Collection, Comparator) (I know Comparator is an interface, but it is the same idea) Using an anonymous class to avoid extra typing, you can write something like 假设您需要根据类名对对象数组进行排序,使用Collections.sort(Collection,Comparator)(我知道Comparator是一个接口,但它是相同的想法)使用匿名类来避免额外的输入,你可以写类似的东西

   Comparator<Object> comparator = new Comparator<Object>{
        @Override
        public int compare(Object o1, Objecto2) {
            return o1.getClass().getSimpleName().compareTo(o2.getClass().getSimpleName()));
        }//I'm not checking for null for simplicity
   } 
   Collections.sort(list, comparator)

In C# 2.0 and beyond you can do pretty much the same using the Comparison<T> delegate. 在C#2.0及更高版本中,您可以使用Comparison<T>委托执行相同的操作。 A delegate can be thought as a function object, or in java words, a class with a single method. 委托可以被认为是一个函数对象,或者在java单词中,可以被认为是一个具有单个方法的类。 You don't even need to create a class, but only a method using the keyword delegate. 您甚至不需要创建类,只需要使用关键字delegate的方法。

Comparison<Object> comparison = delegate(Object o1, Object o2)
{
    return o1.class.Name.CompareTo(o2.class.Name);        
};

list.sort(comparison);

In C# 3.0 and beyond you can write even less code using lambdas and type inference: 在C#3.0及更高版本中,您可以使用lambdas和类型推断编写更少的代码:

list.sort((o1, o2) => o1.class.Name.CompareTo(o2.class.Name))

Anyway, if you are migrating code form java to c# you should read about delegates...in many of cases you will use delegates instead of anonymous classes. 无论如何,如果您要将代码从java迁移到c#,您应该阅读有关委托的内容......在许多情况下,您将使用委托而不是匿名类。 In your case, you are using a method void toDoSmth(). 在您的情况下,您使用的方法void toDoSmth()。 There is a delegate called Action which is pretty much the same thing, a method with no parameters and no return. 有一个名为Action的委托,它几乎是一回事,一个没有参数且没有返回的方法。

No. Not directly. 不,不是直接的。 The closest you can come (assuming you have the following class structure): 你最接近(假设你有以下类结构):

public abstract class AbstractClass
{
}

public class ChildClass : AbstractClass
{ 
}

Is: 方法是:

AbstractClass instance = new ChildClass();

在这种情况下,C#与Java相同,您无法实例化抽象类。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM