Java基础

1 overload  重载

override  重写

2 可变参数列表

    public static int add(int ... i) {
        int sum = 0;
        for (int j : i) {
            sum += j;
        }
        return sum;
    }
View Code

3享元模式

内蕴状态和外蕴状态     i.spend(x, y)

    public static void main(String[] args) {
        Integer i1 = 0;
        Integer i2 = 0;
        System.out.println(i1 == i2);
        Integer i3 = Integer.valueOf(0);
        Integer i4 = Integer.valueOf(0);
        System.out.println(i3 == i4);
        }
    }
View Code

4 枚举

public abstract class Day {

    public static Day MON = new Day(){

        @Override
        public Day next() {
            // TODO Auto-generated method stub
            return TUE;
        }
        public String toString() {
            return "MON";
        }
    };
    
    public static Day TUE = new Day(){

        @Override
        public Day next() {
            // TODO Auto-generated method stub
            return MON;
        }
        
        public String toString() {
            return "TUE";
        }
    };
    public abstract Day next();
}
View Code
public enum Day {

    MON(20) {
        @Override
        public Day next() {
            // TODO Auto-generated method stub
            return TEU;
        }
    }, TEU(40) {
        @Override
        public Day next() {
            // TODO Auto-generated method stub
            return MON;
        }
    };
    
    private int time;
    private Day(int time) {this.time = time;}
    public abstract Day next();
}
View Code

 5 数组类

具有相同数据类型和纬度的数组属于同一个类。

    public static void main(String[] args) {
        int[] a = {1,2,3};
        String[] b = {"1", "2", "3"};
        Object[] a1 = a; // error
        Object[] b1 = b;
        
    }
     cannot convert from int[] to Object[]
    private static void printObject(Object o) {
        // TODO Auto-generated method stub
        Class c = o.getClass();
        if (c.isArray()) {
            int length = Array.getLength(o);
            for (int i = 0; i < length; i++) {
                System.out.println(Array.get(o, i));
            }
        } else {
            System.out.println(o);
        }
    }
View Code

6 hash集合修改结合中的元素可能导致内存泄漏,因为修改元素后,hash值有可能修改,就找不到原来对象,所以不能删除了

public class Mytest {

    public static void main(String[] args) {
        Set<Point> p = new HashSet<>();
        Point p1 = new Point(2, 2);
        Point p2 = new Point(2, 3);
        p.add(p1);
        p.add(p2);
        System.out.println(p.size());
        p1.x = 10;
        p.remove(p1);
        System.out.println(p.size());
    }
}
class Point {
    int x;
    int y;
    public Point(int x, int y) {
        super();
        this.x = x;
        this.y = y;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + x;
        result = prime * result + y;
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Point other = (Point) obj;
        if (x != other.x)
            return false;
        if (y != other.y)
            return false;
        return true;
    }
    
}
View Code

 7 小框架

public class Mytest {

    public static void main(String[] args) throws Exception {
        InputStream ips = new FileInputStream("config.properties");
        Properties pr = new Properties();
        pr.load(ips);
        String name = pr.getProperty("className");
        Collection p = (Collection) Class.forName(name).newInstance();
        Point p1 = new Point(2, 2);
        Point p3 = new Point(2, 2);
        Point p2 = new Point(2, 3);
        p.add(p1);
        p.add(p2);
        p.add(p3);

        System.out.println(p.size());
    }
}
View Code

 8 JavaBean就是值对象,用于设置和访问对象的值,没有别的方法,通常用于传递数据

    public static void main(String[] args) throws Exception {
        
        Point p = new Point(0, 1);
        String property = "x";
        PropertyDescriptor descriptor = new PropertyDescriptor(property, p.getClass());
        Method readMethod = descriptor.getReadMethod();
        System.out.println(readMethod.invoke(p));
    }
View Code

9 注解就相当于某种标记,加了注解就等于为程序打上了某种标记,以后编译器,开发工具和其他程序可以通过反射来查看程序加的标记来做相应的事情。

@MyAnnotation(second=@SecondAnnotation("123"), value="123")
public class Mytest {

    public static void main(String[] args) throws Exception {
        Class c = Mytest.class;
        if (c.isAnnotationPresent(MyAnnotation.class)) {
            MyAnnotation my = (MyAnnotation) c.getAnnotation(MyAnnotation.class);
            System.out.println(my.value());
            System.out.println(my.arrs().length);
            System.out.println(my.second().value());
        }
    }
}
View Code

10  通配符? 解决

    public static void main(String[] args) throws Exception {
        List<Object> list = new ArrayList<Integer>();  // compline error  
        List<?> list2 = new ArrayList<Integer>();  // right
    }
View Code
    private static <T> void fillArray(T[] a, T obj) {
        for (int i = 0; i < a.length; i++) {
            a[i] = obj;
        }
    }
View Code

11 aop面向切面编程,系统中存在一些共同的业务,安全,事务,日志。使交叉业务模块化,

原文地址:https://www.cnblogs.com/whesuanfa/p/7868266.html