关于抽象类的问题.
//AbstractDemo.java
//class AbstractDemo
public class AbstractDemo {
//包含抽象方法的 抽象类
abstract class Shape {
int xPosition, yPosition;
void setCenterPoint(int xPos, int yPos) {
xPosition = xPos;
yPosition = yPos;
}
abstract void draw();
}
//正方形类继承Shape类
class Square extends Shape{
int length;
public Square(int xPos,int yPos,int len){
super.setCenterPoint(xPos,yPos);
length=len;
}
void draw() {
// TODO Auto-generated method stub 以(xPos,yPos 为圆心,画出一个边长为length的正方形)
System.out.println("this is a square :\n center :("+xPosition+","+yPosition+")"+"\t Wide length:"+length);
}
}
//圆类继承Shape类
class Circle extends Shape{
int ri;
public Circle(int xPos,int yPos,int r){
super.setCenterPoint(xPos,yPos);
ri=r;
}
void draw() {
// TODO Auto-generated method stub 以(xPos,yPos 为圆心,画出一个边长为length的正方形)
System.out.println("this is a square :\n center :("+xPosition+","+yPosition+")"+"\t Wide length:"+ri);
}
}
//三角形类继承Shape类
class Trigon extends Shape{
int bottom;
int highness;
public Trigon(int xPos,int yPos,int bott,int high){
super.setCenterPoint(xPos,yPos);
bottom=bott;
highness=high;
}
void draw() {
// TODO Auto-generated method stub 以(xPos,yPos 为圆心,画出一个边长为length的正方形)
System.out.println("this is a square :\n center :("+xPosition+","+yPosition+")"+"\t bottom lenth:"+bottom+"\t Highness "+highness);
}
}
//以Shape类型的对象为参数管理
class ShapeManager{
void manager (Shape shapeObj){
shapeObj.draw();
}
}
public static void main(String args[]) {
ShapeManager shapeManager = new ShapeManager();
Square square = new Square(10,20,5);
Circle circle=new Circle(20,30,8);
Trigon trigon=new Trigon(30,40,6,4);
System.out.println();
shapeManager.manager(circle);
shapeManager.manager(square);
shapeManager.manager(trigon);
System.out.println();
}
}
总是在main函数位置出错,我的环境是 eclipse3.1+myeclipse4.0 jdk 5.0
No enclosing instance of type AbstractDemo is accessible. Must qualify the
allocation with an enclosing instance of type AbstractDemo (e.g. x.new A()
where x is an instance of AbstractDemo).