不负旧时日
2017-07-27 15:04
//Shape父类 public abstract class Shape { public float a; public float b; public float r; public abstract void C(); public abstract void S(); public Shape(float a, float b, float r) { this.a = a; this.b = b; this.r = r; } } //矩形子类 public class Rectangle extends Shape { public void C() { System.out.println("矩形的周长为:"+(a+b)*2); } public void S() { System.out.println("矩形的面积为:"+a*b); } public Rectangle(float a,float b,float r) { super(a,b,r); } } //圆形子类 public class Circle extends Shape { public void C() { System.out.println("圆形的周长为:"+3.14*2*r); } public void S() { System.out.println("圆形的面积为:"+3.14*r*r); } public Circle(float a,float b,float r) { super(a,b,r); } } //输出 import java.util.Scanner; public class Result { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("请输入矩形的长度:"); String z = input.next(); System.out.println("请输入矩形的宽度:"); String x = input.next(); System.out.println("请输入圆形的半径:"); String c = input.next(); float a = Float.parseFloat(z); float b = Float.parseFloat(x); float r = Float.parseFloat(c); Shape rec = new Rectangle(a, b, r); Shape cir = new Circle(a, b, r); rec.C(); rec.S(); cir.C(); cir.S(); } }
你的输入自己给复杂化了,输入直接double(建议)或者float 类型的就好啊,没必要输入String类型,再转化为float类型的。还有你设计的数据模型,建议在Rectangle中只设长和宽,Circle类中只设半径,没必要全设在父类中,还有构造函数建议在每个子类中进行重写,希望能对你有所帮助
package com.imooc1;
public abstract class Shape {
public abstract void ZC();
public abstract void MJ();
}
package com.imooc1;
public class Rectangle extends Shape {
public double c ;
public double k ;
public Rectangle(double c, double k) {
// TODO Auto-generated constructor stub
this.c = c;
this.k = k;
}
@Override
public void ZC() {
// TODO Auto-generated method stub
System.out.println("矩形的周长是:"+(c+k)*2);
}
@Override
public void MJ() {
// TODO Auto-generated method stub
System.out.println("矩形的面积是:"+c*k);
}
}
package com.imooc1;
public class Circle extends Shape {
public double r ;
public Circle(double r) {
// TODO Auto-generated constructor stub
this.r = r;
}
@Override
public void ZC() {
// TODO Auto-generated method stub
System.out.println("圆的周长是:"+(2*3.14*r));
}
@Override
public void MJ() {
// TODO Auto-generated method stub
System.out.println("圆的面积是:"+(3.14*r*r));
}
}
package com.imooc1;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("请输入矩形的长度:");
double c = Double.parseDouble(input.next());
System.out.println("请输入矩形的宽度:");
double k = Double.parseDouble(input.next());
System.out.println("请输入圆形的半径:");
double r = Double.parseDouble(input.next());
Shape test1 = new Rectangle(c,k);
Shape test2 = new Circle(r);
test1.ZC();
test1.MJ();
test2.ZC();
test2.MJ();
}
这是我写的代码 你先看看 有没有多余的 可以优化的地方啊
//优化后的代码 //shape父类 public abstract class Shape { public abstract void C(); public abstract void S(); } //矩形子类 public class Rectangle extends Shape { Scanner input = new Scanner(System.in); double a = input.nextDouble(); double b = input.nextDouble(); public void C() { System.out.println("矩形的周长为:" + (a + b) * 2); } public void S() { System.out.println("矩形的面积为:" + a * b); } } //圆形子类 public class Circle extends Shape { Scanner input = new Scanner(System.in); double r = input.nextDouble(); public void C() { System.out.println("圆形的周长为:" + Math.PI * 2 * r); } public void S() { System.out.println("圆形的面积为:" + Math.PI * r * r); } } //输出 public class Result { public static void main(String[] args) { Scanner input = new Scanner(System.in); String Circle = "圆形"; String Rectangle = "矩形"; System.out.println("请输入你想计算的多边形:"); String q = input.next(); if (q.equals(Rectangle)) { System.out.println("请输入矩形的长和宽:"); Rectangle rec = new Rectangle(); rec.C(); rec.S(); } else if(q.equals(Circle)){ System.out.println("请输入圆形的半径:"); Circle cir = new Circle(); cir.C(); cir.S(); } } }
希望你能把优化后的代码 发一下,让我参考参考
Java入门第二季 升级版
530653 学习 · 6091 问题
相似问题