문제

When declaring an anonymous class like in the following:

Note that ResourceBundle.Control is a concrete class.

Snippet taken from here.

private static void test(Locale locale) {
    ResourceBundle rb = ResourceBundle.getBundle("ResourceBundle", locale,
         new ResourceBundle.Control() {
         @Override
         public List<Locale> getCandidateLocales(String baseName, Locale locale) {
             if (baseName == null)
             throw new NullPointerException();
             if (locale.equals(new Locale("it", "IT"))) {
             return Arrays.asList(
                 locale,
                 Locale.ITALY,
                 Locale.CHINESE ,
                 Locale.ROOT);
             } else if (locale.equals(Locale.GERMANY)) {
             return Arrays.asList(
                 locale.GERMANY,
                 // no Locale.CHINESE here
                 Locale.ROOT);
             }
             return super.getCandidateLocales(baseName, locale);
         }
         });

I see basically that the keywork extends it's not used when declaring the anonymous class. Does it take it for granted? If so when is appropriate to use extends in anonymous classes?

Thanks in advance.

도움이 되었습니까?

해결책

when is appropriate to use extends in anonymous classes?

Never. Keyword extends is appropriate for named classes, which can implement interfaces in addition to extending classes. In contrast, anonymous classes can either implement an interface or extend a class, but not both. That is why the syntax has no keyword at all - just put curly braces after a new expression, and provide implementation for the methods of your base class or an interface.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top