定义一个抽象基类Shape,它包含一个抽象方法getArea(),从Shape类派生出Rectangle和Circle类,这两个类都
(接上边的问题)用getArea()方法计算对象的面积。编写编写应用程序使用Rectangle类和Circle类。求代码,谢谢大家,帮帮忙...
(接上边的问题)用getArea()方法计算对象的面积。编写编写应用程序使用Rectangle类和Circle类。求代码,谢谢大家,帮帮忙
展开
4个回答
展开全部
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
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<iostream.h>
class shape
{
public:
void getarea();
void getperi();
};
class circle:public shape
{
public:
void getarea(double r)
{
cout<<"圆的面积为:";
cout<<r*r*3.14<<endl;
}
void getperi(double r)
{
cout<<"圆的周长为:";
cout<<2*r*3.14<<endl;
}
private:
double r;
};
class rectangle:public shape
{
public:
void getarea(double l,double w)
{
cout<<"矩形的面积为:";
cout<<l*w<<endl;
}
void getperi(double l,double w)
{
cout<<"矩形的周长为:";
cout<<2*(l+w)<<endl;
}
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
{
public:
void getarea();
void getperi();
};
class circle:public shape
{
public:
void getarea(double r)
{
cout<<"圆的面积为:";
cout<<r*r*3.14<<endl;
}
void getperi(double r)
{
cout<<"圆的周长为:";
cout<<2*r*3.14<<endl;
}
private:
double r;
};
class rectangle:public shape
{
public:
void getarea(double l,double w)
{
cout<<"矩形的面积为:";
cout<<l*w<<endl;
}
void getperi(double l,double w)
{
cout<<"矩形的周长为:";
cout<<2*(l+w)<<endl;
}
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("这子类的方法");
}
}
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);
}
}
{
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);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询