基础回顾 1
11 集合 1
111 集合类型特性 1
112 遍历集合 1
1121 遍历集合种方式 1
1122 遍历数组 2
1123 遍历List 2
1124 遍历Set 3
1125 遍历Map 3
12 泛型(Generic) 4
121 泛型方法 8
122 泛型类 9
123 泛型接口 12
13 IO流 13
131 IO流分类 13
132 读文件代码 13
133 拷贝文件代码 14
14 线程 15
141 启动线程方式 15
142 代码 15
二Junit单元测试 16
21 Junit单元测试框架基 16
22 Assert断言工具类 17
23 准备环境清理环境方法 17
三省(Introspector) 18
四Properties类配置文件 20
41 Properties配置文件说明 20
42 加载配置文件 21
43 写配置文件 21
44 Properties类 22
五文件路径 22
51 绝路径 22
52 相路径 23
53 Java程序中相路径 23
54 classpath路径 23
541 classpath路径说明 23
542 获取classpath中资源(InputStream) 24
六基础增强 24
61课程回顾 24
62 泛型 25
621 概述 25
622 泛型方法泛型类泛型接口 26
623 泛型关键字 28
624 泛型反射 30
63 反射复 32
64 注解 35
641 概述 35
642 定义注解 36
a 注解基写法 36
b带默认值注解 36
b默认名称注解 37
643 元注解 38
645 注解反射 38
65 注解优化BaseDao代码 39
66 Log4J日志组件 39
基础回顾
11 集合
111 集合类型特性
|Collection 单列集合
|List 存储序 重复
|ArrayList 数组实现 查找快 增删慢
数组实现 增删时候会牵扯数组
增容 拷贝元素 慢数组直接索引查找 查找时较快
|LinkedList 链表实现 增删快 查找慢链表实现 增加时前元素记住 删时前元素记住元素 元素记住前元素 样增删效率较高查询时需遍历 效率较低
|Vector ArrayList原理相 线程安全 效率略低
ArrayList实现方式相 考虑线程安全问题 效率略低
|Set 存储序 重复
|HashSet 线程安全存取速度快底层哈希表实现
|TreeSet 红黑树数结构默认元素进行然排
序(String)果较时候两象
返回值0元素重复
| Map 键值 键重复键重复
|HashMap 线程安全存取速度快底层哈希表实现
|TreeMap 红黑树数结构默认元素进行然排
序(String)果较时候两象
返回值0元素重复
|HashTable 底层哈希表 维护存取读取快存储元素
序
112 遍历集合
1121 遍历集合种方式
1 迭代器Iterator方式
2 增强for循环方式
3 果标标方式
1122 遍历数组
public static void main(String[] args) {
遍历数组:
String[] arr new String[] { xx yy zz }
1增强for循环
for (String elt arr) {
Systemoutprintln(elt)
}
2标方式
for (int i 0 i < arrlength i++) {
Systemoutprintln(arr[i])
}
}
1123 遍历List
public static void main(String[] args) {
遍历List:
List
listadd(aa)
listadd(bb)
listadd(cc)
1增强for循环
for (String elt list) {
Systemoutprintln(elt)
}
2标
for (int i 0 i < listsize() i++) {
Systemoutprintln(listget(i))
}
3迭代器
for (Iterator
String elt iternext()
Systemoutprintln(elt)
}
}
1124 遍历Set
public static void main(String[] args) {
遍历Set:
Set
setadd(dd)
setadd(ee)
setadd(ff)
1增强for循环
for (String elt set) {
Systemoutprintln(elt)
}
2迭代器
for(Iterator
String elt iternext()
Systemoutprintln(elt)
}
}
1125 遍历Map
public static void main(String[] args) {
遍历Map:
Map
mapput(aa xx)
mapput(bb yy)
mapput(cc zz)
1增强for循环(Entry集合)
for (Entry
Systemoutprintln(entry)
}
2增强for循环(Key集合)
for(String key mapkeySet()){
Systemoutprintln(key + + mapget(key))
}
3遍历值集合
for(String value mapvalues()){
Systemoutprintln(value)
}
}
2
12 泛型(Generic)
集合中存储象类型时会导致程序运行时候转型异常
import javautilArrayList
import javautilIterator
public class Demo5 {
public static void main(String[] args) {
ArrayList arr new ArrayList()
arradd(new Tiger(华南虎))
arradd(new Tiger(东北虎))
arradd(new Sheep(喜羊羊))
Systemoutprintln(arr)
Iterator it arriterator()
while (ithasNext()) {
Object next itnext()
Tiger t (Tiger) next
teat()
}
}
}
class Tiger {
String name
public Tiger() {
}
public Tiger(String name) {
thisname name
}
@Override
public String toString() {
return Tiger@name + thisname
}
public void eat() {
Systemoutprintln(thisname + 吃羊)
}
}
class Sheep {
String name
public Sheep() {
}
public Sheep(String name) {
thisname name
}
@Override
public String toString() {
return Sheep@name + thisname
}
public void eat() {
Systemoutprintln(thisname + 吃青草)
}
}
原 发现然集合存储意象果需象特方法需类型转换果集合中存入象引发类型转换异常
[Tiger@name华南虎 Tiger@name东北虎 Sheep@name喜羊羊]
华南虎吃羊
东北虎吃羊
Exception in thread main javalangClassCastException cnitcastgzmapSheep cannot be cast to cnitcastgzmapTiger
at cnitcastgzmapDemo5main(Demo5java17)
出现问题
存入特定象取出时候Object象需强制类型转换诱发类型转换异常
法控制存入什类型象取出象时候进行强转时诱发异常编译时期法发现问题
然类型转换时候通if语句进行类型检查(instanceof)效率较低(例吃饭时候需判断米饭里没沙子吃饭效率低)通容器加限定形式规定容器存储种类型象
容器贴标签说明该容器中存储什样类型象
jdk50出现泛型
泛型应
格式
1 集合类<类类型> 变量名 new 集合类<类类型>()
public class Demo5 {
public static void main(String[] args) {
泛型规定该集合放羊老虎进
ArrayList
arradd(new Sheep(美羊羊))
arradd(new Sheep(懒洋洋))
arradd(new Sheep(喜羊羊))
编译失败
arradd(new Tiger(东北虎))
Systemoutprintln(arr)
Iterator
while (ithasNext()) {
泛型需强制类型转换
Sheep next itnext()
nexteat()
}
}
}
1 运行时异常提前编译时发生
2 获取元素时候需强转类型避免类型转换异常问题
格式 通<> 指定容器中元素类型
什时候泛型类中操作引数类型确定时候泛型类
JDK50前Comparable
package javalang
public interface Comparable {
public int compareTo(Object o)
}
JDK50Comparable
package javalang
public interface Comparable
public int compareTo(T o)
}
里
细节
声明泛型类型集合中存放特定类型元素
public class Demo6 {
public static void main(String[] args) {
创建存储字符串list
ArrayList
arradd(gz)
arradd(itcast)
存储非字符串编译报错
arradd(1)
}
}
细节二
泛型类型必须引类型
public class Demo6 {
public static void main(String[] args) {
泛型类型必须引类型说集合存储基数类型
ArrayList
基数类型包装类
ArrayList
}
}
细节三 泛型取出元素需类型转换
public class Demo6 {
public static void main(String[] args) {
ArrayList
arradd(gzitcast)
arradd(cditcast)
arradd(bjitcast)
泛型取出元素需类型转换
String strarrget(0)
Systemoutprintln()
}
}
121 泛型方法
需求:写函数调者传递什类型变量该函数返回什类型变量?
实现
法确定具体传递什类型数方法形参定义Object类型返回值Object类型该函数时需强制类型转换
private Object getDate(Object obj) {
return obj
}
进行强制类型转换否写出该功
目前学知识法解决该问题
需泛型类解决
泛型定义解决问题
泛型: 类型作变量处理规范泛型定义般写意字母
1 函数泛型定义
函数中明确数类型函数进行泛型定义
public <泛型声明> 返回值类型 函数名( 泛型 变量名 ){
}
public static void main(String[] args) {
int[] arr { 1 2 3 4 5 }
new Demo6()getData(5)
}
public
return data
}
细节:
泛型方法前需进行泛型声明尖括号 <泛型>声明位置static返回值类型前
类中函数声明泛型该泛型声明声明类
122 泛型类
格式
2 类泛型声明
修饰符 class 类名<泛型>{
}
import javautilArrays
public class Demo6
public static void main(String[] args) {
泛型类创建象时候需指定具体类型
new Demo6
}
public T getData(T data) {
return data
}
反序意类型数组
public void reverse(T[] arr) {
int start 0
int end arrlength 1
for (int i 0 i < arrlength i++) {
if (start < end) {
T temp arr[start]
arr[start] arr[end]
arr[end] temp
}
}
}
泛型类中定义静态方法
public class Demo6
public static void main(String[] args) {
Systemoutprintln(getData2(100))
}
public T getData(T data) {
return data
}
静态方法
public static T getData2(T data) {
return data
}
}
注意:静态方法类中定义泛型
类中泛型需象初始化时指定具体类型静态优先象存类中静态方法需单独进行泛型声明声明泛型定写static返回值类型前
泛型类细节:
1创建象时候指定泛型具体类型
2创建象时指定泛型具体类型(创建集合象眼)默认Object例集合存储元素时候没泛型参数类型Object
3类面声明泛型应非静态成员函数果静态函数需泛型
需函数独立声明
4果建立象指定泛型具体类型该象操作方法时方法操作种数类型
5类泛型声明时该类方法中声明泛型
泛型练:
定义泛型成员
public class Demo7 {
public static void main(String[] args) {
Father
Systemoutprintln(fgetT())
Father
Systemoutprintln(f2getT())
}
}
class Father
private T t
public Father() {
}
public Father(T t) {
super()
thist t
}
public T getT() {
return t
}
public void setT(T t) {
thist t
}
}
果Father类子类子类该实现
public class Demo7 {
public static void main(String[] args) {
Father
Systemoutprintln(fgetT())
Father
Systemoutprintln(f2getT())
}
}
class Father
private T t
public Father() {
}
public Father(T t) {
super()
thist t
}
public T getT() {
return t
}
public void setT(T t) {
thist t
}
}
子类指定具体类型
class Son extends Father
}
子类需泛型
class Son3
}
错误写法父类定义泛型需进行处理
class Son2 extends Father
}
123 泛型接口
public class Demo8 {
public static void main(String[] args) {
MyInter
myprint(泛型)
MyInter2 my2 new MyInter2()
myprint(传字符串)
}
}
interface Inter
void print(T t)
}
实现知类型时样定义
class MyInter
public void print(T t) {
Systemoutprintln(myprint + t)
}
}
接口时明确具体类型
class MyInter2 implements Inter
@Override
public void print(String t) {
Systemoutprintln(myprint + t)
}
}
13 IO流
131 IO流分类
输入流
输出流
说明
字节流
InputStream
OutputStream
字节流处理字节(二进制)
字符流
Reader
Writer
字符流处理字符
注:类抽象类
132 读文件代码
public static void main(String[] args) {
String path catxt
FileInputStream in null
try {
开流
in new FileInputStream(path)
流读文件容
int b inread()
while (b 1) {
Systemoutprint((char) b)
b inread()
}
} catch (Exception e) {
throw new RuntimeException(e)
} finally {
释放资源
if (in null) {
try {
inclose()
} catch (IOException e) {
throw new RuntimeException(e)
}
}
}
}
133 拷贝文件代码
public static void main(String[] args) {
String srcPath catxt
String destPath cbtxt
定字节流
InputStream in null
OutputStream out null
try {
开流
in new FileInputStream(srcPath)
out new FileOutputStream(destPath)
流
byte[] buf new byte[1024 * 8]
for (int len 1 (len inread(buf)) 1) {
outwrite(buf 0 len)
}
} catch (Exception e) {
eprintStackTrace()
} finally {
释放资源
try {
if (in null) {
inclose()
}
} catch (IOException e) {
throw new RuntimeException(e)
} finally {
if (out null) {
try {
outclose()
} catch (IOException e) {
throw new RuntimeException(e)
}
}
}
}
}
14 线程
141 启动线程方式
1 定义类继承Thread类
代码new MyThread()start()
2定义类实现Runnable接口
代码new Thread(new MyRunnable())start
142 代码
代码分两种方式启动线程(匿名部类)
private static int count 100
public static void main(String[] args) {
继承Thread类方式启动线程
new Thread() {
public void run() {
synchronized (StartThreadTestclass) {
while (count > 0) {
count
Systemoutprintln(ThreadcurrentThread() + 卖张票剩 + count)
}
}
}
}start()
实现Runnable接口方式启动线程
new Thread(new Runnable() {
public void run() {
synchronized (StartThreadTestclass) {
while (count > 0) {
count
Systemoutprintln(ThreadcurrentThread() + 卖张票剩 + count)
}
}
}
})start()
}
二 Junit单元测试
21 Junit单元测试框架基
搭建环境:
导入junitjar包(junit4)
二写测试类:
0般类应测试类
1测试类测试类放包中(源文件夹)
2测试类名字测试类名字加Test缀
三:写测试方法:
0般方法应单元测试方法
1测试方法名字test前缀加测试方法名字testAddPerson()
2单元测试方法面加@Test注解(orgjunitTest)
3单元测试方法参数返回值(返回void)测试方法静态方法
四测试方法基:
1单独执行测试方法次执行包类中测试方法
2执行完显示绿色表示测试成功显示红色表示测试失败(抛异常会测试失败)
22 Assert断言工具类
中静态工具方法(符合期抛异常):
assertTrue() 参数值应true
assertFalse() 参数值应false
assertNull() 应null值
assertNotNull() 应非null值
assertSame() 较结果true(表示象)
AssertNotSame() 较结果false
assertEquals() 两象equals()方法较结果true
23 准备环境清理环境方法
@Test
表示单元测试方法
@Before
修饰方法应非static(没参数返回值void)
表示方法会类中单元测试方法前执行次
@After
修饰方法应非static(没参数返回值void)
表示方法会类中单元测试方法执行次
@BeforeClass
修饰方法应static(没参数返回值void)
表示方法会类中单元测试方法前执行执行次
@AfterClass
修饰方法应static(没参数返回值void)
表示方法会类中单元测试方法执行执行次
三 省(Introspector)
什学省?
开发框架时常需java象属性封装程序数次反射技术完成类操作麻烦sun公司开发套API专门操作java象属性
省操作java象属性问题必须清楚
问题: 什Java象属性属性读写方法?
问题二 通省访问javaBean属性
1 通PropertyDescriptor类操作Bean属性
public static void testPropertyDescriptor() throws Exception{
Person p new Person()
PropertyDescriptor propertyDescriptor new PropertyDescriptor(idPersonclass)
获取属性写方法
Method writeMethod propertyDescriptorgetWriteMethod()
Method readMethod propertyDescriptorgetReadMethod()
propertyDescriptorgetReadMethod()
writeMethodinvoke(p 12)
Systemoutprintln(readMethodinvoke(p null))
}
2 通Introspector类获Bean象 BeanInfo然通 BeanInfo 获取属性描述
器( PropertyDescriptor )通属性描述器获取某属性应 gettersetter 方法然通反射机制调方法
public static void testIntrospector() throws Exception{
BeanInfo beanInfo IntrospectorgetBeanInfo(Personclass)
PropertyDescriptor[] descriptor beanInfogetPropertyDescriptors()
for(PropertyDescriptor itemProperty descriptor){
Systemoutprintln(itemPropertygetReadMethod()getName())
}
}
存问题: sun公司省API繁琐Apache组织结合实际开发中应场景开发套简单易API操作Bean属性——BeanUtils
public static void main(String[] args) throws Exception {
Person p new Person()
ConvertUtilsregister(new Converter() {
@Override
public Object convert(Class type Object value) {
try {
if(valuenull){
SimpleDateFormat dateFormat new SimpleDateFormat(yyyy MM dd)
Date d dateFormatparse((String) value)
return d
}
} catch (ParseException e) {
TODO Autogenerated catch block
eprintStackTrace()
}
return null
}
} Dateclass)
BeanUtilssetProperty(pid110)
BeanUtilssetProperty(pname狗娃)
BeanUtilssetProperty(p birthDay1992 12 12)
Systemoutprintln(pgetId() ++ pgetName()++pgetBirthDay())
}
四 Properties类配置文件
41 Properties配置文件说明
Properties类应properties文件文件容键值键值间空格隔开开头#表示注释
Properties类加载properties文件时iso88591编码文件中中文特殊处理:果配置文件中中文必须进行转义native2asciiexe命令操作
native2ascii dmyproperties dmy2properties
Properties类中load(InputStream) 方法加载配置文件中store(OutputStream) 方法保存配置指定文件
更信息Properties类API文档
42 加载配置文件
public static void testLoadProperties() throws Exception {
Properties properties new Properties()
InputStream in new FileInputStream(Eitcastconfigproperties)
propertiesload(in) 加载
inclose()
Systemoutprintln(properties)
}
43 写配置文件
public static void testStoreProperties() throws Exception {
准备配置信息
Properties properties new Properties()
propertiessetProperty(name 李四)
propertiessetProperty(age 20)
准备
OutputStream out new FileOutputStream(dmyproperties)
String comments 配置文件
写出
propertiesstore(out comments)
outclose()
}
作业:properties读取配置文件读取数库户名密码包成jar包
44 Properties类
public class DBUtil {
static Properties properties new Properties()
static{
try {
Class clazz DBUtilclass
InputStreamReader fileReader
new InputStreamReader(clazzgetResourceAsStream(dbproperties))
propertiesload(fileReader)
} catch (IOException e) {
eprintStackTrace()
}
}
public static String getUserName(){
String userName propertiesgetProperty(userName)
return userName
}
public static String getPassword(){
return propertiesgetProperty(password)
}
public static void main(String[] args) {
Systemoutprintln(户名:+ getUserName())
Systemoutprintln(密码 + getPassword())
}
}
五 文件路径
51 绝路径
根目录某盘符开头路径(者说完整路径)
例:
l catxt (Windows操作系统中)
l cxxxatxt (Windows操作系统中)
l varxxaatxt (Linux操作系统中)
绝路径问题 C\abc\aproperties文件路径该路径windows执行没 问题果该项目移动linux面执行 该路径会出现问题linux面没c盘根目录\
52 相路径
相前路径路径例前文件夹cabc时:相路径atxt表示cabcatxt相路径xxatxt cabcxxatxt
l 表示前文件夹
l 表示级文件夹
相路径存问题相路径相目前执行class文件时候控制台路径样子会导致出现问题
53 Java程序中相路径
Java程序中File时写相路径指相执行java命令时前文件夹
测试代码:
public class PathTest {
public static void main(String[] args) throws Exception {
Systemoutprintln(new File(atxt)getAbsolutePath())
}
}
命令行中cd命令切换路径试试说效果
Eclipse中前路径工程根目录
54 classpath路径
541 classpath路径说明
Java程序中般情况绝路径相路径太合适Java程序jar包放位置确定执行java程序时前路径确定合适般Java程序中会资源放classpath中然classpath路径查找资源
Classpath路径:classpath目前路径
542 获取classpath中资源(InputStream)
public static void main(String[] args) throws Exception {
Class clazz new ClassPathTest()getClass()
开头''表示classpath根目录表示classpath根目录中开始查找资源
InputStream in clazzgetResourceAsStream(cnitcastmyproperties)
果开头没''表示前class包中开始查找
InputStream in2 clazzgetResourceAsStream(myproperties)
}
六基础增强
61课程回顾
XML技术
ServletJsp
数库
Jdbc技术
JavaWeb增强
滤器监听器文件传载国际化
学组件:
Dom4JXpathDBUtilsC3p0BeanUtilslog4J…
Struts Spring Hibernate
目标:
1 泛型
2 注解
3 log4J 日志 (项目中)
4 枚举
62 泛型
掌握知识:
基法泛型擦泛型类泛型方法泛型接口泛型关键字反射泛型(案例)
621 概述
泛型JDK15 编译时期进行类型检查避免频繁类型转化
运行时期异常
@Test
public void testGeneric() throws Exception {
集合声明
List list new ArrayList()
listadd(China)
listadd(1)
集合
String str (String) listget(1)
}
泛型
@Test
public void testGeneric2() throws Exception {
声明泛型集合时候指定元素类型
List
listadd(China)
listadd(1) 编译时期报错
String str listget(1)
}
泛型擦
泛型编译时期效编译字节码文件中存泛型信息
*
* 泛型擦实例
public void save(List
}
public void save(List
}
*
泛型写法
泛型写法
@Test
public void testGeneric3() throws Exception {
声明泛型集合集合两端类型必须致
List