import java.awt.*;
import java.applet.*;
abstract class Shapes
{
public int width,height;
public Shapes(int width,int height)
{
this.width=width;
this.height=height;
}
abstract double getArea();
abstract double getPerimeter();
}
class Square extends Shapes
{
public Square(int width,int height)
{
super(width,height);
}
public double getArea()
{return(width*height);}
public double getPerimeter()
{return(2*(width+height));}
}
class jex
{public static void main(String args[])
{
Square jex=new Square(45,56);
System.out.println("Area"+jex.getArea()+"Perimeter"+jex.getPerimeter());
}}
书上说去掉this就默认为左边是数据成员右边是形参,但我去掉this就得不到答案,也就是不能给数据成员赋值。
求高手帮忙。
參考答案:public Shapes(int awidth,int aheight)
{
width=awidth;
height=aheight;
}
这样就可以了,默认为左边是数据成员右边是形参
因为你的形参和数据成员的名字相同,所以用this明确告诉编译器哪个是数据成员.