Java三章学习内容(枚举,包装类,Math)
一.枚举(enum)
固定常量组成的类型
public void fangfa(Curriculum lv){ //定义相应方法
switch (lv) {
case L1:
System.out.println("大数据开发工程师");
break;
case L2:
System.out.println("大数据发掘工程师");
break;
case L3:
System.out.println("大数据架构师");
break;
}
}
固定常量组成的类型
public enum Curriculum { //创建枚举类型,录入所需要固定的信息,逗号隔开
L1,L2,L3
}
L1,L2,L3
}
public class Kecheng { //在自定义类型中使用枚举类型进行设置属性
private Curriculum lv;
private Curriculum lv;
public void fangfa(Curriculum lv){ //定义相应方法
switch (lv) {
case L1:
System.out.println("大数据开发工程师");
break;
case L2:
System.out.println("大数据发掘工程师");
break;
case L3:
System.out.println("大数据架构师");
break;
}
}
public class MeijuText { //在测试类实例化对象调用方法,只能使用规定枚举类型
public static void main(String[] args) {
Kecheng kc = new Kecheng();
kc.fangfa(Curriculum.L1);
}
}
二.包装类
把基本类型转换为对象,转换为对象可提供相应的方法,集合不能存放基本数据类型,需要先包装类型

1.创建一个Integer类型的包装对象
Integer intValue=new Integer(21); //基本数字类型方法
Integer intValue=new.Integer("21"); //字符串类型方法
2.使用包装类的valueOf()方法创建包装类对象public static void main(String[] args) {
Kecheng kc = new Kecheng();
kc.fangfa(Curriculum.L1);
}
}
二.包装类
把基本类型转换为对象,转换为对象可提供相应的方法,集合不能存放基本数据类型,需要先包装类型
1.创建一个Integer类型的包装对象
Integer intValue=new Integer(21); //基本数字类型方法
Integer intValue=new.Integer("21"); //字符串类型方法
Integer intValue=Integer.valueOf("21");
3.包装类转换成基本数据类型
Integer intValue=new Integer(21);
int i =intValue.intValue();
注:jdk1.5后都支持自动转换,包装类型所需作为引用对象时才使用,滥用会导致系统更大的负担,基本类型则不会!!!
三.Math
随机数: int random = (int)(Math.random()*10); 随机1~10的整数
System.out.println((int)((Math.random()*9+1)*100000)); 生成6位
System.out.println((int)((Math.random()*9+1)*10000)); 生成5位
System.out.println((int)((Math.random()*9+1)*1000)); 生成4位
public class Suiji { //该代码目的是随机两个一样的随机数
public static void main(String[] args) {
Random random = null;
Random random2 = null;
int num =(int) (Math.random()*10);
random=new Random(num);
random2=new Random(num);
int rand1 = random.nextInt(10);
int rand2 = random2.nextInt(10);
System.out.println(rand1+"--"+rand2);
}
}
public static void main(String[] args) {
Random random = null;
Random random2 = null;
int num =(int) (Math.random()*10);
random=new Random(num);
random2=new Random(num);
int rand1 = random.nextInt(10);
int rand2 = random2.nextInt(10);
System.out.println(rand1+"--"+rand2);
}
}