慕粉2110022127
2017-05-21 15:42
public abstract class shape{
public abstract double C();
public abstract double S();}
public class Rectaugle extands shape {
double a=4.5,b=2.1;
public double Rectaugle (){
double a=4.5;
double b=2.1;
double C=(a+b)*2;
double S=a*b;
return C;
return S;
System.out.println("矩形周长为"+C);
System.out.println("面积为"+S);
}}
public class Circle extands shape {
public double Circle (){
final double p=3.14;
double r=2.1;
double C=2*p*r;
double s=p*r*r;
return C;
return S;
System.out.println("圆形周长为"+C);
System.out.println("面积是"+S);}
}}
public class Hello{
public static void main (String [] args){
shape s1=new Rectaugle();
shape s2=new Circle();
s1.Rectaugle();
s2.Circle();}
}
public class JS{ public static void main (String [] args){ shape s1=new Rectaugle(); shape s2=new Circle(); System.out.println("矩形的长"+s1.C()+" "+"矩形的面积"+s1.S()); System.out.println(""); System.out.println("圆形的长"+s2.C()+" "+"圆形的面积"+s2.S()); } } //Java.JS 主类 (各个类分开来写) public abstract class shape{ public abstract double C(); public abstract double S(); } //Java.shape 抽象类 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(){ return c;//长度 } public double S() { return s;//面积 } } //Java.Rectaugle (子类--矩形类) public class Circle extends shape{ double p=3.14; double r=2.1; double c=2*p*r; double s=p*r*r; public double C(){ return c;//长度 } public double S() { return s;//面积 } } //Java.Circle (子类--圆形类)
输出的答案: 矩形的长13.2 矩形的面积9.450000000000001
圆形的长13.188 圆形的面积13.8474
有返回值的方法不能返回两个值
继承是extents,C()是方法不是变量,不能赋值!
Java入门第二季 升级版
530652 学习 · 6091 问题
相似问题