public abstract class Area {
public void double area(){
}
public class RoundArea extends Area{
double roundarea , width;
double area(double width){
roundarea=Math.PI*width*width;
return roundarea;
}
}
public class RectArea extends Area{
double rectarea ,length, width;
double area(double length,double width){
rectarea=length*width;
return rectarea;
}
}
public class ImpleArea{
double length,width;
double getx(){
System.out.println("length="+length);
System.out.println("length="+width);
return length;
return width;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
RoundArea c=new RoundArea ();
c.area();
System.out.println("圆的面积是"+c);
RectArea a=new RectArea();
a.area();
System.out.println("长方形的面积是"+a);
}
}
------解决方案--------------------------------------------------------
public void double area()我看不懂这句,返回值到底是void还是double呢
------解决方案--------------------------------------------------------
想理解java里类的多态?
你定义了一个抽象类 叫做面积~~里面有个一个求面积的方法~~
接下里你开始定义了 圆类 还有 矩形类 ~~这里 圆应该有一个半径的变量 就是width
但是没看到你给这个属性赋值
也没看到什么构造方法~
那你这个面积怎么可能求的出来啊? 好比你定义了一个圆去不告诉他这个圆的半径是多少 让它求面积~~这咋求的出来啊!!!
矩形也是类似的~~~
public class ImpleArea{
double length,width;
double getx(){
System.out.println("length="+length);
System.out.println("length="+width);
return length;
return width;
}
}
还有你这个类 我就真不懂 你想干啥?
------解决方案--------------------------------------------------------
- Java code
public class RoundArea extends Area{ double roundarea , width; double area(double width){ roundarea=Math.PI*width*width; return roundarea; }}.......RoundArea c=new RoundArea ();c.area(1.0);//根据你在RoundArea定义的方法需要你传入一个double 类型的参数System.out.println("圆的面积是"+c); //这样就出来结果了//矩形类似的
------解决方案--------------------------------------------------------
Area.java
public abstract class Area {
public double area(double x){
return Math.PI * x * x ;
}
public double area(double x, double y ){
return x * y ;
}
}
RoundArea.java
public class RoundArea extends Area{
//private double roundarea , width;
/**
public double area(double width){
roundarea=Math.PI*width*width;
return roundarea;
}
*/
}
RectArea.java
public class RectArea extends Area{
//private double rectarea ,length, width;
/**
public double area(double length,double width){
rectarea=length*width;
return rectarea;
}
*/
}
ImpleArea.java
public class ImpleArea{
private double length,width;
//保留默认构造函数
public ImpleArea(){
}
//定义一个构造函数 ,初始化私有变量
public ImpleArea(double length, double width){
this.length = length ;
this.width = width ;
}
public double getx(){
System.out.println("length="+length);
System.out.println("length="+width);
//到底是想返回 length 还是 width 还是 length * width ,看你的方法名定义getx()应该是返回 length
return length;
//return width;
}
}
Test.java
public class Test{
public static void main(String[] args) {
// TODO Auto-generated method stub
Area c = new RoundArea() ;
double dc = c.area(10.1) ; // 你定义的area方法是有一个double参数的,调用的时候需要放入参数啊