我需要帮助使这段代码更好...我不确定重复的代码太多是否有效。
该程序演示继承。我先以一个点开始主题,然后将其扩展为一个圆,最后扩展为一个圆柱。
这是主要的shapetest java类
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Payments extends Model
{
public function suppliers()
{
return $this->belongsTo(App\Suppliers::class, 'suppliers_ID', 'id');
}
}
我认为主要内容很容易理解,但是我觉得程序重复的次数太多。
该点的类别是:
import java.awt.*;
import java.util.*;
public class ShapeTest {
public static void main(String args[]){
Scanner getInput = new Scanner(System.in);
int r=0,option;
double point_x=0,point_y=0;
double circle_x=0, circle_y=0, circle_r=0;
double cylinder_x=0, cylinder_y=0, cylinder_r =0, cylinder_h =0;
while(r!=1) {
System.out.println("Which shape do you want, type in the number:");
System.out.println("1: Point");
System.out.println("2: Circle");
System.out.println("3: Cylinder");
System.out.println("4: Exit");
option = getInput.nextInt();
switch (option) {
case 1:
System.out.println("type in the x co-ordinate");
point_x = getInput.nextDouble();
System.out.println("type in the y co-ordinate");
point_y = getInput.nextDouble();
point point =new point(point_x,point_y);
point.getName();
break;
case 2:
System.out.println("type in the x co-ordinate");
circle_x = getInput.nextDouble();
System.out.println("type in the y co-ordinate");
circle_y = getInput.nextDouble();
System.out.println("type in radius of circle");
circle_r = getInput.nextDouble();
circle circle = new circle(circle_x,circle_y,circle_r);
circle.getName();
circle.getArea(circle_r);
break;
case 3:
System.out.println("type in the x co-ordinate");
cylinder_x = getInput.nextDouble();
System.out.println("type in the y co-ordinate");
cylinder_y = getInput.nextDouble();
System.out.println("type in radius of cylinder");
cylinder_r = getInput.nextDouble();
System.out.println("type in height of cylinder");
cylinder_h = getInput.nextDouble();
cylinder cylinder = new cylinder(cylinder_x,cylinder_y,cylinder_r,cylinder_h);
cylinder.getName();
cylinder.getArea(cylinder_r,cylinder_h);
cylinder.getVolume(cylinder_r,cylinder_h);
break;
case 4:
r=1;
break;
default:
r=1;
break;
}
}
}
}
圈子类别是
public class point{
public point(double x, double y){
centre(x,y);
}
public void centre(double x,double y){
double[] centre = new double[2];
centre[0] = x;
centre[1] = y;
}
public String getName(){
System.out.println("Point");
return "Point";
}
}
汽缸类为
public class circle extends point {
public circle(double x, double y, double r){
super(x,y);
radius(r);
}
public void radius(double r){
double radii = r;
}
public String getName(){
System.out.println("Circle");
return "Circle";
}
public double getArea(double r){
double area = 3.14*r*r;
System.out.println("Area is" + area);
return area;
}
}
有很多代码... 有没有办法使这段代码更有效?
感谢您的帮助