如何在嵌套类中调用泛型?初始化泛型属性报错求解
Let's break down the problems in your code and walk through how to fix them step by step:
1. First, Fix the Constructor Name Mistake
Your code has a public Link() constructor, but that's actually supposed to be the constructor for the outer LinkedListDeque class. Constructor names must exactly match the class name, so it should be public LinkedListDeque(). This syntax error was adding to your confusion.
2. The Core Generic Type Mismatch
The main error you're hitting comes from passing 1 (an int) to the Link constructor's unknown_type b parameter. Since LinkedListDeque is a generic class, unknown_type is a placeholder that gets replaced with a specific type when you create an instance (like LinkedListDeque<String> or LinkedListDeque<Double>). Hardcoding an integer here doesn't make sense—you can't guarantee unknown_type will be compatible with int.
3. Corrected Code with Explanations
Here's a revised version of your code that fixes these issues:
// Use standard generic parameter name (E for Element) for better readability public class LinkedListDeque<E> { private class Link { public Link start; public E body; public Link end; public Link(Link s, E b, Link e){ start = s; body = b; end = e; } } private Link sentinel; // Renamed for clarity—this is your placeholder node // Correct constructor for the outer LinkedListDeque class public LinkedListDeque(){ // For a sentinel node (common in deques), we don't need a meaningful body value // Use null since no elements have been added yet sentinel = new Link(null, null, null); // Make it a circular sentinel: point start and end to itself sentinel.start = sentinel; sentinel.end = sentinel; } // Optional: Add a constructor to initialize the deque with an element public LinkedListDeque(E initialElement){ this(); // Call empty constructor to set up the sentinel first addFirst(initialElement); // Add the initial element to the deque } // Example method to add an element to the front public void addFirst(E element) { Link newLink = new Link(sentinel, element, sentinel.end); sentinel.end.start = newLink; sentinel.end = newLink; } }
Key Improvements:
- Standardized generic parameter: Swapped
unknown_typeforE(industry convention for "Element" in collection classes). - Fixed constructor naming: The outer class constructor now correctly matches
LinkedListDeque. - Sentinel node setup: The sentinel (your original
connection) usesnullfor its body since it's just a placeholder, eliminating the type mismatch. - Optional initial element support: If you want to start with an element, the constructor accepts an
Etype parameter that aligns with the deque's generic type.
Why Your Original Code Failed
- When you wrote
new Link(null, 1, null), you tried to assign anintto a parameter of typeunknown_type. Unless you explicitly instantiateLinkedListDeque<Integer>, this will always throw a type error—unknown_typecould beString,Boolean, or any type that can't accept an integer. - The misnamed constructor meant your code was trying to define a second constructor for the inner
Linkclass, which doesn't have a no-arg constructor defined (Java doesn't auto-generate one if you have a parameterized constructor), leading to extra errors.
内容的提问来源于stack exchange,提问作者Adam




