求java大神帮忙
答案:2 悬赏:70 手机版
解决时间 2021-11-21 01:58
- 提问者网友:沉默菋噵
- 2021-11-20 04:54
求java大神帮忙
最佳答案
- 五星知识达人网友:不如潦草
- 2021-11-20 06:07
普通类
public class CircularA {
//求圆的表面积
public double surfaceArea(double radius) {
return 4*Math.PI*radius*radius;
}
//圆内接正方形面积
public double withinSquare(double radius) {
return 6*(Math.sqrt(3)/6)*radius*(Math.sqrt(3)/6)*radius;
}
//圆外接正方形面积
public double outsideSquare(double radius) {
return radius*2*radius*2*6;
}
}
抽象类
public abstract class CircularB {
//求圆的表面积
public abstract double surfaceArea(double radius) ;
//圆内接正方形面积
public abstract double withinSquare(double radius) ;
//圆外接正方形面积
public abstract double outsideSquare(double radius);
}
接口
public interface CircularC {
//求圆的表面积
public double surfaceArea(double radius) ;
//圆内接正方形面积
public double withinSquare(double radius);
//圆外接正方形面积
public double outsideSquare(double radius) ;
}
继承抽象类
public class CircularD extends CircularB {
@Override
public double surfaceArea(double radius) {
return 4*Math.PI*radius*radius;
}
@Override
public double withinSquare(double radius) {
return 6*(Math.sqrt(3)/6)*radius*(Math.sqrt(3)/6)*radius;
}
@Override
public double outsideSquare(double radius) {
return radius*2*radius*2*6;
}
}
实现接口
public class CircularE implements CircularC {
@Override
public double surfaceArea(double radius) {
return 4*Math.PI*radius*radius;
}
@Override
public double withinSquare(double radius) {
return 6*(Math.sqrt(3)/6)*radius*(Math.sqrt(3)/6)*radius;
}
@Override
public double outsideSquare(double radius) {
return radius*2*radius*2*6;
}
}
public class Test {
//测试普通类
public void testA(double redius) {
CircularA circularA = new CircularA();
//求圆的表面积(传入半径)
double s1 = circularA.surfaceArea(redius);
System.out.println(s1);
//圆内接正方形面积(出入半径)
double s2 = circularA.withinSquare(redius);
System.out.println(s2);
//圆外接正方形面积(传入半径)
double s3 = circularA.outsideSquare(redius);
System.out.println(s3);
}
//测试抽象类
public void testB(double redius) {
//多态。声明父类对象,new 子类。
CircularB circular = new CircularD();
//求圆的表面积(传入半径)
double s1 = circular.surfaceArea(redius);
System.out.println(s1);
//圆内接正方形面积(出入半径)
double s2 = circular.withinSquare(redius);
System.out.println(s2);
//圆外接正方形面积(传入半径)
double s3 = circular.outsideSquare(redius);
System.out.println(s3);
}
//测试接口
public void testC(double redius) {
CircularC circular = new CircularE();
//求圆的表面积(传入半径)
double s1 = circular.surfaceArea(redius);
System.out.println(s1);
//圆内接正方形面积(出入半径)
double s2 = circular.withinSquare(redius);
System.out.println(s2);
//圆外接正方形面积(传入半径)
double s3 = circular.outsideSquare(redius);
System.out.println(s3);
}
public static void main(String[] args) {
Test test = new Test();
test.testA(10);
test.testB(10);
test.testC(10);
}
}
这里说明一点:抽象类可以定义一个完整的方法,直接由子类继承,或者定义抽象方法,由子类重写
public class CircularA {
//求圆的表面积
public double surfaceArea(double radius) {
return 4*Math.PI*radius*radius;
}
//圆内接正方形面积
public double withinSquare(double radius) {
return 6*(Math.sqrt(3)/6)*radius*(Math.sqrt(3)/6)*radius;
}
//圆外接正方形面积
public double outsideSquare(double radius) {
return radius*2*radius*2*6;
}
}
抽象类
public abstract class CircularB {
//求圆的表面积
public abstract double surfaceArea(double radius) ;
//圆内接正方形面积
public abstract double withinSquare(double radius) ;
//圆外接正方形面积
public abstract double outsideSquare(double radius);
}
接口
public interface CircularC {
//求圆的表面积
public double surfaceArea(double radius) ;
//圆内接正方形面积
public double withinSquare(double radius);
//圆外接正方形面积
public double outsideSquare(double radius) ;
}
继承抽象类
public class CircularD extends CircularB {
@Override
public double surfaceArea(double radius) {
return 4*Math.PI*radius*radius;
}
@Override
public double withinSquare(double radius) {
return 6*(Math.sqrt(3)/6)*radius*(Math.sqrt(3)/6)*radius;
}
@Override
public double outsideSquare(double radius) {
return radius*2*radius*2*6;
}
}
实现接口
public class CircularE implements CircularC {
@Override
public double surfaceArea(double radius) {
return 4*Math.PI*radius*radius;
}
@Override
public double withinSquare(double radius) {
return 6*(Math.sqrt(3)/6)*radius*(Math.sqrt(3)/6)*radius;
}
@Override
public double outsideSquare(double radius) {
return radius*2*radius*2*6;
}
}
public class Test {
//测试普通类
public void testA(double redius) {
CircularA circularA = new CircularA();
//求圆的表面积(传入半径)
double s1 = circularA.surfaceArea(redius);
System.out.println(s1);
//圆内接正方形面积(出入半径)
double s2 = circularA.withinSquare(redius);
System.out.println(s2);
//圆外接正方形面积(传入半径)
double s3 = circularA.outsideSquare(redius);
System.out.println(s3);
}
//测试抽象类
public void testB(double redius) {
//多态。声明父类对象,new 子类。
CircularB circular = new CircularD();
//求圆的表面积(传入半径)
double s1 = circular.surfaceArea(redius);
System.out.println(s1);
//圆内接正方形面积(出入半径)
double s2 = circular.withinSquare(redius);
System.out.println(s2);
//圆外接正方形面积(传入半径)
double s3 = circular.outsideSquare(redius);
System.out.println(s3);
}
//测试接口
public void testC(double redius) {
CircularC circular = new CircularE();
//求圆的表面积(传入半径)
double s1 = circular.surfaceArea(redius);
System.out.println(s1);
//圆内接正方形面积(出入半径)
double s2 = circular.withinSquare(redius);
System.out.println(s2);
//圆外接正方形面积(传入半径)
double s3 = circular.outsideSquare(redius);
System.out.println(s3);
}
public static void main(String[] args) {
Test test = new Test();
test.testA(10);
test.testB(10);
test.testC(10);
}
}
这里说明一点:抽象类可以定义一个完整的方法,直接由子类继承,或者定义抽象方法,由子类重写
全部回答
- 1楼网友:一把行者刀
- 2021-11-20 06:38
//家3527,837346没有问题哒
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯