呵呵,你这个例子中的Rectangle恐怕是这段代码的原来类名称吧,看得出来它应该是构建函数,所以建议把它改为test2,或者把类名改为Rectangle

解决方案 »

  1.   

    首先数据封装性不好其次方法最好用类的实例来调用试试这样行不行class test2
    {
    protected int width,height;
    public Rectangle(int w,int h)
    {
    width=w;
    height=h;
    }
    protected int GetArea()
    {
    int area;
    area=width*height;
    return area;
    }
    public void drawRect()
    {
    for(int i=width;i>0;i--)
    System.out.print("*");
    System.out.println("\n");
    for(int i=height-2;i>0;i--)
    {
    System.out.print("*");
    for(int j=width-2;j>0;j--)
    System.out.print(' ');
    System.out.print("\n");
    }
    for(int i=width;i>0;i--)
    System.out.print("*");
    System.out.print("*");
    }
    }
      

  2.   

    Rectangle如果作为class中得一个方法,就要有返回类型,否则就是class的一个构造器。构造器名要与class名一致。
    把test2改为Rectangle,或者把Rectangle改为test2
      

  3.   

    public Rectangle(int w,int h)
    protected int GetArea()
    public void drawRect()
    看看你的三个方法的声明有什么不一样的吗?出错的那个和其他比少了点东西
      

  4.   

    1、文件名必须和你这个文件中包含的某一个类名相同。
    2、就是我上面所说除了构造函数,其它方法都需要返回类型。
    3、如果要一个类能独立运行,包含它的java文件必须要用它的名字作为文件名,而它一定要包含如下方法:
       public static void main(String[] args)再检查一次你的程序符合以上条件没有。
    如果还是不行,请注明你的错误是在编译提示还是执行时提示,提示内容。
      

  5.   

    E:\Tomcat\webapps\ROOT\java>javac test2.java
    test2.java:44: cannot resolve symbol
    symbol  : method appendChar (char)
    location: class java.lang.StringBuffer
                            dirBuf.appendChar(ch);
                                  ^
    test2.java:49: cannot resolve symbol
    symbol  : method appendChar (char)
    location: class java.lang.StringBuffer
                            fileBuf.appendChar(ch);
                                   ^
    2 errors
      

  6.   

    class Rectangle
    {
    protected int width,height;
    protected int area;
    public  Rectangle(int w,int h)
    {
    width=w;
    height=h;
    area=GetArea(w,h);
    }
    protected int GetArea(int w,int h)
    {
    int area;
    area=w*h;
    return area;
    }
    public void drawRect()
    {
    for(int i=width;i>0;i--)
    System.out.print("*");
    System.out.println("\n");
    for(int i=height-2;i>0;i--)
    {
    System.out.print("*");
    for(int j=width-2;j>0;j--)
    System.out.print(' ');
    System.out.print("\n");
    }
    for(int i=width;i>0;i--)
    System.out.print("*");
    System.out.print("*");
    }
    }class test2
    {
    public static void main(String [] args){
        Rectangle x=new Rectangle(10,2);
        System.out.println("area:"+x.GetArea(10,2));
        x.drawRect();
    }
    }