switch总结

switch

语句的判断条件可以接受int,byte,char,short,
不能接受其他类型。
Case

后面跟常数或者常量。
Switch

语句总结如下:
(一) 

public void testSwitch(){

       int i=7;

      

       switch (i){

          

           case 0:System . out .println( "this is zero!" ); break ;

           case 1:System . out .println( "this is one!" ); break ;

            default :System . out .println( "the item is default!" ); break ;

           case 3:System . out .println( "this is 3" ); break ;

           case 4:System . out .println( "this is 4" ); break ;

           case 5:System . out .println( "this is 5" ); break ;

          

          /* case 6:System.out.println("this is 6");break;

           case 7:System.out.println("this is 7");//break;

           case 2:System.out.println("this is 2");//break;

           default:System.out.println("......");break;*/

           //case 7:System.out.println("this is seven!");break;

       }

    }

Output: the item is default!

------------------------------------------------------------------------------------

public void testSwitch(){

       int i=7;

      

       switch (i){

           default :System. out .println( "the item is default!" ); break ;

           case 0:System. out .println( "this is zero!" ); break ;

           case 1:System. out .println( "this is one!" ); break ;

          

           case 3:System. out .println( "this is 3" ); break ;

           case 4:System. out .println( "this is 4" ); break ;

           case 5:System. out .println( "this is 5" ); break ;

          

          /* case 6:System.out.println("this is 6");break;

           case 7:System.out.println("this is 7");//break;

           case 2:System.out.println("this is 2");//break;

           default:System.out.println("......");break;*/

           //case 7:System.out.println("this is seven!");break;

       }

    }

Output: the item is default!

------------------------------------------------

public void testSwitch(){

       int i=7;

      

       switch (i){

          

           case 0:System. out .println( "this is zero!" ); break ;

           case 1:System. out .println( "this is one!" ); break ;

          

           case 3:System. out .println( "this is 3" ); break ;

           case 4:System. out .println( "this is 4" ); break ;

           case 5:System. out .println( "this is 5" ); break ;

           default :System. out .println( "the item is default!" ); break ;

          /* case 6:System.out.println("this is 6");break;

           case 7:System.out.println("this is 7");//break;

           case 2:System.out.println("this is 2");//break;

           default:System.out.println("......");break;*/

           //case 7:System.out.println("this is seven!");break;

       }

    }

Output: the item is default!

总结: java switch 语句中当没有相匹配 case 语句, default 执行与代码位置无关,都将执行 default 语句。

 

 

(二)

public void testSwitch(){

       int i=7;

      

       switch (i){

          

           case 0:System. out .println( "this is zero!" ); break ;

           case 1:System. out .println( "this is one!" ); break ;        

           case 3:System. out .println( "this is 3" ); break ;

           case 4:System. out .println( "this is 4" ); break ;

           case 5:System. out .println( "this is 5" ); break ;

           case 6:System. out .println( "this is 6" ); break ;

           case 7:System. out .println( "this is 7" ); break ;

           //case 2:System.out.println("this is 2");//break;

           default :System. out .println( "this is default!" ); break ;

      

       }

    }

Output this is 7

--------------------------------------

public void testSwitch(){

       int i=7;

      

       switch (i){

          

           case 0:System. out .println( "this is zero!" ); break ;

           case 1:System. out .println( "this is one!" ); break ;        

           case 3:System. out .println( "this is 3" ); break ;

           case 4:System. out .println( "this is 4" ); break ;

          

           default :System. out .println( "this is default!" ); break ;

          

           case 5:System. out .println( "this is 5" ); break ;

           case 6:System. out .println( "this is 6" ); break ;

           case 7:System. out .println( "this is 7" ); break ;

           //case 2:System.out.println("this is 2");//break;

          

      

       }

    }

Output this is 7

总结:当 case 语句中有符合项, default 无论放什么位置,都不起作用。

程序依然执行 case 中相匹配语句。

 

 

 

 

 

 

关于继承构造问题:

父类:

public class Base {

   

       public Base(int i){

              System.out.println("this is Base class,the parameter is "+i);

       }

}

 

子类:

public class Child extends Base {

 

      

         public Child(int i) {

              super(i);

              System.out.println("this is Child class,the parameter is "+i);

       }

}

 

测试类:

public class Test{

      public static void main(String args[]){

            Child ch=new Child(112);

      }

}

Output this is Base class,the parameter is 112

       this is Child class,the parameter is 112

 

子类修改如下:①

public class Child extends Base {

        private int i=10;

      

       /* public Child(int i) {

              super(i);

              System.out.println("this is Child class,the parameter is "+i);

       }*/

 

        public Child(){

              super(i);

              System.out.println("this is Child class,no parameter");

        }

}

>javac Child.java

Child.java:10 :无法在调用父类型构造函数之前引用 i

             super(i );

1 错误

 

 

子类再次修改:②

public class Child extends Base {

        private int i=10;

      

       /* public Child(int i) {

              super(i);

              System.out.println("this is Child class,the parameter is "+i);

       }*/

 

        public Child(){

             // super(i);

              System.out.println("this is Child class,no parameter");

        }

}

>javac Child.java

Child.java:9: 找不到符合

符合:构造函数 Base()

位置:类 Base

      Public Child(){

1 错误

 

 

子类修改如下:③

public class Child extends Base {

        private int i=10;

      

       /* public Child(int i) {

              super(i);

              System.out.println("this is Child class,the parameter is "+i);

       }*/

 

        public Child(String s){

              super(i);

              System.out.println("this is Child class,no parameter");

        }

}

错误如①

 

 

子类修改如下:④

public class Child extends Base {

        private int i=10;

      

       /* public Child(int i) {

              super(i);

              System.out.println("this is Child class,the parameter is "+i);

       }*/

 

        public Child(String s){

              super(i);

              System.out.println("this is Child class,no parameter");

        }

}

错误如②

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值