java 抽象方法默认访问权限

abstract方法的访问权限是default  ?

来源:    jdk1.7.0_80

本地新建类:MyJedisCluster,继承自JedisCluster。新建方法getConnectionFromSlot,通过卡槽slot号获取对应节点的Jedis实例。

package cn.com.myjedis;

public class MyJedisCluster extends JedisCluster {
    
    public MyJedisCluster(HostAndPort node) {
        super(node);
    }

    public Jedis getConnectionFromSlot(int slot) {
        return super.connectionHandler.getConnectionFromSlot(slot);
    }
}

JedisCluster来自jedis-2.9.0-sources.jar中的的redis.clients.jedis包中的类。

package redis.clients.jedis;

public class JedisCluster extends BinaryJedisCluster implements JedisCommands,
    MultiKeyJedisClusterCommands, JedisClusterScriptingCommands {

  public static enum Reset {
    SOFT, HARD
  }

  public JedisCluster(HostAndPort node) {
	this(Collections.singleton(node), DEFAULT_TIMEOUT);
  }

  。。。。。。。

}

JedisCluster继承自BinaryJedisCluster类。

package redis.clients.jedis;

public class BinaryJedisCluster implements BasicCommands, BinaryJedisClusterCommands,
    MultiKeyBinaryJedisClusterCommands, JedisClusterBinaryScriptingCommands, Closeable {

  public static final short HASHSLOTS = 16384;
  protected static final int DEFAULT_TIMEOUT = 2000;
  protected static final int DEFAULT_MAX_REDIRECTIONS = 5;

  protected int maxAttempts;

  protected JedisClusterConnectionHandler connectionHandler;

  public BinaryJedisCluster(Set nodes, int timeout) {
    this(nodes, timeout, DEFAULT_MAX_REDIRECTIONS, new GenericObjectPoolConfig());
  }

  public BinaryJedisCluster(Set nodes) {
    this(nodes, DEFAULT_TIMEOUT);
  }

  。。。。。。
}
本地新建类MyJedisCluster中的super.connectionHandler即来自于BinaryJedisCluster中定义。
connectionHandler是JedisClusterConnectionHandler类定义的实例。
getConnectionFromSlot方法是JedisClusterConnectionHandler类中的抽象方法。没有访问权限标识。
package redis.clients.jedis;

public abstract class JedisClusterConnectionHandler implements Closeable {
  protected final JedisClusterInfoCache cache;

  public JedisClusterConnectionHandler(Set nodes,
                                       final GenericObjectPoolConfig poolConfig, int connectionTimeout, int soTimeout, String password) {
    this.cache = new JedisClusterInfoCache(poolConfig, connectionTimeout, soTimeout, password);
    initializeSlotsCache(nodes, poolConfig, password);
  }

  abstract Jedis getConnection();

  abstract Jedis getConnectionFromSlot(int slot);

  public Jedis getConnectionFromNode(HostAndPort node) {
    return cache.setupNodeIfNotExist(node).getResource();
  }
  
  public Map getNodes() {
    return cache.getNodes();
  }

  。。。。。。
}

现象:

MyJedisCluster类中报错:

getConnectionFromSlot(int)在redis.clients.jedis.JedisClusterConnectionHandler中不是公共的; 无法从外部程序包中对其进行访问

java 抽象方法默认访问权限_第1张图片

原因:

getConnectionFromSlot是抽象方法,访问权限默认是default,只能在本包内访问。

解决:

将MyJedisCluster类放到本地新建package路径下:redis.clients.jedis;

package redis.clients.jedis;

public class MyJedisCluster extends JedisCluster {
    
    public MyJedisCluster(HostAndPort node) {
        super(node);
    }

    public Jedis getConnectionFromSlot(int slot) {
        return super.connectionHandler.getConnectionFromSlot(slot);
    }
}

以上,问题解决。

附:

牛客网上看到有人回复关于抽象类

JDK 1.8以前,抽象类的方法默认访问权限为protected

JDK 1.8时,抽象类的方法默认访问权限变为default

而我用的jdk1.7.0_80也是default。

你可能感兴趣的:(Java基础)