Java学习记 泛型数组 枚举类

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一个大码");
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值