创建一个接口Shape,其中有抽象方法area,类Circle 、Rectangle实现area方法计算其面积并返回。又有Star实现Shape的area方法,其返回值是0,Star类另有一返回值bo...

题目补充:

创建一个接口Shape,其中有抽象方法area,类Circle Rectangle实现area方法计算其面积并返回。

又有Star实现Shapearea方法,其返回值是0Star类另有一返回值boolean型方法isStar

main方法里创建一个Vector,根据随机数的不同向其中加入Shape的不同子类对象(如是1,生成Circle对象;

如是2,生成Rectangle对象;如是3,生成Star对象)。然后将Vector中元素依次取出,判断其是否为Star类。如是返回其是个Star。否则返回其面积。

这个程序自己写的,运行时报错.但是我现在仍然不知道错在哪

以下是程序:

 1 import java.lang.Math;
 2 interface Shape{
 3 abstract double Area();
 4 }
 5 class Rectangle implements Shape{
 6 private double x;
 7 Rectangle(){
 8 }
 9 Rectangle(double x){
10 this.x=x;
11 }
12 public double getX(){
13 return x;
14 }
15 public double Area(){
16 //area=getX()*getX();
17 return getX()*getX();
18 }    
19 }
20 class Circle implements Shape{
21 private double r;
22 Circle(){
23 
24 }
25 Circle(double r){
26 this.r=r;
27 }
28 public double getR(){
29 return r;
30 }
31 
32 public double Area(){
33 return 3.14*getR()*getR();
34 }
35 }
36 class Star implements Shape{
37 //private double r;
38 Star(){}
39 public double Area(){
40 return 0;
41 }
42 public boolean isStar(){
43 if(Area()==0)
44 return true;
45 else 
46 return false;
47 }
48 }
49 class Vector implements Shape{
50 public double Area(){
51 return 0;
52 }
53 }
54 public class Test {
55 public static void main(String args[]){
56 Shape[] s=new Vector[9];      //新建对象数组
57 int n;
58 for(int i=0;i<s.length;i++){
59 n=(int)(Math.random()*5);      //随机函数产生随机值
60 switch(n){
61 case 1:s[i]=new Circle();break;  //父类引用指向子类对象
62 case 2:s[i]=new Rectangle();break;
63 case 3:s[i]=new Star();break;
64 }
65 }
66 for(int i=0;i<s.length;i++){
67 if(s[i].Area()==0){
68 System.out.println(s[i]+"是个Star");
69 }
70 else System.out.println(s[i].Area());
71 }
72 
73 
74 }
75 }

调试报错:

有没有哪位大神赐教?

 

转载于:https://www.cnblogs.com/silenTJ/p/7786808.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值