慕粉2110022127
2017-06-04 16:52
public abstract class shape{
public abstract double C();
public abstract double S();}
public class Rectaugle extends shape {
double a=4.5;
double b=2.1;
double c=(a+b)*2;
double s=a*b;
public double C (){
System.out.println("矩形周长为"+c);
return c;}
public double S(){
System.out.println("面积为"+s);
return s;}
}
public class Circle extends shape {
final double p=3.14;
double r=2.1;
double c=2*p*r;
double s=p*r*r;
public double C(){
return c;
System.out.println("圆形周长为"+c);}
public double S(){
System.out.println("面积为"+s);
return s;}
}
public class Hello{
public static void main (String [] args){
shape s1=new Rectaugle();
shape s2=new Circle();
s1.C();
s1.S();
s2.C();
s2.S();}
}
public class Circle extends shape {
final double p=3.14;
double r=2.1;
double c=2*p*r;
double s=p*r*r;
public double C(){
return c;
System.out.println("圆形周长为"+c);}
在这段代码中 return c 应放在System.out.println("圆形周长为"+c);后面。
这是我运行的结果:矩形周长为13.2
面积为9.450000000000001
圆形周长为13.188
面积为13.8474
Java入门第二季 升级版
530715 学习 · 6091 问题
相似问题