Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
291 views
in Technique[技术] by (71.8m points)

java - Implementing methods from an interface but with different parameters

I am looking for a good way to have different implementations of the same method which is defined in an interface but with different parameter types. Would this be possible?

In order to clarify this, suppose I have an interface Database and two implementing classes Database1 and Database2. Database has a method createNode(...) and another one modifyNode(...). The problem is that for Database1 the return type of the createNode method should be a long (the identifier). For Database2, however, it would be an object specific from the technology (in this case OrientDB but this doesn't matter too much, it is simply something that extends Object, of course). And also both create(...) return types should be used as one of modifyNode(...) parameters.

What I was thinking to do is:

`public interface Database {
    public Object createNode(...);
    public void modifyNode(Object id, ...);
    ...
 }`

public class Database1 { 
    @Override
    public Object createNode(...) { 
        ...
        long result = // obtain id of created node
        return Long.valueOf(result);
    }

    @Override
    public void modifyNode(Object id, ...) { 
        ...
        // use id as ((Long)id).longValue();
    }
}

public class Database2 { 
    @Override
    public Object createNode(...) { 
        ...
        SomeObject result = // obtain id of created node
        return result;
    }

    @Override
    public void modifyNode(Object id, ...) { 
        ...
        // use id as (SomeObject)id
    }
}

I wanted to know if there is a better way to do this. Specially to avoid Long -> long and long -> Long conversions. I saw many similar questions here in StackOverflow but none of them were what I was looking for. Thank you very much in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's an example of Generics

Database

public interface Database<T> {
    public T createNode(...);
    public void modifyNode(T id, ...);
    ...  
}

Database1

class Database1 implements Database<Long> { 
    @Override
    public Long createNode(...) { 
        ...
        long result = // obtain id of created node
        return result;
    }

    @Override
    public void modifyNode(Long id, ...) { 
        ...
        // use id
    }
}

Database2

public class Database2 implements Database<SomeObject> { 
    @Override
    public SomeObject createNode(...) { 
        ...
        SomeObject result = // obtain id of created node
        return result;
    }

    @Override
    public void modifyNode(SomeObject id, ...) { 
        ...
        // use id as (SomeObject)id
    } 
}

Btw, don't worry about autoboxing. You are using JDK >= 5 since there are @Override annotations.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...