java getAClass()问题,提示出现下面错误 Exception in thread "main" java.lang.Error: Unresolv

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());// 加载时类名

}

}
最新回答
仅有的温存丶

2024-08-14 00:48:52

你的那个问题报的错是这样的
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类Extendclass

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());// 加载时类名

}

}
class Extendclass extends Baseclass {

private String width;

public String getWidth() {
return width;
}

public void setWidth(String width) {
this.width = width;
}
}
紫珺婳浅

2024-08-14 03:12:10

具体不知道你什么情况。

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());// 加载时类名

}
}