public class Test {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(3, 5);
double area = rectangle.getArea();
System.out.println("Area for Circle with width = 3 and height = 5 is: " + area);
Circle circle = new Circle(2);
area = circle.getArea();
System.out.println("Area for Circle with r = 2 is: " + area);
}
}
abstract class Shape{
public abstract double getArea();
}
class Rectangle extends Shape{
private double width;
private double height;
public Rectangle(double width, double height){
this.width = width;
this.height = height;
}
public double getArea() {//长方形= 长* 宽
return width * height;
}
}
class Circle extends Shape{
private double r;
public Circle(double radius){
this.r = radius;
}
public double getArea() {//圆 = 圆周率 * r *r
return Math.PI * r * r;
}
}
---------------------------
Area for Circle with width = 3 and height = 5 is: 15.0
Area for Circle with r = 2 is: 12.566370614359172
#include
class shape
{
public:
void getarea();
void getperi();
};
class circle:public shape
{
public:
void getarea(double r)
{
cout<<"圆的面积为:";
cout<
void getperi(double r)
{
cout<<"圆的周长为:";
cout<<2*r*3.14<
private:
double r;
};
class rectangle:public shape
{
public:
void getarea(double l,double w)
{
cout<<"矩形的面积为:";
cout<
void getperi(double l,double w)
{
cout<<"矩形的周长为:";
cout<<2*(l+w)<
private:
double l,w;
};
void main()
{
double r,a,b;
circle c;
cout<<"请输入圆的半径:";
cin>>r;
c.getarea(r);
c.getperi(r);
cout<<"请输入矩形的长和宽:";
cin>>a>>b;
rectangle t;
t.getarea(a,b);
t.getperi(a,b);
}
Class Shape{
getArea(){
system.out.println("这是基类的抽象方法");
}
}
Class Rectangle extends Shape{
getArea(){
system.out.println("这子类的方法");
}
}
Class Circle extends Shape{
getArea(){
system.out.println("这子类的方法");
}
}
abstract class Sharp
{
abstract public double getArea();
}
class Rectangle : Sharp
{
public float Length { get; set; }
public float Width { get; set; }
public Rectangle(){}
public Rectangle(float Length, float Width)
{
this.Length = Length;
this.Width = Width;
}
override public double getArea()
{
return Length * Width;
}
}
class Circle : Sharp
{
public float Radius { get; set; }
public Circle(){}
public Circle(int Radius)
{
this.Radius = Radius;
}
override public double getArea()
{
return Math.PI * Math.Pow( Radius,2);
}
}