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");
}
}
错误如②