public class Baseclass { /** * */ private String height; public String getHeight() { return height; } public void setHeight(String height) { this.height = height; } // 下面是继承Baseclass类Extendclass class Extendclass extends Baseclass { private String width; public String getWidth() { return width; } public void setWidth(String width) { this.width = width; } } public static void main(String[] args) { // TODO Auto-generated method stub Baseclass baseclass1 = new Extendclass(); Baseclass baseclass2 = new Baseclass(); System.out.println(baseclass1.getClass().getSimpleName());// 实际运行的是继承类Extendclass System.out.println(baseclass2.getClass().getSimpleName());// 实际运行的是Baseclass System.out.println(Baseclass.class.getSimpleName());// 加载时类名 System.out.println(Extendclass.class.getSimpleName());// 加载时类名 }}
你的那个问题报的错是这样的No enclosing instance of type Baseclass is accessible. Must qualify the allocation with an enclosing instance of type Baseclass (e.g. x.new A() where x is an instance of Baseclass).解释的意思应该是内部类不能实现化,我认为就这个意思,如果你把位置换一下的话,就可以了回该是这样的-------------------------------------------------------------------------------package first.test;public class Baseclass { /** * */ private String height; public String getHeight() { return height; } public void setHeight(String height) { this.height = height; } // 下面是继承Baseclass类Extendclasspublic static void main(String[] args) { // TODO Auto-generated method stub Baseclass baseclass1 = new Extendclass(); Baseclass baseclass2 = new Baseclass(); System.out.println(baseclass1.getClass().getSimpleName());// 实际运行的是继承类Extendclass System.out.println(baseclass2.getClass().getSimpleName());// 实际运行的是Baseclass System.out.println(Baseclass.class.getSimpleName());// 加载时类名 System.out.println(Extendclass.class.getSimpleName());// 加载时类名 } }class Extendclass extends Baseclass { private String width; public String getWidth() { return width; } public void setWidth(String width) { this.width = width; } }
具体不知道你什么情况。public class Baseclass { /** * */ private String height; public String getHeight() { return height; } public void setHeight(String height) { this.height = height; } } // 下面是继承Baseclass类Extendclass class Extendclass extends Baseclass { private String width; public String getWidth() { return width; } public void setWidth(String width) { this.width = width; } public static void main(String args[]) { // TODO Auto-generated method stub Baseclass baseclass1 = new Extendclass(); Baseclass baseclass2 = new Baseclass(); System.out.println(baseclass1.getClass().getSimpleName());// 实际运行的是继承类Extendclass System.out.println(baseclass2.getClass().getSimpleName());// 实际运行的是Baseclass System.out.println(Baseclass.class.getSimpleName());// 加载时类名 System.out.println(Extendclass.class.getSimpleName());// 加载时类名 }}