java运行时老说找不到符号,怎么回事啊
interfaceA{doublegirth();doublearea();}classB{doublewidth;doublelength;publicB(double...
interface A{
double girth();
double area();
}
class B {
double width;
double length;
public B(double width,double length){
this.width=width;
this.length=length;
}
double area(){
return width*length;
}
double girth(){
return (width+length)*2;
}
}
class Rectangle extends B implements A{
public double area(){
return width*length;
}
public double girth(){
return (width+length)*2;
}
}
public class Rectangle1{
public static void main (String[] args){
Rectangle c1=new Rectangle(12.8,14.5);
System.out.println("面积是:"+c1.area());
System.out.println("周长是:"+c1.girth());
}
} 展开
double girth();
double area();
}
class B {
double width;
double length;
public B(double width,double length){
this.width=width;
this.length=length;
}
double area(){
return width*length;
}
double girth(){
return (width+length)*2;
}
}
class Rectangle extends B implements A{
public double area(){
return width*length;
}
public double girth(){
return (width+length)*2;
}
}
public class Rectangle1{
public static void main (String[] args){
Rectangle c1=new Rectangle(12.8,14.5);
System.out.println("面积是:"+c1.area());
System.out.println("周长是:"+c1.girth());
}
} 展开
展开全部
错误原因:Rectangle虽然继承了B类,但还有接口A,Rectangle重写的是接口里边的两个方法和继承B类的属性,而没重写的A里边的方法没有得到Rectangle继承B的值。所以一直运行为0。
修改方法有2种:
1>---需要给Rectangle重写一下,如下:
public Rectangle(double width, double length) {
super(width, length);
this.width=width;
this.length=length;
}
2>---只需要改动B和Rectangle类的内容,请看代码,内容改动大不便细说:
class B implements A{
double width;
double length;
public B(double width,double length){
this.width=width;
this.length=length;
}
double area(){
return width*length;
}
double girth(){
return (width+length)*2;
}
}
class Rectangle extends B{
private double width,length;
public Rectangle(double width, double length) {
super(width, length);
}
}
修改方法有2种:
1>---需要给Rectangle重写一下,如下:
public Rectangle(double width, double length) {
super(width, length);
this.width=width;
this.length=length;
}
2>---只需要改动B和Rectangle类的内容,请看代码,内容改动大不便细说:
class B implements A{
double width;
double length;
public B(double width,double length){
this.width=width;
this.length=length;
}
double area(){
return width*length;
}
double girth(){
return (width+length)*2;
}
}
class Rectangle extends B{
private double width,length;
public Rectangle(double width, double length) {
super(width, length);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询