面向对象风格的-KWIC

1、什么是面向对象风格?什么是面向对象风格?什么是面向对象风格?
看图:
在这里插入图片描述
这个图来自博客:https://blog.csdn.net/qq_41626229/article/details/85930248?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522160603771419725225056063%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=160603771419725225056063&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_v2~rank_v28-3-85930248.first_rank_ecpm_v3_pc_rank_v2&utm_term=%E4%BD%93%E7%B3%BB%E7%BB%93%E6%9E%84%E5%87%A0%E5%A4%A7%E9%A3%8E%E6%A0%BC&spm=1018.2118.3001.4449
感谢这位博主!!!

对象之间通过函数调用和消息传递实现交互,在一个对象中调用另一个对象的方法,从而使得各个对象之间相互交互,这就是面向对象风格。
好看代码!
直接敲,因为已经有啦基于过滤器风格的kwic
第一个类,对数据进行封装Text,也就是创建一个类用来,他的对象就保存着当前文本的所有内容。
Text类

package com.ren.cn;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

/**
 * 
 * @author huihui
 *  这是一个文本类,专门负责构建文本对象,读取数据并构建数据的对象
 */
public class Text {
	private String text;
	private BufferedReader bufread=null;
	private ArrayList<String> list=new ArrayList<String>();
	private String s=" ";
	public String getText() {
		return text;
	}
	public void setText(String text) {
		try {
			bufread= new BufferedReader(new FileReader(text));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			while((s=bufread.readLine())!=null) {
				this.text=s+"\n"+bufread.readLine();
				}
//			System.out.println("读入文件\n"+this.text+"\n读入文件没有问题\n");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

2、第二个类

package com.ren.cn;

import java.util.ArrayList;
import java.util.StringTokenizer;

/**
 * 
 * @author huihui
 * 进行移位操作
 */
public class Move {
	private Text tet = null;
	private static ArrayList<String> text=new ArrayList<String>();
	private  ArrayList<String> texts=new ArrayList<String>();
	private String s="";
	public void setText(String text) {
		tet = new Text();
		tet.setText(text);
		text = tet.getText();
		StringTokenizer tokener = new StringTokenizer(text, "\n");
		String token = new String();
		int index;
		ArrayList<String> tokens = new ArrayList<String>();
		int count = tokener.countTokens();
		for (int j = 0; j < count; j++) {// 将一行解析,并且将解析的word加入ArrayList中
			token = tokener.nextToken();
			tokens.add(token);
			
		}
//		System.out.println("切分形成句子\n"+tokens.get(1));//没有问题
		for(int i=0;i<tokens.size();i++) {
			this.text.add(mmove(tokens.get(i)));
		}
        System.out.println(this.text.size());
	}
	private static String mmove(String s) {
		StringTokenizer tokener = new StringTokenizer(s, " ");
		// 2、进行移位操作
		String token = new String();
		int index;
		ArrayList<String> tokens = new ArrayList<String>();
		int count = tokener.countTokens();
		for (int j = 0; j < count; j++) {// 将一行解析,并且将解析的word加入ArrayList中
			token = tokener.nextToken();
			tokens.add(token);
		}
		ArrayList<String> kwicList = new ArrayList<String>();
		// 对ArrayList中的字进行循环移位,得出最后结果
		for (int i = 0; i < count; i++) {
			index = i;
			StringBuffer linebuffer = new StringBuffer();
			for (int j = 0; j < count; j++) {
				if (index >= count)
					index = 0;
				linebuffer.append(tokens.get(index));
				linebuffer.append(" ");
				index++;
			}
			String line = linebuffer.toString();
			kwicList.add(line);
		}
		for(int i=1;i<kwicList.size();i++) {
			s=s+"\n"+kwicList.get(i);			
		}
//		System.out.println("movezhong \n"+s);
		return s;
	}
	public static ArrayList<String> getText() {
		return text;
	}
}

操作来了,看啊,很快啊,我在这个类的方法中,直接new了一个对象tet = new Text();,然后在这个类中的这个方法中调用由上一个类new产生的对象的方法。这不就是在一个对象中使用另一个对象的方法吗
3、这里这个类也是如此(这里完成排序)

package com.ren.cn;

import java.util.ArrayList;
import java.util.StringTokenizer;

/**
 * 
 * @author huihui
 * 进行排序处理
 */
public class Inorder {
	private String text=null;
	private String a="";
	private ArrayList<String> texts;
	private Move mo=null;
	private ArrayList<String> tokens = new ArrayList<String>();
	public String getText() {
		return text;
	}

	public void setText( String text) {
		mo=new Move();
		mo.setText(text);
		texts=mo.getText();
//		System.out.println(texts.get(0));
		for(String ss:texts) {
			StringTokenizer tokener = new StringTokenizer(ss, "\n");
			String token = new String();
			int index;
			int count = tokener.countTokens();
			for (int j = 0; j < count; j++) {// 将一行解析,并且将解析的word加入ArrayList中
				token = tokener.nextToken();
				tokens.add(token);				
			}
		}
		tokens.sort(null);
		for(String ss:tokens) {
			a=a+ss+"\n";
		}
		this.text = a;
	}
	
}

4、进行测试

package com.ren.cn;
/**
 * 
 * @author huihui
 * 主方法进行测试
 */
public class test {
	public static void main(String[] args) {
		Inorder in=new Inorder();
		in.setText("main.txt");
		System.out.println(in.getText());
	}
}

创建一个对象,调用其方法在其方法中又会创建其他的对象,,调用其他对象的方法。层层递进。
5、最后,当然要给出一个uml图了!!!
在这里插入图片描述

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值