论坛首页 Java企业应用论坛

挑战大家的设计,看看有什么好方法?不好描述,大家耐心一点看代码

浏览 9956 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (5)
作者 正文
   发表时间:2010-02-06   最后修改:2010-02-06
不知道猴子爬树能不能解决你的问题。。。
树枝:
public class Branch {
	
	private Long id;
	
	private Branch parent;
	
	private String name;
	
	/**
	 * 获得树
	 * @return
	 */
	public Branch getTree() {
		return parent;
	}

	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * @return the parent
	 */
	public Branch getParent() {
		return parent;
	}

	/**
	 * @param parent the parent to set
	 */
	public void setParent(Branch parent) {
		this.parent = parent;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	
}


树:
public class Tree extends Branch{

	/*
	 * (non-Javadoc)
	 * @see com.runes.test.Branch#getTree()
	 */
	@Override
	public Branch getTree() {
		return this;
	}
}


猴子:
public abstract class Monkey {

	// 当前所在的深度
	private Integer depth = 0;
	
	// 最大爬上的深度
	private Integer maxDepth = 0;
	
	// 抓住的树枝
	private Branch grabbed;
	
	/**
	 * 得到当前抓住的树枝
	 * @return 
	 */
	public Branch on() {
		return grabbed;
	}
	
	/**
	 * 爬!
	 * @param branch
	 */
	private void climbUp(Branch branch) {
		grabbed = branch;
		climbedUp(branch);
		if(this.maxDepth != -1 && this.depth < this.maxDepth) {
			this.depth++;
			for(Branch b : findGrabbableBranchs(branch)) {
				climbUp(b);
			}
			this.depth--;
		}
	}
	
	public void climbUp(Tree tree) {
		this.climbUp((Branch) tree);
	}
	
	/**
	 * @return the depth
	 */
	public Integer getDepth() {
		return depth;
	}

	/**
	 * @return the maxDepth
	 */
	public Integer getMaxDepth() {
		return maxDepth;
	}

	/**
	 * @param maxDepth the maxDepth to set
	 */
	public void setMaxDepth(Integer maxDepth) {
		this.maxDepth = maxDepth;
	}

	/**
	 * 每爬上一个新的树枝
	 * @param branch
	 */
	protected abstract void climbedUp(Branch branch);
	
	/**
	 * 寻找可抓住的树枝
	 * @param currentOn 当前所在的树枝
	 * @return 所有可抓住的树枝
	 */
	protected abstract List<Branch> findGrabbableBranchs(Branch currentOn);
}

实现一下findGrabbableBranchs方法
你的逻辑在climbedUp方法内处理一下就行了吧

程序没跑过 就提供个思路 有错轻拍。。。。
0 请登录后投票
   发表时间:2010-02-08  
一般树形使用编码比较好处理,如
一级02
二级0204
三级020406
……
要查三级直接code长度为6,要查0204的子,直接长度为6前面四位为0204的就可以了,避免了循环一类的。
当然每一级长度要根据实际情况来定
0 请登录后投票
   发表时间:2010-02-08  
这么一个原语可以不?

class TreeNode {
var $level = 0;
var $deep = 1;

var $label;
var $value;

var $node_list = array();

private function get_sub_by_value($v){
}

private function has_sub($v){
}

private function add_sub($arr) {
}

private function get_sub_by_return_value($node_list, $v) {
// 递归
}
}
0 请登录后投票
   发表时间:2010-02-09  
用的是sun现在的基本通用格式
以前老程序员:public class DynamicProxy implements InvocationHandler{            
                
    private Object object;             
    public Object bindRelation(Object object){             
        this.object = object;            
        return Proxy.newProxyInstance(object.getClass().getClassLoader(), object.getClass().getInterfaces(),this);             
    }      
0 请登录后投票
   发表时间:2010-02-09  
以前老程序员:public class DynamicProxy implements InvocationHandler
{            
                
    private Object object;             
    public Object bindRelation(Object object)
{             
        this.object = object;            
        return Proxy.newProxyInstance(object.getClass().getClassLoader(), object.getClass().getInterfaces(),this);             
   
}      
0 请登录后投票
   发表时间:2010-02-10  
根本没有看懂~~
你要做什么?
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics