Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Design Pattern & UML  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 [请教]一个类设计问题
href





发贴: 34
积分: 0
于 2004-10-10 10:00 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
有一个基类定义了两个对外公共抽象方法parse和wrap,这两个方法必须由子类实现。
后来从需求上发现子类不应该由构造方法来生成,因此我把构造方法写成了private。改由parse方法返回一个本身的实例。因此parse方法必须改成static,但是在基类中定义的parse抽象方法不能用static修饰,因为static和abstract冲突。
请教这种情况下应该怎么重构?
代码如下:

public abstract class DemoBase {
public abstract DemoBase parse(StringBuffer s);
public abstract String wrap();
}

-----------------

public class DemoExtend extends DemoBase {
public DemoBase parse(StringBuffer s) {
return null;
}

public String wrap() {
return null;
}
}

现在我只能改成这样,感觉有点怪怪的。

public abstract class DemoBase {
protected DemoBase() {
}

public static DemoBase parse(StringBuffer s) {
throw new UnsupportedOperationException(
"implement this method in sub-class.");
}

public abstract String wrap();
}

-----------------

public class DemoExtend extends DemoBase {
private DemoExtend() {
}

public static DemoBase parse(StringBuffer s) {
return null;
}

public String wrap() {
return null;
}
}



作者 Re:[请教]一个类设计问题 [Re:href]
主管





发贴: 448
积分: 100
于 2004-11-05 14:52 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
为什么不用另加一个static方法来得到本身的实例



flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923