日期:2014-05-20 浏览次数:20829 次
//供参考 class A { public void checkStr(String [] str) { try { for(String s:str) { if(s==null) throw new RuntimeException("空字符串!"); } } catch(Exception e) { e.printStackTrace(); } } } interface C { public String compareLen(String [] str); } class B extends A implements C { public static void main(String[] args) { B b = new B(); String [] str = {"a","bb","ccc","dddd"}; b.checkStr(str); System.out.println(b.compareLen(str)); } public String compareLen(String [] str) { int max = 0; String maxStr = null; for(String s:str) { if(s.length()>max) maxStr = s; } return maxStr; } }
------解决方案--------------------
public class A extends Exception { //输出异常信息 public void print() { printStackTrace(); } } class B extends A implements C { @Override public String getMax(String a, String b, String c, String d) { String res = a; if(a.length() < b.length()) res = b; if(res.length() < c.length()) res = c; if(res.length() < d.length()) res = d; return res; } } interface C { //输入 4个字符串,返回长度最大的那个 public String getMax(String a, String b, String c, String d); }