难将就
2016-05-03 17:30
package yu.zhang; public abstract class Shape { public abstract double perimeter(); public abstract double area(); }
package yu.zhang; public class Rectangle extends Shape{ public double i = 3; //定义矩形的长 public double j = 2; //定义矩形的宽 public double perimeter(){ return(i+i+j+j); } public double area(){ return(i*j); } }
package yu.zhang; public class Circle extends Shape{ private double r = 4; //定义圆的半径 public double perimeter(){ return(3.14*r*2); } public double area(){ return(3.14*r*r); } }
package yu.zhang; public class main { public static void main(String[] args) { // TODO Auto-generated method stub Shape rec = new Rectangle(); Shape cir = new Circle(); double recPerimeter = rec.perimeter(); //求矩形周长 double recArea = rec.area(); //求矩形面积 System.out.println("矩形的周长是:" + recPerimeter); System.out.println("矩形的面积是:" + recArea); double cirPerimeter = cir.perimeter(); //求圆形的周长 double cirArea = cir.area(); //求圆形的面积 System.out.println("圆形的周长是:" + cirPerimeter); System.out.println("圆形的面积是:" + cirArea); } }
用double也是可以运行的
package yu.zhang;
public class main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Shape rec = new Rectangle();
System.out.println("矩形的周长是:" + rec.perimeter());
System.out.println("矩形的面积是:" + rec.area());
Shape cir = new Circle();
System.out.println("圆形的周长是:" + cir.Perimeter());
System.out.println("圆形的面积是:" + cir.area());
}
}
抽象类和接口的方法应该是public abstract void而不是public abstract double
橙色部分为Java关键字,蓝色应该为常量
Java入门第二季 升级版
530553 学习 · 6091 问题
相似问题