怎么做"未经检查的通用类调用?"

时间:2015-03-09 22:29:28

标签: java generics javac

我有这堂课:

package Main;

public abstract class Click123<T extends java.awt.Component>  ////////////////
{

  boolean debugging = false;

  public abstract void singleClick(java.awt.event.MouseEvent e); ////////////////
  public abstract void doubleClick(java.awt.event.MouseEvent e); ////////////////
  public abstract void tripleClick(java.awt.event.MouseEvent e); ////////////////
  public abstract void manyClick(java.awt.event.MouseEvent e);   ////////////////
  public abstract int  getFreq();// how long thread sleeps; i.e., click interval

  public Click123(T target)  ////////////////
  {
    target.addMouseListener  ////////////////
    (
      new java.awt.event.MouseAdapter()   ////////////////
      {
        Thread cp = null;

        public void mouseClicked(final java.awt.event.MouseEvent e) 
        {
          if (cp != null && cp.isAlive())
            cp.interrupt(); 

          if (e.getClickCount() == 1) 
          {
            cp =  new Thread(new ClickProcessor(new java.util.concurrent.Callable<Void>() {
              @Override public Void call() throws Exception {
                singleClick(e); //////////////////////////////////////////
                return null;
              }
            }));
            cp.start();
          }
          else if (e.getClickCount() == 2) 
          {
            cp = new Thread(new ClickProcessor(new java.util.concurrent.Callable<Void>() {
              @Override public Void call() throws Exception {
                doubleClick(e); //////////////////////////////////////////
                return null;
              }
            }));
            cp.start();
          }
          else if (e.getClickCount() == 3) 
          {
            cp =  new Thread(new ClickProcessor(new java.util.concurrent.Callable<Void>() 
            {
              @Override public Void call() throws Exception {
                tripleClick(e); //////////////////////////////////////////
                return null;
              }
              })              
            );
            cp.start();
          }
          else manyClick(e); //////////////////////////////////////////
        } // mouseClicked
      }  // new MouseAdapter
    ); // add mouseListener
  } // Click123  

  class ClickProcessor implements Runnable 
  {
    java.util.concurrent.Callable<Void> eventProcessor;

    ClickProcessor(java.util.concurrent.Callable<Void> eventProcessor) 
    {
        this.eventProcessor = eventProcessor;
    }

    @Override public void run() 
    {
      try 
      {
        System.out.println("About to sleep " + getFreq());
          Thread.sleep(getFreq()); // this value comes from implementation
          eventProcessor.call();
      } catch (InterruptedException e) { System.out.println(e);} 
        catch (Exception e)            { System.out.println(e);}
    }  // run
  }  // class ClickProcessor
} // class Click123

我从Netbeans得到的唯一警告是关于&#34;包可见的内部类&#34; ClickProcessor

我使用此命令行编译了我的项目:

javac -Xlint:unchecked -classpath main\*.java gbl\*.java

它作为原始类型Click123<T>&#34;的成员,对Click123未经检查的调用发出了警告。在几个地方,包括在这个类中的引用:

public class GridCell extends JTextField {

  int     row,
          col;
  char    content;
  Color   foreground,
          background;

  GridCell(){
    content = ' ';
    foreground = Color.BLACK;
    background = Color.WHITE;

    disableKeyCombo(KeyEvent.VK_A, KeyEvent.VK_C, KeyEvent.VK_V, KeyEvent.VK_X,
                                                                 KeyEvent.VK_H);

    new Click123(this) ////////// flagged warning unchecked call to Click123
    {
      @Override
      public void singleClick(MouseEvent e) {
        if(SwingUtilities.isRightMouseButton(e))
        {
          if( ! Game.getAvailable().contains("*"))
            Game.changeSMBorder(e.getComponent().getX(),
                                e.getComponent().getY());
          else
            Game.changeSbBackground(e.getComponent().getX(),
                                    e.getComponent().getY());
        }
        Game.btnClearBorders.setEnabled(true);
      }

      @Override public void doubleClick(MouseEvent e){
        if(SwingUtilities.isRightMouseButton(e))
        {
          if(btnUndo.isEnabled())
            btnUndo.doClick();
        }
      }

      @Override
      public void tripleClick(MouseEvent e) {
        if(SwingUtilities.isRightMouseButton(e))
        {
          if(btnRedo.isEnabled())
            btnRedo.doClick();
        }
      }

      @Override
      public void manyClick(MouseEvent e) {
      }

      @Override
      public int getFreq() {
        return CLICK_FREQUENCY;
      }
    };    
  }
  ... class goes on much further
}

如果有的话,我可以/应该做什么?

2 个答案:

答案 0 :(得分:2)

尝试更改

new Click123(this)

new Click123<GridCell>(this)

Click123是一个泛型类,这意味着它有一个类型参数,写在尖括号之间。对于Click123,类型参数T必须是Component的子类,GridCell是。

应避免使用ListSet等原始类型。原始类型是在没有类型参数的情况下使用的泛型类型。引入泛型时,只允许原始类型与泛型之前编写的代码兼容。

然而,看着你的课程,看起来没有理由认为Click123应该是通用的。构建者无法选择Component而不是T

答案 1 :(得分:1)

看起来你没有完全使用Generics试试:

new Click123<GridCell>(this)

How to fix unchecked call warning in Java?