import java.awt.Graphics;public abstract class MyShape     //第一个类,abstract 
{
   private int x1;
   private int y1;
   private int x2;
   private int y2;public MyShape()
{
  
}
public MyShape(int x1,int y1,int x2,int y2)
{
  this.x1 = x1;
  this.y1 = y1;
  this.x2 = x2;
  this.y2 = y2;
}public int getx1()
{
  return x1;
}
public int gety1()
{
  return y1;
}
public int getx2()
{
  return x2;
}
public int gety2()
{
  return y2;
}public void setx1(int x1)
{
  this.x1 = x1;
}
public void sety1(int y1)
{
  this.y1 = y1;
}
public void setx2(int x2)
{
  this.x2 = x2;

public void sety2(int y2)
{
  this.y2 = y2;
}public abstract void draw(Graphics g);
}
****************************************************************************************
import java.awt.Graphics;public class MyLine extends MyShape        //第二个类,从上一个抽象的MyShape类派生而来。
{
   public MyLine()
   {
     super(); 
    }
    public MyLine(int x1,int y1,int x2,int y2)
    {
     super(x1,y1,x2,y2);
    }
    
    public void draw(Graphics g)
    {
     g.drawLine(getx1(),gety1(),getx2(),gety2());
    }
}
****************************************************************************************
import javax.swing.JFrame;
import java.awt.Graphics;
                                             
public class TestFrame extends JFrame                   //这是一个测试用的类。
{
  MyShape shape;
  shape = new MyLine(1,1,10,10);
public TestFrame()
{
  super("测试窗口");
}public static void main(String args[])
{
  TestFrame TF = new TestFrame();
  TF.setSize(300,300);
  TF.show();
  graphics g = TF.getGraphics();
  TF.paint(g);
}public void paint(Graphics g)
{
  shape.draw(g);                  //用shape调用draw方法,由于shape实际上指        //指向 一个MyLine  对象,所以调用的应该是
                                  //MyLine的draw方法。
}
}
*******************************************************************************************
前两个类编译都通过了。但是第三个类在编译时无法通过。出现如下错误:
D:\JCreator Pro\MyProjects\xutao\ch09\9.28\TestFrame.java:8: <identifier> expected
shape = new MyLine(1,1,10,10);
               ^
1 error<identifier>expected 按我的理解应该是指缺少了标识符,可问题是这一行怎么会缺少标识符呢?new MyLine(1,1,10,10)这行程序创建了一个
MyLine对象,应该是可以赋值给超类的引用shape的。
为了省事,只写了一个MyLine类,算是多态了,但是这个错误我怎么也想不出来是怎么回事

解决方案 »

  1.   


    import javax.swing.JFrame;
    import java.awt.Graphics;public class TestFrame extends JFrame {    MyShape shape = new MyLine(1, 1, 10, 10);    public TestFrame() {
            super("测试窗口");
        }    public static void main(String args[]) {
            TestFrame TF = new TestFrame();
            TF.setSize(300, 300);
            TF.show();
            Graphics g = TF.getGraphics();
            TF.paint(g);
        }    public void paint(Graphics g) {
            shape.draw(g); // 用shape调用draw方法,由于shape实际上指 //指向 一个MyLine
            // 对象,所以调用的应该是
            // MyLine的draw方法。
        }
    }
      

  2.   

    楼主犯了低级错误了.类的内部(方法体以外)只能有成员变量的声明语句, 当然你可以在声明的同时赋值, 但是不允许有单独的赋值语句, 把MyShape shape;
    shape = new MyLine(1,1,10,10);这两句合成一句:MyShape shape = new MyLine(1,1,10,10)或者把shape = new MyLine(1,1,10,10);这一句放到方法体中去, 都可以通过编译.