public void siyang(Animal c){ public void use(USB u){
System.out.println("饲养动物"); u.openUSB(); notebook n = new notebook();
c.eat(); u.closeUSB(); mouse m = new mouse(); n.use(m);
c.drink();
匿名内部类 两种 这种是要下面有方面调用的
↓ ↓ ↓
接口 i = new 接口(): new 接口(){ 方法(new 接口(){
重写方法 重写方法}.调用方法 重写方法};
调用方法
Integer 装箱 拆箱
String s =String.valueof(i) int y =Integer.parseInt(s)
Int转String String转Int
加数字=从1970年加毫秒
↓
Date d = new Date();
String s =sdf.format(d);
System.out.println(s); 输出当天年月
定义格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd") 这是一个类,才能用方法
Date转String String转Date 解析 年 - 月 - 日 - E(周几)
String s =sdf.format(“d") Date d = sdf.parse(“s”)
sout(s)
日历
Calendar c = Calendar.getInstance(); c.add(Calendar.YEAR,10); 增加10年
int year = c.get(Calendar.YEAR); c.set(year: mouth: date: ) 修改时间
int month = c.get(Calendar.MONTH)+1;
迭代器 ←遍历→ list迭代器
Collection c =new ArrayList(); List list = new ArrayList();
Iterator it = c.iterator(); ListIterator sli = list.listIterator();
while (it.hasNext()) { while (sli.hasNext()){ 正向输出 sli.hasPrevious() 反向输出
String s = (String) it.next(); String s = (String) sli.next();
System.out.println(s); System.out.println(s);}
public class Student implements Comparable<Student> 引用Comparable接口
1.在定义类 compareble
@Override
public int compareTo (Student s) {
int num =this.age -s.age;
int num2 = num ==0?this.name.compareTo(s.name):num;
return num2;
2.在测试类 Comparator比较器
TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
@Override
public int compare (Student s1, Student s2) {
int num = s1.getAge() - s2.getAge();
int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num;
return num2;
Generic 泛型 Generic可以随意写字母 通配符 Collection<?>
public class Generic<T> { Generic s = new Generic("aa",55);
private T name; System.out.println(s.getAge()+","+s.getName());
private T age;
public<T> void show(T z){
方法也可以泛型
HashMap<String,String> map = new HashMap<String, String>();
增强for遍历 Student类型
Set<String> set = hm.keySet();
for ( String s : set ) {
Student value =hm.get(s);
System.out.println(s+","+value.getName()+","+value.getAge());
迭代器遍历
Iterator<String> it = set.iterator();
while (it.hasNext()){
String key = (String) it.next();
String value = map.get(key);
entrySet方法
Set<Map.Entry<String,String>> entr =map.entrySet();
Iterator<Map.Entry<String,String>> ite = entr.iterator();
while (ite.hasNext()){
Map.Entry<String,String> next =ite.next();
String key =next.getKey();
String value =next.getValue();
System.out.println(key+""+value);
}
for ( Map.Entry<String, String> entry : entr ) {
String key =entry.getKey();
String value = entry.getValue();
System.out.println(key+""+value);
}