简体   繁体   English

泛型类中的编译错误

[英]compile error in generic class

why following code doesn't compile ? 为什么下面的代码不能编译?

class aa1 <String> {
public void fun(){
String s = ""; // not compiling
}
}
class aa2 <String> {
String s = "";  // not compiling
}
class aa3 <String> {
String s = (String)""; // compiling
}

can some tell or give me link for this thanks. 可以告诉或给我链接以表示感谢。

I think you may have misconstrued the idea behind generics. 我认为您可能误解了泛型背后的想法。 The point is that your class ( aa , in this case) can be... well, generic. 关键是您的类(在这种情况下为aa )可以是...好,通用的。 It's not of one fixed type, but rather can be any number of types through polymorphism. 它不是一种固定类型,而是通过多态性可以是任何数量的类型。 The generic type is similar to a variable name, but it represents a class, rather than an instance of a class. 泛型类型类似于变量名,但它表示类,而不是类的实例。 You could do something like: 您可以执行以下操作:

class aa <T> {
    public void fun(T myObject){
        T s = myObject;
    }
}

This is an appropriate use of generics. 这是泛型的适当用法。 T just represents "some class". T只是代表“某类”。 The reason the example you posted didn't compile because you overrode the visibility of String (making it the generic type, rather than java.lang.String ). 您发布的示例之所以无法编译,是因为您覆盖了String的可见性 (使其成为通用类型,而不是java.lang.String )。 If you don't want it to be any class, but some subset of classes, you can do something like: 如果您不希望它成为任何类,而是某些类的子集,则可以执行以下操作:

class aa <T extends MyInterface> {
    public void fun(T myObject){
        T s = myObject;
    }
}

In this case, you're guaranteed that instances of class T also extend the interface MyInterface . 在这种情况下,可以确保类T实例也扩展了接口MyInterface However, you cannot extend String , since it's final. 但是,您不能扩展String ,因为它是最终的。 If all you wanted was for the String object to be set, you do not need generics at all: 如果您只想设置String对象,则根本不需要泛型:

class aa {
    public void fun(){
        String s = "";
    }
}

For more information about generics, read over the Java tutorials on generics . 有关泛型的更多信息,请阅读有关泛型的Java教程

The generic parameter "String" hides the java.lang.String in this case. 在这种情况下,通用参数“ String”隐藏了java.lang.String。

in the class, if you declare the string s and specify its data type as "java.lang.String" then compiler won't complain. 在类中,如果您声明字符串s并将其数据类型指定为“ java.lang.String”,则编译器不会抱怨。

public class aa<String> {

    java.lang.String s = "" ;

}

Your code makes no sense - the declaration makes no sense - what are you really trying to do here? 您的代码没有意义-声明没有意义-您在这里真正要做什么?

The reason you're having issues compiling is that you're making a generics declaration that's hiding the JDK's java.lang.String. 编译时遇到问题的原因是,您正在做出隐藏JDK的java.lang.String的泛型声明。

You're also declaring the same class 3 times. 您还要声明3次相同的课程。 Something like this would compile, but still probably doesn't achieve what you want. 这样的东西可以编译,但是可能仍达不到您想要的效果。

class aa<NotString> {
    public void fun() {
        String s = ""; // compiles
    }
}

class aa2<NotString> {
    String s = "";  // compiles
}

class aa3<NotString> {
    String s = (String) ""; // compiling
}

Well, it seems that your generic parameter name ( String ) hides the java.lang.String type. 好吧,似乎您的通用参数名称( String )隐藏了java.lang.String类型。

I would recommend renaming your generic parameter. 我建议重命名您的通用参数。 By the way, which type do you want your s to be: java.lang.String or the same as the parameter? 顺便说一句,您希望s是哪种类型: java.lang.String或与参数相同? In the latter case, how cuold the compiler assign "" (which is of type java.lang.String ) to the potentially irrelevant type represented by the generic parameter? 在后一种情况下,编译器如何使"" (类型为java.lang.String )分配给由通用参数表示的可能不相关的类型?

In your example you are declaring String as an identifier. 在您的示例中,您将String声明为标识符。 You must not use keywords or type names. 您不得使用关键字或键入名称。 ( Generics Tutorial ). 泛型教程 )。

Since you're declaring the generic type String , that will override the default java.lang.String type, your code is like typing: 由于您声明的是通用类型String ,它将覆盖默认的java.lang.String类型,因此您的代码就像键入:

class aa<T> {
    T s = "";
}

T is simply Object after type erasure, but since it can be initialized with anything (for example Integer ), the compiler won't let you assign String s to it. T是类型擦除后的Object ,但是由于可以用任何东西(例如Integer )初始化,因此编译器不允许您将String分配给它。

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

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