Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

java - My is my method not defined for the class?

I am a bit inexperienced in programming and may therefore be foolish. But I have a problem in the following method:

import java.util.Set;

public class Launcher 
{
    

        public static void printEntries(ReadableMap<String, Integer> a)
        {
           Set<String> b = a.keysAsSet();
            try
                {
                    for(String i : b)
                    {
                            if(i != null)
                            {
                                getOrThrow(i);
                            }
                            System.out.println(i + ": " + getOrThrow(i));
                    } 
                }
                catch(UnknownKeyException z)
                    {
                        throw new UnknownKeyException();
                        System.out.println("Eine UnknownKeyException ist aufgetreten.");
                    }
        }

}

The method getOrThrow(String) is undefined for the type Launcher.

Here are some code snippets that might be helpful:

public interface ReadableMap<K, V> {

    
    public abstract V getOrThrow(K key) throws UnknownKeyException;
}

and

public abstract class AbstractReadableMap<K, V> implements ReadableMap<K, V> 
{  
        
protected Entry<K, V>[] entries;


public V getOrThrow(K k) throws UnknownKeyException 
        {
            for(Entry<K, V> i : entries) 
            {
                if(i.getValue() != null && i.getKey().equals(k)) 
                {
                    return i.getValue();
                }
            }
            throw new UnknownKeyException();
        }
}

Why is that and how can I fix it?

I am very grateful for any kind of help. Thank you in advance!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (71.8m points)

The call to getOrThrow(i) here is implicitly Launcher.getOrThrow(i) - in other words, it is calling a method belonging to class Launcher. You may want to call a.getOrThrow(i) instead, since a is of a type that does implement that method, ReadableMap.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
...