一、枚举
-
枚举指由一组固定的常量组成的类型
-
优点:类型安全;易于输入;代码清晰
[Modifier]enum enumName{
enumContantName1[,
enumConstantName2...[;]]
//[field,method]
}
需求:根据用户给的数字,告知该数字对应的日期该做什么事情
具体实现
package cn.lch.enums;
//根据用户给的数字,告知该数字对应的日期该做什么事情
//方法一:if判断
public class Test {
public void doWhat(int day){
if(day>7 || day<1){
System.out.println("输入数字错误,应在1-7之间!");
}
switch(day){
case 1:
case 2:
case 3:
case 4:
case 5:
System.out.println("工作日,要好好写代码!");
break;
case 6:
System.out.println("周六啦,看看电影休息下!");
break;
case 7:
System.out.println("周日啦。你可以睡个懒觉了!");
break;
}
}
public static void main(String[] args){
Test t = new Test();
t.doWhat(5);
}
}
//代码1:
package cn.lch.enums;
public enum Week {
MON,TUE,WED,THU,FRT,SAT,SUN
}
*****************************
//代码2:
package cn.lch.enums;
//根据用户给的数字,告知该数字对应的日期该做什么事情
//方法二:利用枚举对应输出
public class Test {
public void doWhat2(Week day){
switch(day){
case MON:
case TUE:
case WED:
case THU:
case FRT:
System.out.println("工作日,要好好写代码!");
break;
case SAT:
System.out.println("周六啦,看看电影休息下!");
break;
case SUN:
System.out.println("周日啦。你可以睡个懒觉了!");
break;
}
}
public static void main(String[] args){
Test t = new Test();
t.doWhat2(Week.MON);
}
}
二、Java API
- 常用Java API
- java.lang
- Enum、包装类、Math、String、StringBuffer、System… - java.util
- java.io
- java.sql
- … …
- java.lang
- 学习方法
- 查看帮助文档
包装类
- 包装类把基本类型数据转换为对象
- 每个基本类型在java.lang包中都有一个相应的包装类 - 包装类有何作用
- 提供了一系列实用的方法
- 集合不允许存放基本数据类型数据,存放数字时,要用包装类型
包装类的构造方法
- 所有包装类都可将与之对应的基本数据类型作为参数,来构造它们的实例
- public Type (type value)
- 如:Integer i = new Integer(1);
- public Type (type value)
- 除Character类外,其他包装类可将一个字符串作为参数构造它们的实例
- public Type(String value)
- 如:Integer i = new Integer(“123”);
具体实现
- 如:Integer i = new Integer(“123”);
- public Type(String value)
package cn.lch.common;
public class Demo {
public static void main(String[] args) {
//构造方法:
//所有包装类都有的构造方法,将与之对应的基本数据做参数
//把基本数据类型变成包装类
int i = 9;
Integer i1 = new Integer(i);
double d1 = 9.8;
Double d = new Double(9.8);
Boolean b = new Boolean(true);
Character c = new Character('a');
System.out.println(i1+"\t"+d+"\t"+b+"\t"+c);
//除Character之外的包装类都有的构造方法,将字符串做参数
//把字符串变成包装类
Integer i2 = new Integer("4");//字符串中内容为数字,即Number类型
Double d2 = new Double("9.8");//同上
Boolean b2 = new Boolean("true");
Boolean b3 = new Boolean("TrUe"); //得到结论true不论字母大小写都为true,但只要不是true,都是false
Boolean b4 = new Boolean("tre");
//Character c2 = new Character('a'); 字符串""做参数编译报错
System.out.println(i2+"\t"+d2+"\t"+b2+"\t"+b3+"\t"+b4+"\t"+c);
}
}
输出结果:
9 9.8 true a
4 9.8 true true false a
包装类的常用方法
1.包装类转换成基本类型
package cn.lch.common;
public class Demo1 {
public static void main(String[] args) {
//包装类---》基本类型
Integer i = new Integer(9);
int j = i.intValue();
System.out.println(j);
Boolean b = new Boolean(true);
boolean b1 = b.booleanValue();
System.out.println(b1);
}
}
2.基本类型—>字符串
-
toString():以字符串形式返回包装对象表示的基本类型数据
(1) String sex = Character.toString('男'); String id = Integer.toString(25); (2) String strNum2 = num+"";
具体实例
//部分代码
int num = 9;
String strNum = Integer.toString(num);
String strNum2 = num+"";
strNum2 += strNum;
System.out.println(strNum2); //输出结果:99
3.字符串—>基本类型
具体代码
//部分代码
//字符串--》基本类型 parse***() Chararcter类除外
String s = "89";
int num2 = Integer.parseInt(s);
boolean bNum = Boolean.parseBoolean("true");
System.out.println(num2+" "+bNum); //输出结果: 89 true
valueOf():
- 所有包装类都有如下方法(基本类型–>包装类)
- public static Type calueOf(type value)
-如:Integer intValue = Integer.valueOf(21);
- public static Type calueOf(type value)
- 除Character类外,其他包装类都有如下方法(字符串–>包装类)
- public static Type valueOf(String s)
-如:Integer intValue = Integer.valueOf(“21”);
具体实现
- public static Type valueOf(String s)
//valueOf(): (1)包装类---》基本类型 (所有包装类都有这个方法) (2)基本类型---》字符串(除Character外)
Integer iNum = Integer.valueOf(89);
Character cNum = Character.valueOf('s');
Boolean jNum = Boolean.valueOf(true);
System.out.println(iNum+" "+cNum+" "+jNum);
//valueOf:(2)基本类型---》字符串(除Character外)
Integer iNum2 = Integer.valueOf("89");
//Character cNum2 = Character.valueOf("sd"); //编译报错
Boolean jNum2 = Boolean.valueOf("true");
System.out.println(iNum2+" "+jNum2);