JAVA简单问题!!急!!好人帮帮忙
定义一个抽象类shape,包含一个抽象方法Area(),从Shape派生出一个正方形Square子类和圆Circle子类,要求两个子类有构造方法,实现父类的Area方法计...
定义一个抽象类shape,包含一个抽象方法Area(),从Shape派生出一个正方形Square子类和圆Circle子类,要求两个子类有构造方法,实现父类的Area方法计算面积,并对toString进行重写,编写main方法,生成两个类对象,调用Area方法求面积,用toString输出。谢谢!!!!!
展开
3个回答
展开全部
public class Test
{
public static void main(String[] args)
{
Circle c = new Circle(5);
c.Area();
System.out.println(c);
Square s = new Square(2,3);
s.Area();
System.out.println(s);
}
}
class Circle extends Shape
{
private double r;
private double PI = 3.14159265;
private double area;
public Circle(double r)
{
this.r = r;
}
public void Area()
{
area = 2*PI*r*r;
}
@Override
public String toString()
{
return "面积: " + area;
}
}
class Square extends Shape
{
private double x;
private double y;
private double area;
public Square(double x,double y)
{
this.x = x;
this.y = y;
}
public void Area()
{
area = x*y;
}
@Override
public String toString()
{
return "面积: " + area;
}
}
abstract class Shape
{
abstract public void Area();
}
{
public static void main(String[] args)
{
Circle c = new Circle(5);
c.Area();
System.out.println(c);
Square s = new Square(2,3);
s.Area();
System.out.println(s);
}
}
class Circle extends Shape
{
private double r;
private double PI = 3.14159265;
private double area;
public Circle(double r)
{
this.r = r;
}
public void Area()
{
area = 2*PI*r*r;
}
@Override
public String toString()
{
return "面积: " + area;
}
}
class Square extends Shape
{
private double x;
private double y;
private double area;
public Square(double x,double y)
{
this.x = x;
this.y = y;
}
public void Area()
{
area = x*y;
}
@Override
public String toString()
{
return "面积: " + area;
}
}
abstract class Shape
{
abstract public void Area();
}
展开全部
abstract class Shape
{
void Area()
{
}
}
class Square extends Shape
{
int a, b, Sarea;
void Area()
{
Sarea = a* b;
}
void String toString()
{
return Sarea+'' '';
}
}
class Circle extends Shape
{
int r;
double Carea;
void Area()
{
Carea = 4* Math.PI* r* r;
}
void String toString()
{
return Circle+ '' '';
}
}
public class Test
{
public static void main(String[] args)
{
String S, C;
Square square = new Square();
Circle circle = new Circle();
square.Area();
circle.Area();
S = square.toString();
C = circle.toString();
System.out.println("the square's area is"+ S);
System.out.println("the square's area is"+ C);
}
}
````````````````````````````````````````````````````````````````````````````
我直接打的 你自己去验证一下 可能我输入会有错误
{
void Area()
{
}
}
class Square extends Shape
{
int a, b, Sarea;
void Area()
{
Sarea = a* b;
}
void String toString()
{
return Sarea+'' '';
}
}
class Circle extends Shape
{
int r;
double Carea;
void Area()
{
Carea = 4* Math.PI* r* r;
}
void String toString()
{
return Circle+ '' '';
}
}
public class Test
{
public static void main(String[] args)
{
String S, C;
Square square = new Square();
Circle circle = new Circle();
square.Area();
circle.Area();
S = square.toString();
C = circle.toString();
System.out.println("the square's area is"+ S);
System.out.println("the square's area is"+ C);
}
}
````````````````````````````````````````````````````````````````````````````
我直接打的 你自己去验证一下 可能我输入会有错误
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询