java设计模式的一个问题谁有兴趣讨论一下。

hardas 2002-05-06 01:12:54
我现在有系列的类,他们的方法名都是一样的,但方法的参数类型和返回类型一样。现在我可以做到的是写一个基类把都有的一些东西都有的方法都在基类里面实现。但我并不可以定义一个基类或接口让我一系列的类实现,你有什么好的办法?
比如:
public class classa{
public Entrya method1(){
.................
}

public void method2(Entrya eta){
.................
}
}

public class classb{
public Entryb method1(){
.................
}

public void method2(Entryb etb){
.................
}
}

现在我希望classa ,classb都有同一个接口或基类,如何实现?好象实现不了。
...全文
46 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
ngkai 2002-05-10
  • 打赏
  • 举报
回复
用接口完全可以实现。
aprim 2002-05-09
  • 打赏
  • 举报
回复
这个问题在c++中能得到很好的解决,类模板。
java现在好像还不行。如果要用可以到gj下载.

否则只好造型,或封装类。
xujiaqiang 2002-05-09
  • 打赏
  • 举报
回复
定义一个抽象类:

import java.io.Serializable;

public abstract class YourObject
implements Serializable, Cloneable
{

public YourObject()
{
}
}

所有的类都继承该类不就可以了么
hardas 2002-05-09
  • 打赏
  • 举报
回复
To:Dove_c不好意思,是我笔误。
算了,我也就不使用继承和接口了,我知道C++里面可以实现,不过很谢谢大家。同时我不喜欢使用第三方的类库。
ajoo 2002-05-09
  • 打赏
  • 举报
回复
It's no good because you'll need to do downcast. If you don't care downcast and type mismatch, then seems the common interface cannot make much sense. you have Object as the common super class anyway. Just downcast it to whatever.

To make it syntactically implements an interface really means nothing.

In short, if you don't give more details about your requirement, all those discussion can not touch the point.
keios 2002-05-09
  • 打赏
  • 举报
回复
如果 EntryA 和 EntryB 应该有相同的接口,
或者说,saveInfo 方法只需要看到他们相同的接口,那么可以这样呀:
定义interface EntryInterface
class EntryA inplements EntryInterface {...}
class EntryB inplements EntryInterface {...}

public BaseClass {
public EntryInterface getInfo(){
.................
}

public void saveInfo(EntryInterface info){
...........
}
}

class classA extends BaseClass {...}
class classB extends BaseClass {...}

dove_c 2002-05-09
  • 打赏
  • 举报
回复
不知道是我看错了,还是少打了!!

前面肯定句“他们的方法名都是一样的”

那么从“但”字看来应该是“不一样”

而你后面的提示是一样的,是不是“但”应该是“而且”!!

语文问题,我就是看得很难受,不知道为什么!!
haichuang 2002-05-09
  • 打赏
  • 举报
回复
这是典型的模板类问题,但是JAVA目前并不支持这一功能,所以只能是采用折衷的办法来实现.
alou 2002-05-09
  • 打赏
  • 举报
回复
在我看来,这个问题上类模板和Java的接口一样棒。
jimconrad 2002-05-08
  • 打赏
  • 举报
回复
adapter模式或许也行。
jimconrad 2002-05-08
  • 打赏
  • 举报
回复
使用模板类
nielinjie 2002-05-08
  • 打赏
  • 举报
回复
还可以考虑visitor模式。
class saver
public voidsave(classbase)
void savea(classbase)
void saveb(classbase)
class a extends classbase
accepted(saver visitor){
visitor.savea(this);
}
class b extends classbase
accepted(saver visitor){
visitor.saveb(this);
}

nielinjie 2002-05-08
  • 打赏
  • 举报
回复
好像可以考虑用TempletMethod,看具体的需球。
classbase
abstract classbase getInfo()
abstract void saveinfo(classbase info)
public final void save
saveinfo(getInfo());
nielinjie 2002-05-08
  • 打赏
  • 举报
回复
如果返回的类型不同的话,从概念上讲就不应该是同一个基类或者接口。为什么要这么做……
hardas 2002-05-07
  • 打赏
  • 举报
回复
因为使用一个ClassBase和使用Object有什么区别???
ajoo 2002-05-06
  • 打赏
  • 举报
回复
If the return type and parameter type are different. Why do you want a public base class or an interface? It does not make sense.

downcasting is ugly, don't do that.

By the way, parameter type is contravariant, return type is covariant, so your classa is invariant on entrya, classb is invariant on entryb.

so, there's no way you can get a common interface at all.
ferrytang 2002-05-06
  • 打赏
  • 举报
回复
请关注下面的贴子
http://www.csdn.net/expert/topic/701/701403.xml?temp=.7961542
cocia 2002-05-06
  • 打赏
  • 举报
回复
The name of method of the two classes is same,
but if the operation of the methodes are same?
For example:
the getInfo() is read the infomation from the file,and the saveInfo(..) save the infomation into the file.Then we can define a base class ClassBase,and define the ClassA derive from ClassBase.
So the ClassBase should be:
class ClassBase
{
public ClassBase getInfo();
public void saveInfo(ClassBase info);
}
Or if the the operationes are diffrent,define the each class individuality.
It's my opinion.
neek2000 2002-05-06
  • 打赏
  • 举报
回复
这是你设计上的问题,这样的类可以有更好的解决方法,比如将返回值和参数封装在另一各类里,这样不就可以使用接口和继承了吗?
hardas 2002-05-06
  • 打赏
  • 举报
回复
用Object很傻,你必须在子类中所有的参数返回结果都是Object
如下

public classA {
public Object getInfo(){
.................
}

public void saveInfo(Object info){
...........
}
}

public classB {
public Object getInfo(){
.................
}

public void saveInfo(Object info){
...........
}
}
这样要有类型的强制转换反儿有写不清晰。我之所以希望都继承一个基类就是为了结构清晰。
加载更多回复(6)

51,411

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧