switch总结
来源:互联网 发布:nike高仿淘宝店铺 编辑:程序博客网 时间:2024/05/12 20:17
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 is6");break;
case7:System.out.println("this is 7");//break;
case2:System.out.println("this is 2");//break;
default:System.out.println("......");break;*/
//case 7:System.out.println("this isseven!");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 is6");break;
case7:System.out.println("this is 7");//break;
case2: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 is6");break;
case7:System.out.println("this is 7");//break;
case2:System.out.println("this is 2");//break;
default:System.out.println("......");break;*/
//case 7:System.out.println("this isseven!");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 is2");//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 is2");//break;
}
}
Output:this is 7
总结:当case语句中有符合项,default无论放什么位置,都不起作用。
程序依然执行case中相匹配语句。
关于继承构造问题:
父类:
public class Base {
publicBase(int i){
System.out.println("thisis Base class,the parameter is "+i);
}
}
子类:
public class Child extends Base {
public Child(int i) {
super(i);
System.out.println("thisis 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("thisis Child class,the parameter is "+i);
}*/
public Child(){
super(i);
System.out.println("this isChild 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("thisis Child class,the parameter is "+i);
}*/
public Child(){
// super(i);
System.out.println("this isChild 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("thisis Child class,the parameter is "+i);
}*/
public Child(String s){
super(i);
System.out.println("this isChild class,no parameter");
}
}
错误如①
子类修改如下:④
public class Child extends Base {
private int i=10;
/*public Child(int i) {
super(i);
System.out.println("thisis Child class,the parameter is "+i);
}*/
public Child(String s){
super(i);
System.out.println("this isChild class,no parameter");
}
}
错误如②
- switch总结
- Switch总结
- switch语句使用总结
- java switch语法总结
- switch使用总结
- switch语句使用总结
- 有关switch语句的总结
- switch选择语句测试总结
- OBJ-C小知识点总结,switch-case
- C语言 switch语句的使用总结
- Swift:下划线及switch使用总结
- 自己对switch 语句的理解总结
- switch—case break 语句总结
- Java中switch case 语句总结
- java基础总结四(数组、switch语句)
- 【Java】Switch-case的一些小总结
- python学习总结---switch功能实现
- switch
- Java 编程技术中汉字问题的分析及解决
- Delphi中用ADO系列控件获得存储过程的Return值
- OpenGL的 error LNK2019问题
- Java/JSP中文乱码问题解决心得
- SQL存储过程(CLR存储过程)或函数中调用.NET程序集中的方法
- switch总结
- MSP430寄存器的一些注释,带中文解释。熟知这些写起程序来很方便
- 10 things you should never do on a consulting job
- 关于继承和覆盖(Override)的一些基础知识
- java.util.date与java.sql.date
- powerdesigner 显示窗口小工具栏
- Effective Java 学习笔记(3)
- 不确定性引发的烦恼
- java常用类的使用方法