首页 > 编程语言 > 详细

java课后题圆形类三角形类矩形类继承抽象形状类,依旧无法运行的原因

时间:2021-04-30 10:21:46      阅读:22      评论:0      收藏:0      [点我收藏+]
abstract class Shape_basic{
    public abstract void perimeter();//周长
    public abstract void area();//面积
}
class Circle_basic extends Shape_basic{
    public double r;
    @Override
    public void perimeter() {
        System.out.println(4*3.14*r);
    }
    @Override
    public void area() {
        System.out.println(r*3.14*r);
    }
}

class Triangle_basic extends Shape_basic{
    public double length1,length2,length3;
    @Override
    public void perimeter() {
        System.out.println(length1+length2+length3);
    }
    @Override
    public void area() {
    double d = (length1+length2+length3)/2;
    System.out.println(Math.sqrt(d*(d-length1)*(d-length2)*(d-length3)));
    }
}
 class Rect_basic extends Shape_basic{
    public double r_long,r_wider;
    @Override
    public void perimeter() {
        System.out.println(2*(r_long+r_wider));
    }
    @Override
    public void area() {
        System.out.println(r_long*r_wider);
    }    
}
class Test{
    public static void main(String[] arge) {
        Circle_basic c1 = new Circle_basic();
        c1.r = 3;
        c1.area();
    }
}
 

开始编写的代码如上,也适合前一篇一样的问题,main无法运行。参考同学的代码发现,前面的四个类都是写在public class Shape{}之外,其后将四个类拿到主类之外以后能正常运行。代码如下

package javastudent423;

abstract class Shape_basic{
    public abstract void perimeter();//周长
    public abstract void area();//面积
}
class Circle_basic extends Shape_basic{
    public double r;
    @Override
    public void perimeter() {
        System.out.println("圆形的周长为:"+4*3.14*r);
    }
    @Override
    public void area() {
        System.out.println("圆形的面积为:"+r*3.14*r);
    }
}

class Triangle_basic extends Shape_basic{
    public double length1,length2,length3,length;
    @Override
    public void perimeter() {
        length = length1+length2+length3;
        System.out.println("三角形的周长为:"+length);
    }
    @Override
    public void area() {
    double d = (length1+length2+length3)/2;
    System.out.println("三角形的面积为:"+Math.sqrt(d*(d-length1)*(d-length2)*(d-length3)));
    }
}
 class Rect_basic extends Shape_basic{
    public double r_long,r_wider;
    @Override
    public void perimeter() {
        System.out.println("矩形的周长为:"+2*(r_long+r_wider));
    }
    @Override
    public void area() {
        System.out.println("矩形的面积为:"+r_long*r_wider);
    }    
}
public class Shape{
    public static void main(String[] arge) {
        Circle_basic c1 = new Circle_basic();
        c1.r = 3;
        System.out.println("圆形的半径为:"+c1.r);
        c1.area();
        c1.perimeter();
        Triangle_basic t1 = new Triangle_basic();
        t1.length1 = 3;
        t1.length2 = 4;
        t1.length3 = 5;
        System.out.println("三角形的三边长分别为:"+t1.length1+","+t1.length2+","+t1.length3);
        t1.area();
        t1.perimeter();
        Rect_basic r1 = new Rect_basic();
        r1.r_long = 4;
        r1.r_wider = 3;
        System.out.println("矩形的长宽分别为:"+r1.r_long+","+r1.r_wider);
        r1.area();
        r1.perimeter();
    }
}
 
 

对照前一篇解决方法,继承抽象类的类无法用static来修饰,所以之前的方法不可行,猜测应该是实现功能的类要写在主类外,才能正常的调用和运行。

java课后题圆形类三角形类矩形类继承抽象形状类,依旧无法运行的原因

原文:https://www.cnblogs.com/wenjis-nine-life-cat/p/14720244.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!