EffectiveJava-方法

来源:互联网 发布:淘宝一周流量分布 编辑:程序博客网 时间:2024/05/21 10:23
/* * 38检查参数的有效性 * 后果:1.处理失败、产生异常2.正常返回、结果错误3.正常返回、破坏某对象 *  * 有些参数保存起来供以后使用,如:构造器 *  * 39 必要时保护性拷贝 * java是一门安全的语言,对于缓冲区溢出、数组越界、非法指针以及其它的内存破坏错误都自动免疫。 * 参数的保护性拷贝并不仅仅针对不可变类。每当编写方法或者构造器时,如果它要允许客户提供的对象进入到内部数据结构中,则需要 * 考虑客户提供的对象是否可变。若是,则考虑你的类在进入数据结构之后是否发生变化,若不能发生变化,则必须对对象进行保护性拷贝。 * 并让拷贝之后的对象而不是原始对象进入到数据结构中。 *  * 40 谨慎设计方法签名 * 缩短过长的参数列表: * 1.一个方法分解为多个方法,每个方法只需要这些参数的一个子集。 * 2.创建辅助类,保存参数的分组。 * 3.构造器采用Builder模式 * 参数类型优先使用借口而不是类 * */public class Item38_40 {public static void main(String[] args) {// TODO Auto-generated method stublong array[]={1,3,4};Item38_40.sort(null,4,1);}//断言public static void sort(long a[],int offset,int length){assert a != null;assert offset > 0 && offset<=a.length;assert length > 0 && length <= a.length-offset;}}final class Period {final Date start;final Date end;public Period(Date start,Date end){if(start.compareTo(end) > 0 )throw new IllegalArgumentException(start + " after "+end);this.start=start;this.end = end;}public Date getStart(){return start;}public Date getEnd(){return end;}@Overridepublic String toString() {return "Period [start=" + start + ", end=" + end + "]";}public static void main(String[] args) {Date start = new Date();Date end = new Date();Period p = new Period(start,end);System.out.println(p);start.setYear(2019);System.out.println(p);//p改变}}//进行保护性拷贝实验final class Period1 {final Date start;final Date end;public Period1(Date start,Date end){this.start = new Date(start.getTime());//保护性拷贝this.end = new Date(end.getTime());/* * 后进行有效性检查 放在保护性拷贝之后 * 避免在“危险阶段”期间从另一个线程改变类的参数。 * */if(start.compareTo(end) > 0 )throw new IllegalArgumentException(start + " after "+end);}public Date getStart(){return new Date(start.getTime());//保护性拷贝}public Date getEnd(){return new Date(end.getTime());}@Overridepublic String toString() {return "Period [start=" + start + ", end=" + end + "]";}public static void main(String[] args) {Date start = new Date();Date end = new Date();Period1 p = new Period1(start,end);System.out.println(p);p.getEnd().setHours(1);start.setYear(1);System.out.println(p);//不改变}}
/* * 41 慎用重载 * 重载--静态选择--编译时类型 * 重写--动态选择--运行时类型 *  * 对于重载方法的选择是静态的,对于重写方法的选择是动态的 * 选择被重写的方法的正确版本是在运行时进行的,选择依据:被调用方法所在对象的运行时类型。 * 当子类方法重写父类方法时,并且这个方法是在孩子类的实例上被调用,那么子类的重写方法将被执行,而不管其编译时类型。 * 在进行重写时,对于某对象 某重写方法 执行最为具体的方法 *  * 两个重载方法在同样的参数上被调用时,它们执行相同的功能,重载就不会带来危害。 * 标准做法:在一般的方法中调用具体方法 *  * 42 慎用可变参数  * 可变参数是用printf而设计 *  * 43 返回零长度的数组或集合 而不是null * 否则在客户端要有非null判断 * */class AsList{public static void main(String[] args) {List<String> l = Arrays.asList("a","b","c");System.out.println(l);//[a, b, c]List<Integer> l1 = Arrays.asList(1,23,3);System.out.println(l1);//[1, 23, 3]//asList返回的List是Array中的实现的内部类Integer [] ii = {1,23,5};List<Integer> l2=Arrays.asList(ii);System.out.println(l2);//[1, 23, 5]ii[0]=0;l2.set(1, 0);System.out.println(Arrays.asList(l2));//[0, 0, 5]int [] ints = {132,32,342,32};System.out.println(Arrays.asList(ints));//[[I@2a139a55] 把ints当成一个元素}}public class Item41_43 {public static void main(String[] args) {Set<Integer> set = new TreeSet<Integer>();List<Integer> list = new ArrayList<Integer>();for(int i= -2;i<3;i++){set.add(i);list.add(i);}System.out.println(set);//[-2, -1, 0, 1, 2]System.out.println(list);//[-2, -1, 0, 1, 2]for(int i= 0;i<3;i++){set.remove(i);//remove(object)list.remove(i);//remove(int index) remove(object)//修改:list.remove((Integer)i);}System.out.println(set);//[-2, -1]System.out.println(list);//[-1, 1]}}class CollectionClassifer {public static void classify(Collection<?> c) {System.out.print("collection ");}public static void classify(Set<?> c) {System.out.print("set ");}public static void classify(List<?> c) {System.out.print("list ");}public static void classify1(Collection<?> c){System.out.println( c instanceof Set?"set":c instanceof List?"list":"others");}public static void main(String[] args) {Collection<?> [] c = {new HashSet<String>(),new ArrayList<String>(),new HashMap<String,String>().values()};for(Collection<?> l : c){classify(l);//classify1(l);}//输出:collection collection collection /* * 调用哪个重载方法是由编译时决定的,对于for循环你的三次迭代过程中,参数的编译时类型是相同的:Collection<?>, * 每次迭代时的运行时类型都是不同的,但是并不影响对重载方法的选择。 * */}}class Wine{void name(){System.out.print("wine ");}}class Wine1 extends Wine{void name(){System.out.print("wine1 ");}}class Wine2 extends Wine1{/*void name(){System.out.print("wine2 ");}*/public static void main(String[] args) {Wine [] c = {new Wine(),new Wine1(),new Wine2()};for(Wine l : c){l.name();;}//输出:wine wine1 wine1   //执行最为具体的方法}}


0 0
原创粉丝点击