试图分配较弱的访问权限;是isCancelled的公共错误

时间:2014-05-14 05:46:36

标签: java boolean swingworker

所以我在SwingWorker中有了forCancelled的代码

boolean isCancelled() return true; 

它给我错误

  

尝试分配较弱的访问权限;是公共错误

我不知道如何修复它。谁能帮我?谢谢

3 个答案:

答案 0 :(得分:2)

首先,你需要在方法体周围使用大括号。

其次,您当前声明它的方式使用访问权限,但isCancelled方法是公开的,因此您必须使用其他公共方法覆盖它。

第三,该方法无论如何都是final,所以你不能首先覆盖它。目前尚不清楚你想要达到的目标,但这不是可行的方法。

答案 1 :(得分:0)

你可以像以前的海报一样试试这个:

Connecting

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The  driver has not received any packets from the server.

    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:389)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1038)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:338)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2237)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2270)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2069)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:794)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:389)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at PROKOM.Zadanie11.test.main(test.java:23)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:213)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:297)
... 15 more

但是你可能还想考虑一下你是否真的需要一个方法,当你可能只需要一个变量时:

@Override
public boolean isCancelled(){
 return true;
}

答案 2 :(得分:0)

因为如果覆盖方法,您在超类或扩展中使用相同的访问权限(package - &gt; protected - &gt; public)。

如果超类的例子:

boolean isCancelled(){}; // package local default access

protected boolean isCancelled(){};

public boolean isCancelled(){};

在子类中使用(相对):

@Override
boolean isCancelled(){}; //or add  protected or public

@Override
protected boolean isCancelled(){};//or change public

@Override
public boolean isCancelled(){};