构造方法
public class ClassName {
( public ) ClassName ( 参数列表) {
}
}
继承
public class Child extends Father {
}
接口
1. 实现
public class ClassName implements Interface {
}
2. 自定义接口
public interface MyInterface {
}
比较器
1. 内部比较器
public class ClassName implements Comparable < Object > {
public ClassName {
}
public int compareTo ( Object o) {
return this . -o. ;
}
}
Collections . sort ( array) ;
2. 外部比较器
public class MyComparator implements Comparator < Object > {
public int compare ( Object first, Object second) {
return first. -second. ;
}
}
Collections . sort ( array, new MyComparator ( ) ) ;
3. 匿名内部类
Collections . sort ( array, new Comparator < Object > {
public int compare ( Object first, Object second) {
return first. -second. ;
}
} ) ;
4.L ambda
Collections . sort ( array, ( first, second) -> {
return frist. -second.
} ) ;
异常
1. try . . . catch . . finally
try {
} catch ( Exception e) {
e. printStackTrace ( ) ;
} finally {
}
2. throws
public static void method ( ) throws Exception {
}
3. thow自定义异常, 再由方法throws 抛出, 再由 try . . catch 处理
public class MyException extends Exception {
public MyException ( String message) {
super ( message) ;
}
}
public static void method ( ) throws MyException {
throw new MyException ( "这是一个异常" ) ;
}
集合
1. ArrayList
List < Object > arr = new ArrayList < Object > ( ) ;
2. HashMap
Map < Type , Type > hm = new HashMap < Type , Type > ( )
3. Iterator
Iterator < Object > iterator = list. iterator ( ) ;
4. 迭代器遍历
while ( iterator. hasNext ( ) ) {
. . .
String point= iterator. next ( ) ;
}
5.L istIterator
ListIterator < String > listIterator= list. listIterator ( ) ;
6. 遍历
while ( listIterator. hasNext ( ) ) {
. . .
String point= listIterator. next ( ) ;
}
7. 反向遍历
while ( listIterator. hasPrevious ( ) ) {
. . .
String point= listIterator. previous ( ) ;
}
流与文件
1.F ileInputStream
FileInputStream fis= new FileInputStream ( "filename" ) ;
2.F ileOutputStream, 注意异常处理throws IOEXception ( 流这边都能用)
public static void main ( String [ ] args) throws FileNotFoundException {
FileOutputStream fos= new FileOutputStream ( new File ( "filename" ) ) ;
=
FileOutputStream fos= new FileOutputStream ( "filename" ) ;
}
3. Reader
Reader reader= new FileReader ( "filename" ) ;
4. Writer
Writer writer= new FileWriter ( "filename" ) ;
线程
1. 实现Runnable 接口
public class MyRunnable implements Runnable {
public void run ( ) {
}
}
Thread thread = new Thread ( new MyRunnable ( ) ) ;
thread. start ( ) ;
2. 同步代码块+ wait+ notify
private static Object object = new Object ( ) ;
public void run ( ) {
. . .
synchronized ( object) {
try {
object. wait ( ) ;
或者
object. notify ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
. . .
}
也可以
public synchronized void run{
后面就不用再写syn
}