关于抽象类的问题.

tiancom 2007-01-12 04:57:22
//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).
...全文
158 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
tiancom 2007-01-12
  • 打赏
  • 举报
回复
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).

这是出错信息,英文不好 3ke yu

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧