楼主的意思是不是CalendarFeed实现接口A?如果是的话,在CalendarFeed再加上implements A就可以了,你自己可以试一试看
A a = new CalendarFeed();
a.start()也是可以的。如果不是,那我就不明白你到底要干什么,呵呵~~~
A a = new CalendarFeed();
a.start()也是可以的。如果不是,那我就不明白你到底要干什么,呵呵~~~
解决方案 »
- 关于GregorianCalendar时区的问题???
- 在JPanel上画图片的问题
- 关于Timestamp的问题
- 为什么JAVA写的SWT程序用工具生成EXE速度 超级慢
- 求助:关于swing中的JsplitPane的问题
- 求jTree中取得子节点的int值,并能将新的node插入到节点下。
- 请大家说说--专科(非计算机毕业)能继续JAVA程序的工作吗
- 有大连理工大学软件学院的吗???
- 请问用java通过pop3协议如何获取信件内容.
- 求大神帮我用java编写一个课程表要求可以编辑和展示课程名称,周次,单双周,地点,老师
- 在se中如何将某个文件夹下面的所有文件都以abc0,abc1,abc2的方式命名
- jtable中如何对某一行的字体和颜色进行改变?
public class CalendarFeed implements A
{ private static FeedThread thread;//线程成员 public CalendarFeed()
{
} public void start() //开启推送服务
{
if (thread == null)
{
thread = new FeedThread();
thread.start();
}
} public void stop() //结束推送服务
{
thread.running = false;
thread = null;
} public static class FeedThread extends Thread
{ public boolean running = true; public void run()
{ while (running)
{
try
{
//业务逻辑
Thread.sleep(5000);//五秒推送一次
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
}
interface A{
void start();
void stop();
}
public class FatoryModule{
public A getA(String name){
if(name.equals("CalendarFeed")){
return new CalendarFeed();
}else if(){....}
}
}
这样对不对呢?
接口里面是否要写一个属性?
比如像这样:interface A{
private static FeedThread thread;
void start();
void stop();
}
void a();
}
footBall implements ballFacotroy {
void a(){
......
}
}BascketBall implements ballFacotroy {
void a(){
..........
}
}test{
public static void main(){
ballFacotroy basketball=new BasketBall();
ballFacotroy footBall=new FootBall();
basketball.a()
footBall.a()
}
}设计模式心理清楚就可以了,其实不需要为了用设计模式而使用设计模式!
abstract class A implements Runnable {
public boolean running = true;
public void doSomething();
public void run() {
...
doSomething();
...
}
}然后CalendarFeed的构造方法public CalendarFeed(A a){}
个人感觉这样更好点。
abstract class A implements Runnable {
public boolean running = true;
public void doSomething();
public void run() {
...
doSomething();
...
}
}然后CalendarFeed的构造方法public CalendarFeed(A a){}
个人感觉这样更好点。
{ while (running)
{
try
{
doSomething();//业务逻辑
Thread.sleep(5000);//五秒推送一次
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}也就是说抽象类里面的run方法几乎就纯粹调用doSomething(),然后每个子类去实现究竟该干啥事儿。
抽象类中可以实现具体方法,若不想让该方法实现的话,加上前缀abstract。实际上接口是全部一种特殊的抽象类,里面所有的方法都需要子类实现的。
而真正意义上的抽象类则有抽象方法,也可以有已被实现的方法。但是由于java是单继承特性,所以接口、抽象类就要自己看看究竟使用哪种合适了
start();和stop();基本实现都是一样的!
唯独不一样的就是
public static class FeedThread extends Thread
{ public boolean running = true; public void run()
{ while (running)
{
try
{
//业务逻辑
Thread.sleep(5000);//五秒推送一次
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
} 就是业务逻辑的实现不一样!因为每个模块负责做不同的事情!
就因为它是静态内部类,所以我才不知道该如何去设计那个抽象类!
this.doing = doing;
} public void start() // 开启推送服务
{
if (doing != null) {
Thread thread = new Thread(doing);
thread.start();
}
} public void stop() // 结束推送服务
{
doing.running = false;
doing = null;
}
public static void main(String[] args) {
CalendarFeed feed = new CalendarFeed(new FeedThread2());
feed.start();
try {
Thread.sleep(2000);//
} catch (InterruptedException e) {
e.printStackTrace();
}
feed.stop();
}
}abstract class Doing implements Runnable {
protected boolean running = true;
public abstract void doSomething();
public void run() {
while (running) {
try {
doSomething();// 业务逻辑
Thread.sleep(500);// 五秒推送一次
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}class FeedThread1 extends Doing {
public void doSomething() {
System.out.println("this is FeedTread1");
}
}class FeedThread2 extends Doing {
public void doSomething() {
System.out.println("this is FeedTread2");
}
}你试试这东西是不是你想要的效果。