1、枚举类实现接口的用法
以前在学校接触过枚举类,只会调用它,也不太了解它的实际用处。
//接口声明
public interface PeopleInterface {
String getJob();//获取职业
}
//程序员枚举类
public enum Programmer implements PeopleInterface{
Java_engineer(1,"Java开发工程师"),
PHP_engineer(2,"PHP开发工程师"),
C_engineer(3,"C语言开发工程师")
;
private Integer id;
private String job;
Programmer(Integer id, String job) {
this.id = id;
this.job = job;
}
@Override
public String getJob() {
return job;
}
}
//学生枚举类
public enum Student implements PeopleInterface{
primary_school_student(1,"小学生"),
middle_school_student(2,"中学生"),
college_student(3,"大学生")
;
private Integer id;
private String job;
Student(Integer id, String job) {
this.id = id;
this.job = job;
}
@Override
public String getJob() {
return job;
}
}
//启动类,入口
public class App {
public static void main(String[] args) {
PeopleInterface people = Programmer.Java_engineer;
if(people instanceof Programmer) {
System.out.println("我是个程序员,我的职业是:"+ people.getJob());
}
}
}
通过这个例子显然易见它的用法本质上和普通类继承接口区别不大,但是他有了默认的类似于初始化对象的枚举对象,可供选择。这样就可以当作常量来使用。使代码更加规范了。
2、枚举类的遍历、结合switch语句使用
public class Course {
private Integer courseId;
private String courseName;
public Course(Integer courseId, String courseName) {
this.courseId = courseId;
this.courseName = courseName;
}
public Integer getCourseId() {
return courseId;
}
public void setCourseId(Integer courseId) {
this.courseId = courseId;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
@Override
public String toString() {
return "Course{" +
"courseId=" + courseId +
", courseName='" + courseName + '\'' +
'}';
}
}
public enum CourseEnum {
Chinese(1, "语文"),
Math(2, "数学"),
English(3, "英语")
;
private Integer id;
private String name;
CourseEnum(Integer id, String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static Course find(String name) {
for (CourseEnum courseEnum: CourseEnum.values()) {
if(courseEnum.getName().equals(name)) {
return new Course(courseEnum.getId(),courseEnum.getName());
}
}
return null;
}
}
public class App {
public static void main(String[] args) {
Course course = CourseEnum.find("语文");
if (course != null) {
System.out.println(course.toString());
}
}
}
2、多线程(Thread,Runable)
首先,很长时间不用线程了,基本不会用了。公司里最近一个项目用到了异步(主要是涉及到CompletableFuture)的操作,基本上看不懂。那么就先从多线程开始吧。
话不多说,直接show code。
public class ThreadDemo {
public static Integer value = 20;
//在这里我打算采用内部类的方式写不同的thread类了。
static class MyThread_1 extends Thread{
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
}
}
static class MyThread_2 extends Thread {
private String name;
public MyThread_2(String name) {
this.name = name;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(name + " : " + i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
static class MyThread_3 extends Thread {
@Override
public void run() {
while(value > 0 && value < 50) {
if (value % 2 == 1) {
System.out.println("a:" + value);
value *= 2;
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
value--;
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println(value);
}
}
static class MyThread_4 extends Thread {
@Override
public void run() {
while(value > 0 && value < 100) {
if (value % 2 == 0) {
System.out.println("b:" + value);
value /= 2;
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
value++;
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println(value);
}
}
}
//启动类
public class App {
public static void main(String[] args) {
ThreadDemo.MyThread_1 thread1 = new ThreadDemo.MyThread_1();
thread1.run();
ThreadDemo.MyThread_2 thread2 = new ThreadDemo.MyThread_2("第一个线程");
ThreadDemo.MyThread_2 thread3 = new ThreadDemo.MyThread_2("第二个线程");
thread2.start();
thread3.start();
ThreadDemo.MyThread_3 thread4 = new ThreadDemo.MyThread_3();
ThreadDemo.MyThread_4 thread5 = new ThreadDemo.MyThread_4();
thread4.start();
thread5.start();
}
}
thread1的运行结果,其他线程注释掉:
分析:只有一个线程在运行,不会出现什么线程运行的优先级问题。
thread2,thread3的运行结果,其他线程注释掉:
分析:当两个线程同时运行的时候就会出现运行的优先级问题,但是却不会干扰其他线程的运行结果。只会出现谁先运行谁后运行的影响。
thread4,thread5的运行结果,其他线程注释掉:
………
分析:当两个进程抢占同一个资源的时候,那肯定是相互影响的,甚至还会导致死锁。