请教一个Java接口的理解问题

wxcm2 2014-12-20 05:01:28
初学Java ,对于接口的理解不是很透,恳请各位大神指导!

我想写一个从网站上抓新闻的程序,暂时抓取文章的 题目,作者,字数

写了如下的代码,但不知这样是否体现了java里接口的思想
思路如下:
1.定义接口InfoCommunication ,实现发送数据 和 接受数据
2.定义两个类 : 一个 MyFrame类继承自 JFrame类,用于实现窗口显示数据;另外一个类InfoData用于从网页上抓取数据
3. InfoData的对象 把抓下来的数据send 给 MyFrame类的对象
4. MyFrame的对象收取数据,用于显示在窗口上
5. Test 是程序入口点
6. 在debug模式下可以看到frame 里已经收到了来自data的数据

问题如下:
1. 我是这样体现接口的 frame.receiving(data.sending()); , 但是如果在InfoData 里写一个sending方法,在MyFrame 里写一个receiving方法是不是也能达到这样的目的? 总之该怎样体现接口的思想呢?
2. frame 里只用到了receiving 方法, data里只用到了sending方法,这种情况是不是应该把InfoCommunication写成抽象类比较好?就像mouseListener 和 mouseAdaptor 那样的关系?

希望各位前辈大神们给予指导,谢谢了!


// Interface
public interface InfoCommunication {
public Info sending();
public void receiving(Info info);
}

// Entrance
public class Test {
public static void main(String[] args) {
InfoCommunication frame = new MyFrame();
InfoCommunication data = new InfoData();
frame.receiving(data.sending());
}
}

// The class to show the data which is grabbed from Internet
public class MyFrame /*extends JFrame*/implements InfoCommunication{
Info info ;
public MyFrame() {
info = new Info();
}

@Override
public Info sending() {
//none
return null;
}

@Override
public void receiving(Info info) {
this.info = info;
}
}

// The class which aims to grab data
public class InfoData implements InfoCommunication {
Info info;
InfoData(){
info = new Info();
this.grabInfo();
}

private void grabInfo(){
//garb information from Internet, assuming that:
//author: Tom
//title: Passage title
//wordsCount: 1000
info.setAuthor("Tom");
info.setTitle("Passage title");
info.setWordsCount("1000");
}

@Override
public Info sending() {
return this.info;
}

@Override
public void receiving(Info info) {
//None
}
}
...全文
383 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wxcm2 2015-02-15
  • 打赏
  • 举报
回复
谢谢各位的解答,抱歉现在才结贴给分= =~~
as扬沙 2014-12-23
  • 打赏
  • 举报
回复
package test7;

public class MianXiangJieKou
{
	public static void main(String[] args)
	{
		Book b1 = new Novel();
		Book b2 = new Magazine();
		Mather mather = new Mather();
		
		mather.read(b1);
		mather.read(b2);
 	}
}

interface Book
{
	public void read();
}
class Novel implements Book
{
	@Override
	public void read()
	{
		System.out.println("读小说内容");
	}
}
class Magazine implements Book
{
	@Override
	public void read()
	{
		System.out.println("读杂志内容");
	}
}
class Mather
{
	//mather能读书,不论是小说或者杂志。只要这本读物实现了Book都能被mather读。
	//这就是面向接口的好处。
	public void read(Book b)
	{
		b.read();
	}
}
接口, 你可以看做是电脑的USB接口。定义了某个功能的名字, 然后具体的那个插件,只要插上去就可以实现相应的功能。
  • 打赏
  • 举报
回复
上面的大婶说的对
乔不思 2014-12-23
  • 打赏
  • 举报
回复
接口是对 不同事务 共同 具有的“动作”的抽象, 建立接口,然后让他们实现这个定义在接口中的“动作”,他们就拥有的各自的不同的“动作”内容,但是这个动作的名字是一样的,只不过各自的实现不同罢了
wxcm2 2014-12-20
  • 打赏
  • 举报
回复
忘记了贴 Info类的定义、

public class Info {
	String title = "non-init";
	String author = "non-init";
	String wordsCount = "non-init";
	
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public String getWordsCount() {
		return wordsCount;
	}
	public void setWordsCount(String wordsCount) {
		this.wordsCount = wordsCount;
	}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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