简体   繁体   English

扩展LinkedHashMap时出错

[英]Error extending LinkedHashMap

I am trying to extend LinkedHashMap from my Students class. 我正在尝试从我的学生类扩展LinkedHashMap By this I want to bring in all functionalities of a Map like Map.put(value) , Map.get(key) . 通过这种方式,我想引入Map的所有功能,例如Map.put(value)Map.get(key) I just create the object inside PSVM and not making static references but still I get the below error. 我只是在PSVM内创建对象,没有进行静态引用,但仍然收到以下错误。 Can someone point me as to what mistake I am committting here? 有人可以指出我在这里犯的错误吗? Also is there a better approach to achieve the task? 还有没有更好的方法来完成任务? Thanks in advance! 提前致谢!

import java.util.LinkedHashMap;

public class Students<Integer,String> extends LinkedHashMap<Integer,String> {

    public static void main(String args[]) {                     // line 5
        Students<Integer,String> students = new Students<>();    // line 6
        students.put(1, "s1");
        students.put(2, "s2");
        students.put(3, "s3");

        System.out.println(students.get(1));
    }

}

Error Message: 错误信息:

>> javac Students.java 
Students.java:5: error: non-static type variable String cannot be referenced from a static context
        public static void main(String args[]) {
                                ^
Students.java:6: error: non-static type variable Integer cannot be referenced from a static context
                Students<Integer,String> students = new Students<>();
                         ^
Students.java:6: error: non-static type variable String cannot be referenced from a static context
                Students<Integer,String> students = new Students<>();
                                 ^
Students.java:6: error: unexpected type
                Students<Integer,String> students = new Students<>();
                                                                ^
  required: class
  found:    <Integer,String>Students<Integer,String>
  where Integer,String are type-variables:
    Integer extends Object declared in class Students
    String extends Object declared in class Students
4 errors

By doing 通过做

class Student<Integer, String>

You have defined your own generic types Integer and String . 您已经定义了自己的通用类型IntegerString These are associated with the instance of the class Student however you are also attempting to use these new generic types in your main() method which is a static rather than an instance class and this is not allowed. 它们与Student类的实例相关联,但是您还尝试在main()方法中使用这些新的泛型类型,它是静态的而不是实例类,并且不允许这样做。 What you intended to use as java.lang.String 您打算用作java.lang.String

The simple solution is 简单的解决方案是

  • don't define you own generic types, you don't need to. 不需要定义自己的泛型类型,也不需要。
  • don't extend LinkedHashMap but rather use composition as this limits the methods you expose. 不要扩展LinkedHashMap ,而是使用组合,因为这会限制您公开的方法。

eg 例如

public class Students {
    private final Map<Integer, String> map = new LinkedHashMap();

    public void put(int num, String str) { map.put(num, str); }

    public String get(int num) { return map.get(num); }

    public static void main(String args[]) { 
        Students students = new Students(); 
        students.put(1, "s1");
        students.put(2, "s2");
        students.put(3, "s3");

        System.out.println(students.get(1));
    }
}

Remove the type variables on the class declaration: 删除类声明上的类型变量:

public class Students<Integer,String> extends LinkedHashMap<Integer,String>

should be 应该

public class Students extends LinkedHashMap<Integer,String>

You're actually declaring type variables whose names happen to be the same as Java classes, meaning that your type variables take precedence over the class names and hide them; 实际上,您是在声明类型变量,其名称恰好与Java类相同,这意味着您的类型变量优先于类名称并隐藏它们。 your class definition is currently semantically identical to: 您的类定义目前在语义上与:

public class Students<I, S> extends LinkedHashMap<I, S>

In

public class Students<Integer,String> extends LinkedHashMap<Integer,String> 

you are declaring generic type parameters called Integer and String , which hide standard Java class names. 您要声明称为IntegerString通用类型参数,它们会隐藏标准Java类名称。

If you want your Students class to support only java.lang.Integer keys and java.lang.String values, use : 如果您希望Students类仅支持java.lang.Integer键和java.lang.String值,请使用:

public class Students extends LinkedHashMap<Integer,String> 

but then you would declare and instantiate the class with : 但随后您将使用以下方法声明并实例化该类:

Students students = new Students();

since it will no longer have generic type parameters. 因为它将不再具有泛型类型参数。

If you want your Students class to be generic, use more standard names for the type parameters (which don't hide standard Java classes) : 如果您希望Students类是通用类,请为类型参数使用更多标准名称(不会隐藏标准Java类):

public class Students<K,V> extends LinkedHashMap<K,V> 

With the declaration 随着宣言

class Students<Integer,String> ...

you do not refer to the existent classes java.lang.Integer and java.lang.String but you declare your own type parameters for a generic Students class. 您没有引用现有的类java.lang.Integerjava.lang.String而是为通用的Students类声明了自己的类型参数

As your class already shall be a LinkedHashMap<Integer,String> simply remove those type parameters: 由于您的类已经是LinkedHashMap<Integer,String>只需删除这些类型参数即可:

class Students extends LinkedHashMap<Integer,String> ...

As a side note: It's a good habit to name your own type parameters for generic types with single uppercase letters, which also prevents such confusions. 附带说明:这是一个好习惯,即使用单个大写字母为泛型类型命名自己的类型参数,这也可以避免此类混淆。 Example: class Students<I, S> ... . 示例: class Students<I, S> ...

public class Students<T,E> extends LinkedHashMap<T,E> {

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

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