gpt4 book ai didi

java - 子类不能调用父类的public方法

转载 作者:行者123 更新时间:2023-12-02 11:04:40 25 4
gpt4 key购买 nike

我有一个名为 Foo 的抽象类,它实现了 Bar 接口(interface)。 Foo 确实为 Bar 方法提供了 public 实现。

现在我有了扩展 FooFooBar 类。奇怪的是,我现在必须在 FooBar 中实现 Bar 接口(interface)。接下来让我感到奇怪的是,我无法在 FooBar 中调用 super.barMethod(),因为这会引发以下错误:

Cannot directly invoke the abstract method barMethod for the type Bar

更奇怪的是,Bar 有 10 多个方法,但我只需在 FooBar 中实现 2 个。

有什么想法为什么会发生这种情况以及如何解决它吗?

<小时/>

代码:

Foo 类:

public abstract class PersistentEnumUserType<T extends PersistentEnum>
implements UserType {

/** The persistent enum type */
private Class<T> persistentClass;

@SuppressWarnings("unchecked")
public PersistentEnumUserType() {
this.persistentClass = (Class<T>) findParameterizedType(getClass())
.getActualTypeArguments()[0];
}

private ParameterizedType findParameterizedType(
@SuppressWarnings("rawtypes") Class clazz) {
if (clazz == null) {
return null;
}

Type type = clazz.getGenericSuperclass();
if (type instanceof ParameterizedType) {
return (ParameterizedType) type;
}
return findParameterizedType(clazz.getSuperclass());
}

public Object assemble(Serializable cached, Object owner)
throws HibernateException {
return cached;
}

public Object deepCopy(Object value) throws HibernateException {
return value;
}

public Serializable disassemble(Object value) throws HibernateException {
return (Serializable) value;
}

public boolean equals(Object x, Object y) throws HibernateException {
return x == y;
}

public int hashCode(Object x) throws HibernateException {
return x == null ? 0 : x.hashCode();
}

public boolean isMutable() {
return false;
}

public Object nullSafeGet(ResultSet rs, String[] names,
SessionImplementor session, Object owner)
throws HibernateException, SQLException {
int id = rs.getInt(names[0]);
if (rs.wasNull()) {
return null;
}
for (PersistentEnum value : returnedClass().getEnumConstants()) {
if (id == value.getCode()) {
return value;
}
}
throw new IllegalStateException("Unknown "
+ returnedClass().getSimpleName() + " id");
}

public void nullSafeSet(PreparedStatement st, Object value, int index,
SessionImplementor session) throws HibernateException, SQLException {
if (value == null) {
st.setNull(index, Types.INTEGER);
} else {
st.setInt(index, ((PersistentEnum) value).getCode());
}
}

public Object replace(Object original, Object target, Object owner)
throws HibernateException {
return original;
}

public Class<T> returnedClass() {
return persistentClass;
}

public int[] sqlTypes() {
return new int[] { Types.INTEGER };
}

}

Bar界面:

public interface UserType {

int[] sqlTypes();

Class returnedClass();

boolean equals(Object x, Object y) throws HibernateException;

int hashCode(Object x) throws HibernateException;

Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException;

void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException;

Object deepCopy(Object value) throws HibernateException;

boolean isMutable();

Serializable disassemble(Object value) throws HibernateException;

Object assemble(Serializable cached, Object owner) throws HibernateException;

Object replace(Object original, Object target, Object owner) throws HibernateException;
}

当前 FooBar 类应实现 2 个方法(nullSafeGetnullSafeSet):

public class FileTransferJobUserType extends
PersistentEnumUserType<FileTransferJob> {

}

最佳答案

nullSafeGet(...)nullSafeSet(...) 未在抽象类中实现,而是重载:它们不具有相同的参数类型,例如SessionImplementor 而不是 SharedSessionContractImplementor

关于java - 子类不能调用父类的public方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51062472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com