您的位置:首页 > 娱乐 > 明星 > 关于内部类的一些问题

关于内部类的一些问题

2024/10/5 20:20:13 来源:https://blog.csdn.net/2302_80729149/article/details/141756229  浏览:    关键词:关于内部类的一些问题

这是初始code

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Inheritable{}
public class InheritableFather {public InheritableFather(){System.out.println("inheritableather"+InheritableFather.class.isAnnotationPresent(Inheritable.class));}//interitableson类继承于inheritablefather
public class  InheritableSon extends InheritableFather{public InheritableSon(){super();//调用父类构造参数System.out.println("inheritableson"+InheritableSon.class.isAnnotationPresent(Inheritable.class));}
}public static  void main(String[] args){InheritableSon is=new InheritableSon();}
}

此时,编译器报错

无法从 static 上下文引用 'InheritableFather.this'

这是正确的code

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Inheritable{}
public class InheritableFather {public InheritableFather(){System.out.println("inheritableather"+InheritableFather.class.isAnnotationPresent(Inheritable.class));}//interitableson类继承于inheritablefather
public  class InheritableSon extends InheritableFather{public InheritableSon(){super();//调用父类构造参数System.out.println("inheritableson"+InheritableSon.class.isAnnotationPresent(Inheritable.class)+name);}
}public static  void main(String[] args){InheritableFather i=new InheritableFather();InheritableSon is=i.new InheritableSon();}String name="shix";
}

或者说将inheritableson这个内部类改为静态

还有一种方法就是把这个内部类挪出来

那为什么会出现这个错误呢?


是因为InheritableSon类是一个非静态的嵌套类(即内部类),而非静态的嵌套类需要依赖于外部类的实例。也就是说,创建一个非静态嵌套类的实例时,需要首先创建它的外部类的实例。

为什么 InheritableFather 不报错?

InheritableFather 是一个顶层类,不嵌套在其他类中,因此可以直接创建它的实例,而不需要依赖于任何其他类的实例。

为什么 InheritableSon 报错?

因为InheritableSon是一个非静态的嵌套类,它隐式地持有外部类InheritableFather的一个实例引用。当你在main方法中直接创建InheritableSon的实例时,编译器无法自动找到这个外部类实例,导致错误。

解决方法

如果你想在没有外部类实例的情况下创建InheritableSon的对象,有两种方法可以选择:

  1. InheritableSon声明为静态嵌套类
    这样就不需要依赖于外部类实例了,可以直接在main方法中创建InheritableSon对象。

    public class InheritableFather {public InheritableFather() {System.out.println("inheritableather " + InheritableFather.class.isAnnotationPresent(Inheritable.class));}public static class InheritableSon extends InheritableFather {public InheritableSon() {super();System.out.println("inheritableson " + InheritableSon.class.isAnnotationPresent(Inheritable.class));}}public static void main(String[] args) {InheritableSon is = new InheritableSon();}
    }
    
  2. 保持InheritableSon为非静态类,但创建外部类实例时嵌套创建
    先创建InheritableFather的实例,然后通过它创建InheritableSon的实例。

    public class InheritableFather {public InheritableFather() {System.out.println("inheritableather " + InheritableFather.class.isAnnotationPresent(Inheritable.class));}public class InheritableSon extends InheritableFather {public InheritableSon() {super();System.out.println("inheritableson " + InheritableSon.class.isAnnotationPresent(Inheritable.class));}}public static void main(String[] args) {InheritableFather father = new InheritableFather();InheritableSon son = father.new InheritableSon(); // 通过外部类实例创建内部类实例}
    }
    

总结

报错的根本原因在于你在静态上下文(main方法)中尝试创建一个非静态嵌套类的实例,而没有提供必要的外部类实例。如果你将InheritableSon改为静态嵌套类,或者在创建InheritableSon实例时提供一个外部类的实例,就可以解决这个问题。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com