一.枚举
1.枚举产生的原因
场景:定义属性的同时如果出现不合逻辑的类型,则需要用枚举类型替代
2.创建枚举
枚举的关键字:enum
枚举实战:这里的sex的类型是枚举类型的名称
public enum Xinbie{
MALE,FEMAL
}
private Xinbie sex;调用Xinbie里的属性
把sex进行封装;main方法==》实例化对象Demo demo =new Demo()==》demo.setSex(Xinbie.MALE)
输出sout(demo.getSex())
3.使用枚举
需要映射中文类型时
public enum Xinbie{
MALE(“男”),FEMAL(“女”)
private String name;
}跟上面一样,但是要把枚举里的类型进行封装,在调用枚举中封装的属性
sout(demo.getSex().getName())
4.实战
public enum chenji{
A(“优秀”),B(“良好”),C(“及格”),D(“不及格”);
private String score;
Chengji(){}(无参构造)
Chengji(String score){
this.score=score
}(有参构造)
public String getScore(){return score }(get方法)
public void setScore(String score){this score=score}(set方法)
}
(枚举中的代码)
public class Demo{
private Chenji sco;
封装Chenji(枚举)中的属性
public Demo(){}
public Demo(Chneji sco){
this .sco=sco
}
public void setSco(Chenji sco){this.sco=sco}
public chenji getSco(){return sco;}
}
psvm{
Demo demo=new Demo();
demo.setSco(Chenji.A)
输出sout(demo.getSco().getScore())
}
(类中的代码)
第二种方法
private String sco;
封装sco;
psvm{
Demo demo=new Demo;
demo.setSco(Chenji.A.getScore())
sout( demo. getSco)
}
二.包装
1.定义:每个基本数据类型在java.lang包中都有各自的包装类型,例如int类型的包装类型是Interger;char类型的包装类型是Character;
2.作用:(1)提供一系列使用的方法,例如可以实现基本数据类型和包装类型之间的转换
(2)集合不允许存放基本数据类型,存放数值时,要用包装类型
3.包装类型的构造方法实现基本数据类型转换成包装类型
int==》Integer;byte==》Byte;short==》Short;
long==》Long;float==》Float;double==》Double;
boolean==》Boolean;char==>Character
4.字符串类型转换成包装类型
(1)除了character类型以外都可以使用构造器将字符串类型转换成包装类型
String a=“123”;
Integer integer=new Integer(a);
Sout(intrger)(转换成int的包装类型)
Double adouble=new Double(a)
sout(adouble)(转换成double的包装类型)
(2)特例Boolean
Boolean中除了“true”字符串结果否是false
Boolean boolean=new Boolean(a)
sout(boolean)(显示false;因为a是123)
5.利用包装类型的xxValue()方法实现 包装类型转换成基本数据类型
要有一个包装类型:Integer integer=new Integer(24)
int i=integer.value(i就是基本数据类型)
6.将基本数据类型转换成字符串类型(调用包装类型的toString()方法)
int a=3;(基本数据类型)
String b=Integer.toString(a)
b就是字符串类型的a
7.将字符串类型转换为基本数据类型(利用包装类型parsexx()(xx是基本数据类型))
String b=“123”;
int a=Integer.parseInt(b)
8.基本数据类型转换成包装类型(利用包装类型中的valueOf())
int a =3;
Integer integer=new Integer.valueOf(a)
(valueOf()也可以不写;效果是一样的)
9.字符串类型转换成包装类型(利用包装类型中的valueOf()(Character除外))(valueOf()也可以不写;效果是一样的)
String a="123"
Interger interger= Interge.valueOf(a);或者Interger interger= new Interge(a)
sout(interger)
10.拆箱和装箱
装箱:基本类型转换为包装类型的对象(自动转换)
拆箱:包装类型转换为基本类型的值(自动转换)
int a=3;
Integer b=a;(装箱)
Integer integer=new Integer("233”)(自动转换:string类型也可以转换为int类型)
int cc=integer(拆箱)
int cc=integer;
备注:int 和integer在属性初始化的时候系统默认赋的值不一样;
int类型的默认值是0
Integer类型的属性默认为null;
二.String
1.String的基本语法
(1)存储字符串
(2)String a=“”;String string=new String();String string =new String(“xx”);
(3)包路径:java.lang包
(4)备注String a=“”;字符串存放在静态池或者叫常量值中)
String a=new String“xx”;字符串存放在堆中(只要有new就是开辟空间)
2.常用方法length()
length()返回该字符串的长度
备注:数组长度 array.length属性
集合长度 list.size()方法
3.常用方法equals()以及它和==的区别
==比较的是内存地址是否相同
equals比较的是字符串的内容是否相同
String a=“xx”;(xx是存放在方法区中)
String string=new String(“xx”)(xx是存放在堆中)
4.equaIsgnoreCase
定义:将此String和另一个String作比较,不考虑大小写
a.equaIsgnoreCase(b);
5.toLowerCase
定义:String中的所有字符都转换成小写(大写转小写,小写不变)
String b=“xxx”.toLowerCase
6.toUppercase
定义:String中的所有字符都转换成大写(小写转大写,大写不变)
String a=“xxxx”.toUpperCase
7.concat
定义:将指定字符串连接到此字符串的结尾
String a=“xxx”
Sout(a.concet("xx"))
等同于a+b
8.trim
定义:返回字符串的文本,忽略头部空白和尾部空白
String a=“ a b ”
sout(a.trim)
9.indexOf("xx")
定义:返回指定字符串在此字符中第一次出现处的索引,如果未出现则返回-1
String a=“hello”
int o=a.indexOf(“o”)
sout(o)(输出o在字符串a中的位置)
int hehe=a.indexOf(“hehe”)
sout(hehe)(字符串中没有hehe,所以这里返回-1)
int ell=a.indexOf(“ell”)
sout(ell)(这里因为字符串中有ell所以返回值子字符串中第一个字符的索引位置)
10.substring(int befinIndex0)
返回一个子字符串,该子字符串从指定索引处的字符开始,直到字符串末尾
suot(a.substring(4))(4代表的是角标)
11.substring (int befinIndex,int endIndex)
返回一个子字符串。该子字符串从指定的beginIndex处开始,直到索引endIndex-1处结束。因此,该子字符串的长度为endIndex-beginIndex(能取到的值是左闭右开区间)
suot(a.substring(4,6))
12.spilt
定义:分隔字符串
String a=“我 是 中 国 人”
String[]s=a.split("");
for(String ss:s){sout(ss)}增强for循环输出
三.StringBuffer
1.StringBuffer产生的原因
场景:String是不可修改的字符串,拼接效率低,所以需要使用StringBuffer替代
2.StringBuffer声明
StringBuffer buffer=new StringBuffer(“hello”)
3.常用方法:
append()将新的字符串拼接到原有的字符串的末尾
buffer.append("world")
4.常用方法
insert()
在原有的字符串的某个索引的位置上插入某个字符串,该索引之后的字符依次往后移一位
(意思就是我这里填入的角标就是我子字符在字符串中的角标)
buffer.insert(3,"x")
四.日期
1.获取当前日期
Date date=new Date();
输出date(Date是默认的方法,这个方法可以获取西文的日期)
2.字符串类型转换成Date类型
String a=“2020-02-02 02:02:02”
SimpleDateFormat sdf=new SimpleDateFormat(“yyyy-MM-dd HH :mm:ss”)
Date datenow=sdf parse(a);
sout(datenow);
3.Date类型转换成字符串类型
Date date=new Date();
SimpleDateFormat sdf=new SimpleDateFormat(“yyyy-MM-dd HH :mm:ss”)
String time=sdf.format(date)
sout(time)
浙公网安备 33010602011771号