Java核心技术卷 5.3-5.6
5.3泛型数组列表
package arrayList;
import inheritance.Employee; //此处导入的为同一项目下 inheritance包中的 employee类
import java.util.ArrayList;
public class ArrayListT {
public static void main(String[] args){
//ArrayList 是一个采用类型参数的泛型类 类型参数(Employee)
ArrayList<Employee> staff = new ArrayList<>(3);
staff.add(new Employee("tom" , 50000 , 1999 ,12 ,5));
staff.add(new Employee("jack" , 50000 , 1999 ,12 ,5));
staff.add(new Employee("jerry" , 50000 , 1999 ,12 ,5));
staff.set(1,new Employee("harry",50000 , 1999 ,12 ,5)); //set 方法可以更改原有对象
System.out.println("name:" + staff.get(1).getName() + "salary=" + staff.get(1).getSalary() );
// for(int i = 0 ; i < staff.size() ; i++){
// Employee e = staff.get(i);
// System.out.println("name:" + e.getName() + "salary=" + e.getSalary() + "hireDay=" + e.getHireDay());
// }
for(Employee e : staff){
System.out.println("name:" + e.getName() + "salary=" + e.getSalary() + "hireDay=" + e.getHireDay());
}
// Employee e = staff.get(1);
// System.out.println(e); 在Employee类中重写toString类
}
}
ArrayList数组的大小可以随意改变 通过add()方法 当然还有其他一些方法,在此变不做赘述。
5.4 对象包装器与自动装箱
list.add(3) -> list.add(Integer.valueOf(3)) 自动装箱
int n = list.get(i) -> int n = list.get(i).intValue();
5.5 枚举类
package enums;
import java.util.Scanner;
public class EnumT {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter a size:(SMALL, MEDIUM, LARGE, EXTRA_LARGE)");
String in = input.next().toUpperCase();
Size size = Enum.valueOf(Size.class , in);
System.out.println("size=" + size);
System.out.println("abbreviation=" + size.getAbbreviation());
if(size == Size.EXTRA_LARGE)
System.out.println("Good job--you paid attention to the_.");
Size[] sizes = Size.values();
for(int i =0 ; i < sizes.length ;i++){
}
}
}
enum Size {
SMALL("S"), MEDIUM("M"), LARGE("L"), EXTRA_LARGE("XL");
private final String abbreviation;
private Size(String abbreviation) {
this.abbreviation = abbreviation;
}
public String getAbbreviation() {
return abbreviation;
}
// public static final Size SMALL = new Size("S");
// SMALL("S") , MEDIUM("M") , LARGE("L") , EXTRA_LARGE ("XL");
@Override
public String toString() {
return "";
}
}
待更新
//自定义枚举类
class Size{
private final String abbreviation;
private Size(String abbreviation){
this.abbreviation = abbreviation;
}
public String getAbbreviation(){
return abbreviation;
}
Size SMALL = new Size("S");
Size MEDIUM = new Size("M");
Size LARGE = new Size("L");
public String toString(){
return "";
}
}
对接口的实现
interface Info{
void show();
}
enum Size implements Info{
SMALL("S"){
public void show(){
System.out.println("woshi一个小码");
}
},
LARGE("L"){
public void show(){
System.out.println("woshi一个大码");
}
}
}