Come on

CSDN Q & A 2022-02-13 06:08:17 阅读数:609

come

Create a drawing parent class Shape, In this class, there are 1 Abstract methods Area(), Two derived classes, circular class Cirle, Rectangle class Rectangle, Override abstract methods in derived classes Area(). Create a test class Test, Output the area of circles and rectangles .

The writing of the three classes can be referred to ：

`` public abstract class Shape { public abstract double Area(); } public class Circle extends Shape{ int r; public Circle(int r) { this.r = r; } @Override public double Area() { return Math.PI*r*r; } } public class Rectangle extends Shape { int a; int b; public Rectangle(int a, int b) { this.a = a; this.b = b; } @Override public double Area() { return a*b; } }``

You have analyzed and understood , Do you want code

If help , Please accept . Click on the top right corner of my answer 【 Adopt 】 Button .

``public abstract class Shape { public abstract double Area();}public class Circle extends Shape{ private int r; public Circle(int length) { this.r = length; } @Override public double Area() { return Math.PI*r*r; } }public class Rectangle extends Shape { private double a; private double b; public Rectangle(double a, double b) { this.a = a; this.b = b; } @Override public double Area() { return a*b; }}public class ShapeTest { public static void main(String[] args) { Circle c=new Circle(3); Triangle t=new Triangle(3,4); System.out.println(" Area of circle ："+c.Area()); System.out.println(" Rectangular area ："+t.Area()); }}``

package mianji;

public abstract class Shape {
public abstract void Area();

}//////
package mianji;

public class Cirle extends Shape {
int r = 2;
double l = 3.14;
@Override
public void Area() {
// TODO Auto-generated method stub
System.out.println(" Area of circle ："+lrr);
}
}
/////
package mianji;

public class Rectangle extends Shape {
int chang =2;
int kuan =2;
@Override
public void Area() {
// TODO Auto-generated method stub
System.out.println(" Area of rectangle "+chang*kuan);
}
}
///////////
package mianji;

public class Test {
public static void main(String[] args) {
Shape a =new Cirle();
Shape b =new Rectangle();
a.Area();
b.Area();
}
}