nnpc.net
当前位置:首页 >> 利用JAVA继承和多态性编写程序:分别利用不同的公式来计算长方形正方形圆的面积 >>

利用JAVA继承和多态性编写程序:分别利用不同的公式来计算长方形正方形圆的面积

public class Demo { public static void main(String[] args) { A a=new B(); System.out.println(a.re()); a=new C(); System.out.println(a.re()); a=new D(); System.out.println(a.re()); } } abstract class A{ abstract int re(); } class B extends A{ int a=3; int re() {

// myrectangle.java 长方形 public class myrectangle { private double width; private double height; public myrectangle(double width, double height) { this.width = width; this.height = height; } public double getwidth() { return width; } public void setwidth(

长方形面积

代码如下,已经测试过了,希望能帮到你,仍有问题可以HI我~!abstract class Shape{ abstract double area(); } class Square extends Shape{ private double sideLength;//正方形边长 Square(double length){ sideLength = length; } public double

貌似 正方形的 w 跟 h 是相等的吧?一下代码仅供参考 public class TestCircle { public static void main(String[] args) { Shape shape1 = new Circle(2); Shape shape2 = new Triangle(10, 20); Shape shape3 = new Square(20); System.out.println("圆

Shape.java public class Shape {// 半径 private double r;// 长 private double l;// 宽 private double h;// 实例化圆的时候需要给出半径,长方形的长和宽 public Shape(double r, double l, double h) { this.r = r; this.l = l; this.h = h; }// 圆的 public double

首先要声明一下LZ问题描述的有点问题,Square的意思是正方形,所以应该是正方形(Square),三角形(Triangle),圆(Circle)来继承图形(Shape)来实现多态,即利用多态编程创建一个Shape类.补充:1楼,不知道你的多态体现在哪里

interface NewShape{public void setName(String string);public void setArea(double area);public String getName();public double getArea();public double getCir();//计算周长}class NewRectangle implements N

Test.java 为程序入口(main函数)

public class TestArea extends Area { public TestArea(){ super(); } public TestArea(double x,double y){ super(x,y); } public TestArea(double r){ super(r); } public static void main(String[] args) { try{ TestArea t1 = new TestArea(2,3); TestArea t2 =

相关文档
网站首页 | 网站地图
All rights reserved Powered by www.nnpc.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com