zoukankan      html  css  js  c++  java
  • javeWeb常用快捷键 Junit for changeableargs enumn reflect

    *1 工具常用的快捷键


      1) Eclipse和MyEclipse,IBM,2001,Java编写,开源,跨平台跨语言
      2)Alt+/快速内容提示
      3)Ctrl+1快速修补错误
      4)Syso+Alt+/快速显示System.out.println()语句
      5)Ctrl+Shift+F快速排版正确代码
      6)Ctrl+Shift+X转大写,Ctrl+Shift+Y转小写
      7)Ctrl+Shift+O一次性导入多个包
      8)Ctrl+/加或减单行注释
      9) Ctrl+Shift+/加多行注释,Ctrl+Shift+取消多行注释
    10) 选中行+Alt+上下光标键移动行

    *2 使用断点调试程序,使用Junit测试程序


      1)通过断点方式调式程序的每个步骤 
      2)Step over查看代码粗粒度执行过程
      3)Step into查看代码细粒度执行过程
      4)@Before和@After会在@Test方法执行前后各执行一次
      5)@BeforeClass和@AfterClass是在类前后各执行一次

    //Junit单独/同时测试多个方法
    public class Demo1 {
        @Test
        public void test1(){
            System.out.println("Junit使用");
        }
        @Test
        public void test2(){
            System.out.println("Junit调式");
        }
    }
    //Junit测试在方法前后执行
    public class Demo2 {
        @Before
        public void setUp() throws Exception {
            System.out.println("文件打开");
        }
        @Test
        public void test1(){
            System.out.println("test1()");
        }
        @Test
        public void test2(){
            System.out.println("test2()");
        }
        @After
        public void tearDown() throws Exception {
            System.out.println("文件关闭");
        }
    }
    //Junit测试在类加载前后执行
    public class Demo3 {
        @BeforeClass
        public static void setUpBeforeClass() throws Exception {
            System.out.println("setUpBeforeClass()");
        }
        @Test
        public void test1(){
            System.out.println("test1()");
        }
        @Test
        public void test2(){
            System.out.println("test2()");
        }
        @AfterClass
        public static void tearDownAfterClass() throws Exception {
            System.out.println("tearDownAfterClass()");
        }
    }
    *3 静态导入和自动装拆箱


      1)静态导入可以导入对象,属性,方法,*
      2)语法:
        import static java.lang.Math.PI;
        import static java.lang.Math.pow;
        import static java.lang.System.out;
      3)在JDK5中,编译器自动将基本类型和对应的包装类型,自动转换 
        例如:int<->Integer

    //List集合加入int型和取出Integer类型数据
    public class Demo1 {
        
        @Test
        public void test1(){
            List<String> list = new ArrayList<String>();
            list.add("jack");
            String name = list.get(0);
            System.out.println("name="+name);
        }
        
        @Test
        public void test2(){
            int i = 100;
            List list = new ArrayList();
            list.add(i);
            Integer num = (Integer) list.get(0);
            System.out.println("num="+num);
        }
    }
    *4 增强for循环

      1)循环迭代数组/List/Set/Map集合
      2)对Map集合的二种迭代方式:
        a)keySet()
        b)entrySet()
      3)在迭代集合时,一定要动态通知Iterator,而不要动态通知List集合,应选用ListIterator。

    public class Demo1 {
        @Test
        public void test1(){
            int[] is = {1,2,3,4,5};
            for(int i=0;i<is.length;i++){
                System.out.println(is[i]);
            }
        }
        @Test
        public void test2(){
            int[] is = {1,2,3,4,5};
            for(int value : is){
                System.out.print(value+"	");
            }
        }
        @Test
        public void testSet(){
            Set<Integer> set = new HashSet<Integer>();
            set.add(100);
            set.add(200);
            set.add(300);
            Iterator<Integer> it = set.iterator();
            while(it.hasNext()){
                Integer key = it.next();
                System.out.print(key+"	");
            }
        }
        @Test
        public void testList(){
            List<String> list = new ArrayList<String>();
            list.add("jack");
            list.add("marry");
            list.add("sisi");
            for(String name : list){
                System.out.print(name+"	");
            }
        }
        @Test
        public void testMap1(){
            Map<Integer,String> map = new HashMap<Integer,String>();
            map.put(100,"jack");
            map.put(200,"marry");
            map.put(300,"sisi");
            Set<Integer> set = map.keySet();
            Iterator<Integer> it = set.iterator();
            while(it.hasNext()){
                Integer key = it.next();
                String value = map.get(key);
                System.out.println(key+"-"+value);
            }
        }
        @Test
        public void testMap2(){
            Map<Integer,String> map = new HashMap<Integer,String>();
            map.put(100,"jack");
            map.put(200,"marry");
            map.put(300,"sisi");
            Set<Entry<Integer,String>> set = map.entrySet();
            Iterator<Entry<Integer,String>> it = set.iterator();
            while(it.hasNext()){
                Entry<Integer,String> entry = it.next();
                Integer key = entry.getKey();
                String value = entry.getValue();
                System.out.println(key+"<->"+value);
            }
        }
    //向List集合并发存取元素引起的问题
    public class Demo2 {
        public static void main(String[] args) {
            List<String> list = new ArrayList<String>();
            list.add("jack");
            list.add("marry");
            list.add("sisi");
            System.out.println("list前长度"+list.size());
            //Iterator<String> it = list.iterator();
            ListIterator<String> it = list.listIterator();
            while(it.hasNext()){
                String key = it.next();
                System.out.print(key+"	");
                //list.add("qq");
                //动态通知迭代器,加入了新元素,从而迭代器自动通知List集合
                it.add("qq");
            }
            System.out.println("
    list后长度"+list.size());
            it = list.listIterator();
            while(it.hasNext()){
                String key = it.next();
                System.out.print(key+"	");
            }
        }
    }
    *5 可变参数


      1)参数的个数不确定的
      2)可变参数只能放置在参数的最后,即最右边
      3)可变参数最少0个,最多1个
      4)编译器将可变参数当作一个一维数组来对待

    //可变参数
    public class Demo1 {
        public static void main(String[] args) {
            //show1(new String[]{"jack","marry","sisi","soso"});
            //show2("jack","marry","sisi","soso","berry");
            show3("qq","jack","marry","sisi","soso","berry");
            
        }
        public static void show1(String[] arr) {
            for(String a :arr){
                System.out.print(a+"	");
            }
        }
        public static void show2(String... arr) {
            for(String a :arr){
                System.out.print(a+"	");
            }
        }
        public static void show3(String qq,String... arr) {
            System.out.println("第一个参数是:" + qq);
            for(String a :arr){
                System.out.print(a+"	");
            }
        }
        /*
        public static void show4(String... arr1,String... arr2) {
        }
        */
    }
    *6 枚举


      1)当取值是某范围中的之一,这时可以使用枚举
      2)当向用户提示严重的错误时,尽可以使用抛异常的方式通知客户端
      3)可以对枚举在运行时验证
      4)可以对枚举在编译时验证
      5)枚举值相当于实例变量
      6)枚举类与普通一致,可以继承或实现,如果有抽像方法的话,可以利用每个枚举值以匿名内部类的方式去实现
      7)枚举值可以用在switch()语句中
      8)枚举类常用方法如下:
        a)name():显示枚举值
        b)ordinal():显示枚举值对应的索引,从0开始
        c)values():取得该枚举类对应的所有枚举值,是一个数组
        d)valueOf():判段某个字符串是否属性枚举值之一

    //自定义算法解决学员分数等级ABCDE(运行验证)
    public class Demo1 {
        public static void main(String[] args) throws Exception {
            Student student = new Student();
            student.setName("jack");
            student.setGrade("E");
            System.out.println("姓名:" + student.getName());
            System.out.println("等级:" + student.getGrade());
        }
    }
    
    //学生
    public class Student {
        //姓名
        private String name;
        
        //等级
        private String grade;//等级是能是{A,B,C,D,E取值之一}
        
        public Student(){}
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getGrade() {
            return grade;
        }
        public void setGrade(String grade) throws Exception {
            //判段
            if(grade!=null && grade.matches("A|B|C|D|E")){
                this.grade = grade;
            }else{
                throw new Exception();
            }
        }
    }
    //使用模拟枚举类表示学员分数等级ABCDE(编译验证)
    public class Demo1 {
        public static void main(String[] args) throws Exception {
            Student student = new Student();
            student.setName("jack");
            student.setGrade(Grade.B);
            System.out.println("姓名:" + student.getName());
            System.out.println("等级:" + student.getGrade().getTip());
            System.out.println("分数区间:" + student.getGrade().getDesc());
        }
    }
    //分数类
    public class Grade {
        public static Grade A = new Grade("A","90-100");
        public static Grade B = new Grade("B","80-89");
        public static Grade C = new Grade("C","70-79");
        public static Grade D = new Grade("D","60-69");
        public static Grade E = new Grade("E","<60");
        private String tip;
        private String desc;
        private Grade(String tip,String desc){
            this.tip = tip;
            this.desc = desc;
        }
        public String getTip() {
            return tip;
        }
        public String getDesc() {
            return desc;
        }
    }
    
    //学生
    public class Student {
        
        //姓名
        private String name;
        
        //分数类
        private Grade grade;
        
        public Student(){}
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Grade getGrade() {
            return grade;
        }
    
        public void setGrade(Grade grade) {
            this.grade = grade;
        }
        
    }
    //使用枚举类
    public class Demo1 {
        public static void main(String[] args) throws Exception {
            Student student = new Student();
            student.setName("marry");
            student.setGrade(Grade.B);
            System.out.println("姓名:" + student.getName());
            System.out.println("等级:" + student.getGrade().getTip());
            System.out.println("分数区间:" + student.getGrade().getDesc());
    
        }
    }
    //分数枚举类
    public enum Grade {
        A("A","90-100"),
        B("B","80-89"),
        C("C","70-79"),
        D("D","60-69"),
        E("E","<60");
        private String tip;
        private String desc;
        private Grade(String tip,String desc){
            this.tip = tip;
            this.desc = desc;
        }
        public String getTip() {
            return tip;
        }
        public String getDesc() {
            return desc;
        }
    }
    
    //学生
    public class Student {
        
        //姓名
        private String name;
        
        //分数类
        private Grade grade;
        
        public Student(){}
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Grade getGrade() {
            return grade;
        }
    
        public void setGrade(Grade grade) {
            this.grade = grade;
        }
        
    }
    //带抽象方法的枚举
    public class Demo1 {
        public static void main(String[] args) {
            Season season = Season.SPRING;
            //System.out.println(season.name());
            //System.out.println(season.ordinal());
            //Season[] seasons = season.values();
            //for(Season s : seasons){
            //    System.out.println(s.name());
            //}
            
            Season.valueOf(Season.class,"SPRING");
            
            
            /*season.show();
            String msg = null;
            switch(season){
                case SPRING:
                    msg = "1";break;    
                case SUMMER:
                    msg = "2";break;    
                case AUTOMN:
                    msg = "3";break;    
                case WINNTER:
                    msg = "4";break;    
            }
            System.out.println(msg);*/
        }
    }
    
    //季节枚举类
    public enum Season{
        SPRING{
            public void show(){
                System.out.println("春天");
            }
        },SUMMER{
            public void show(){
                System.out.println("夏天");
            }
        },AUTOMN{
            public void show(){
                System.out.println("秋天");
            }
        },WINNTER{
            public void show(){
                System.out.println("冬天");
            }
        };
        //抽像方法
        public abstract void show();
    }
    package cn.itcast.java.model;
    //单例/态/值设计模式[单线程情况下]
    public class Singleton {
        // NO1
        private Singleton() {
        }
        // NO2
        public static Singleton getInstance() {
            if (instance == null) {
                instance = new Singleton();
            }
            return instance;
        }
        // NO3
        private static Singleton instance;
    }
    package cn.itcast.java.model;
    
    public class Demo1 {
        public static void main(String[] args) {
            for(int i=0;i<10;i++){
                Singleton s = Singleton.getInstance();
                System.out.println(s.hashCode());
            }
        }
    }

    *7 反射

      1)解析一个类的各个部分,形成一个对象。
      2)外存中的类,加载到内存中,会形成该对象的Class类,例如:String类,加载到内存中,就是StringClass对象。
      3)使用不同的方式,取得同一个类型的字节码对象Class是相同的。
      4)通过getXxxxx()只能取得该类public的类型
        通过getDeclaredXxxxxx()可以取得该类非public的类型
        设置非public类型的可访问性,默认为false,不可访问
        c.setAccessible(true);
      5)反射可以反射private/public的构造方法,字段,普通方法
      6)在反射main方法时,编译器会将数组自动拆分,取第一个值
        解决方案:
        //m.invoke(null,(Object)new String[]{"A","B","C","D"});,将数组当作对象,此时编译器不进行拆分
        m.invoke(null,new Object[]{new String[]{"A1","B1","C1","D1"}});在数组中嵌入另一个数组

    //Class对象的比较使用==号或!=号
    public class Demo1 {
        public static void main(String[] args) throws Exception {
    
            //类名.class
            Class c1 = String.class;
            
            //对象.getClass()
            Class c2 = new String().getClass();
            
            //Class.forName()
            Class c3 = Class.forName("java.lang.String");
            
            if(c2==c3){
                System.out.println("c2==c3");
            }else{
                System.out.println("c2!=c3");
            }
        }
    }
    //含有构造方法,字段,成员方法,main方法的类
    public class Person {
        private String name;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        /*    
        public Person(){
            System.out.println("public Person()");
        }
        public Person(String name,int age){
            System.out.println("public Person(String name,int age)");
        }
        private Person(){
            System.out.println("private Person()");
        }
        protected Person(){
            System.out.println("protected Person()");
        }
        */
        public void show(){
            System.out.println("public void show()");
        }
        public void show(String[] likes,double salary){
            System.out.println("public void show(String[] likes,double salary)");
        }
        private void haha(){
            System.out.println("private void haha()");
        }
        private void xixi(){
            System.out.println("private void xixi()");
        }
        private String goodbye(){
            System.out.println("private String goodbye()");
            return "thanks";
        }
        public static void main(String[] args) {
            System.out.println("public static void main(String[] args)");
        }
    }
    //通过反射解析构造方法
    public class Demo2 {
        public static void main(String[] args) throws Exception {
            
            //取得Person类对应的字节码对象Class
            Class clazz = Class.forName("cn.itcast.java.reflect.Person");
            
            //取得该类的唯一构造方法
            //Constructor c  = clazz.getConstructor(null);
            //Constructor c = clazz.getConstructor(String.class,int.class);
            Constructor c = clazz.getDeclaredConstructor(null);
            
            //创建实例
            //Person p = (Person) c.newInstance(null);
            //Person p = (Person) c.newInstance("berry",10);
            
            //设置非public成员的访问性,默认false即不可访问性
            c.setAccessible(true);
            
            Person p = (Person) c.newInstance(null);
            
            //执行方法
            p.show();
            
        }
    }
    //通过反射解析字段
    public class Demo3 {
        public static void main(String[] args) throws Exception {
            Person p = new Person();
            Class c = Class.forName("cn.itcast.java.reflect.Person");
            Field f = c.getDeclaredField("name");
            f.setAccessible(true);
            f.set(p,"杰克");//p.setName("杰克")
            System.out.println("用户名:" + p.getName());
        }
    }
    //通过反射解析成员方法
    public class Demo4 {
        public static void main(String[] args) throws Exception {
            Class c = Class.forName("cn.itcast.java.reflect.Person");
            Constructor con = c.getConstructor(null);
            Person p = (Person) con.newInstance(null);
            Method m = c.getDeclaredMethod("goodbye",null);
            m.setAccessible(true);
            String returnValue = (String) m.invoke(p,null);
            System.out.println(returnValue);
            
            //Method m = c.getMethod("show",String[].class,double.class);
            //m.invoke(p,new String[]{"sing","dance"},5000);//new Person().show()
        }
    }
    //通过反射解析main方法 
    public class Demo5 {
        public static void main(String[] args) throws Exception {
            Class c = Class.forName("cn.itcast.java.reflect.Person");
            //获取main方法
            Method m = c.getMethod("main",String[].class);
            //m.invoke(null,(Object)new String[]{"A","B","C","D"});
            m.invoke(null,new Object[]{new String[]{"A1","B1","C1","D1"}});
            
            
        }
    }
    public class ReflectDemo {
    
        public static void main(String[] args) throws IOException,
                ClassNotFoundException, InstantiationException,
                IllegalAccessException {
            @SuppressWarnings("resource")
            BufferedReader reader = new BufferedReader(new FileReader("config.txt"));
            String line = reader.readLine();
            Class clazz = Class.forName(line);
            //Class clazz = Class.forName("com.Jordon.Reflect.MyinterImplement");
            Myinter myinter = (Myinter) clazz.newInstance();
            myinter.show();
        }
    }

    这部分代码反映了reflect的可扩展性,通过读取配置文件就能方便的更改所读取的类文件。

    package cn.itcast.java.reflect;
    
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.lang.reflect.Constructor;
    import java.lang.reflect.Method;
    import java.util.Properties;
    
    public class Demo6 {
        public static void main(String[] args) throws Exception {
            
            //加载属性文件,取得类名的方法名
            Properties props = new Properties();
            InputStream is = new FileInputStream("src/cn/itcast/java/reflect/class.properties");
            props.load(is);
            String className = props.getProperty("className").trim();
            String methodName = props.getProperty("methodName").trim();
            
            //通过反射,执行该类的方法
            Class c = Class.forName(className);
            Constructor con = c.getConstructor(null);
            Method m = c.getDeclaredMethod(methodName,null);
            m.setAccessible(true);
            m.invoke(con.newInstance(null),null);
            
        }
    }
    //-------------------------------------------------------------------
    className=cn.itcast.java.reflect.Person
    methodName=xixi
  • 相关阅读:
    POJ1659:Frogs' Neighborhood(图论)
    POJ1659:Frogs' Neighborhood(图论)
    zzuli2504: 建国的签到活动二(dfs)
    zzuli2504: 建国的签到活动二(dfs)
    zzuli2506: 建勋的魔法学院
    zzuli2506: 建勋的魔法学院
    趣学算法:沟通无限校园网 (最小生成树)
    cocoapods的使用
    MJExtension的使用
    类别
  • 原文地址:https://www.cnblogs.com/sunhan/p/3542147.html
Copyright © 2011-2022 走看看