java-抽象类

package com.test.jun;
import com.test.jun.A.B;
abstract class A {
    public static final String FLAG = "CHINA";
    private String name = "june";    
    public void setName(String name) {
this.name = name;
 }  
    public String getName() {
return name;

    public abstract void print(); 
  public static class B extends A{
   public void print() {
  // TODO Auto-generated method stub
  System.out.println("FLAG:" + FLAG);
  System.out.println("name:" +super.getName());
  }
    }
}

public class AbstractTest {
/**
* @param args
*/
public static void main(String[] args) {

// TODO Auto-generated method stub

//抽象类不能初始化,所以new A 是错误的

//A a=new A(); 
B b= new B();
b.print();
}

}

--------------------------------------------------------------------------------------------

 class B extends A{

     public void print() {
   // TODO Auto-generated method stub
   System.out.println("FLAG:" + FLAG);
   System.out.println("name:" +super.getName());
   }
    }

//抽象类无法初始化的,需要初始化就别用抽象类,用抽象类的子类,当时new B时候还是报错。

错误为:No enclosing instance of type A is accessible. Must qualify the allocation with an enclosing instance of type A (e.g. x.new A() where x is an instance of A).

然后一直纳闷,不是说抽象类不能初始化,可以用抽象类的子类吗?然后百度一下。

答案是:我写的内部类是动态的,也就是开头以public class开头。而主程序是public static class main

解决方法为: class B extends A{   改为 public static class B extends A{

public static class B extends A{

     public void print() {
   // TODO Auto-generated method stub
   System.out.println("FLAG:" + FLAG);
   System.out.println("name:" +super.getName());
   }
    }





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值