毕设前奏(java回顾)

package com.company;


import java.util.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class Main {

    public static void print(int index,Object obj){
        System.out.println(String.format("%d,%s",index,obj.toString()));
    }

    public static void demoList(){
        List<String> strList = new ArrayList<String>();
        for(int i = 0; i <4 ; i++){
            strList.add(String.valueOf(i));//Change to String
        }
        print(1,strList);
        List<String> strListB = new ArrayList<String>();
        for(int i = 0; i <4 ; i++){
            strListB.add(String.valueOf(i*i));//Change to String
        }
        strList.addAll(strListB);
        print(2,strList);
        strList.remove(0);
        print(3,strList);
        strList.remove(String.valueOf(1));
        print(4,strList);
        Collections.sort(strList);
        print(6,strList);
        Collections.sort(strList, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o2.compareTo(o1);
            }
        });
        print(7,strList);
        Collections.reverse(strList);
        print(8,strList);
        int[] array = new int[]{1,2,3};
        print(9,array[1]);

    }

    public static void demoSet(){
        Set<String>strSet = new HashSet<String>();
        for(int i=0;i<3;i++){
            strSet.add(String.valueOf(i));
            strSet.add(String.valueOf(i));
            strSet.add(String.valueOf(i));
        }
        print (1,strSet);
        strSet.addAll(Arrays.asList(new String[]{"A","B","C"}));
        print(4,strSet);
        for(String value:strSet){
            print(5,value);
        }

    }

    public static void demoKeyValue(){
        Map<String,String>map = new HashMap<>();
        for(int i=0;i<4;i++){
            map.put(String.valueOf(i),String.valueOf(i*i));
        }
        print(1,map);
        for(Map.Entry<String,String>entry:map.entrySet()){
            print(2,entry.getKey()+":"+entry.getValue());
        }
        print(2,map.keySet());
        print(3,map.replace("1","A"));
        print(4,map.containsKey("2"));
        print(5,map.containsValue("9"));
        print(6,map);
    }

    public static void demoException(){
        try{
            print(1,"Hello");
            int a = 2;
            a = a / 0;//除零异常,跳转到4;下面代码不执行;
            String b = null;//若执行到此处代码,为null异常,跳转到3;
            b.indexOf('a');
        }catch(NullPointerException npe){
            print(3,"NPE");
        } catch(Exception e){
            print(4,"Exception");
        }finally{//所有都会经过Finally,做清理工作;
            print(2,"finally");
        }
    }

    public static void demoCommon() {
        //salt uuid
        Random random = new Random();
        random.setSeed(1);
        for (int i = 0; i < 4; i++) {
            print(1, random.nextInt(100));
            print(2, random.nextDouble());
        }
        List<Integer> array = Arrays.asList(new Integer[]{1, 2, 3, 4, 5});
        print(3, array);
        Collections.shuffle(array);
        print(4, array);

        Date date = new Date();
        print(5, date);
        print(6, date.getTime());
        DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        print(7, df.format(date));
        print(8, DateFormat.getDateInstance(DateFormat.DEFAULT).format(date));

        print(9, UUID.randomUUID());
        print(10, Math.max(1, 2));
        print(11, Math.ceil(2.2));
        print(12, Math.floor(2.5));
        print(13, Math.pow(4, 2));
    }

    public static void demoClass(){
        Talk animal = new Animal("Jim",1);
        animal.say();
        animal=new Human("Lei",11,"CN");
        animal.say();
        Talk human=new Human("Han",11,"Am");
        human.say();
    }

    public static void main(String[] args) {
        /*System.out.println("1");
        String str="hello world";
        print(1,str.replace('o','l'));
        print(2,str.indexOf('o'));
        print(3,str.toUpperCase());
        print(4,str.lastIndexOf('o'));*/
//        demoList();
//        demoSet();
//        demoKeyValue();
//        demoException();
//        demoCommon();
        demoClass();
    }
}

package com.company;

public class Animal implements Talk{
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    private int age;
    public Animal(String name,int age){
        this.name=name;
        this.age=age;
    }
    public void say(){
        System.out.println("This is Animal");
    }

}

package com.company;

public class Human extends Animal{//继承
    private String country;
    public Human(String name,int age,String country){
        super(name,age);
        this.country=country;
    }

    public void say(){//多态
        System.out.println("This is "+getName()+" from "+country);
    }
}

package com.company;

public interface Talk {
    void say();
}

猜你喜欢

转载自blog.csdn.net/kxg6666/article/details/79604775