1、java枚举基础入门1 枚举类Direction.javapackage cn.itcast_02;/* * 通过JDK5提供的枚举来做枚举类 */public enum Direction FRONT, BEHIND, LEFT, RIGHT;Direction2.javapackage cn.itcast_02;/* * 通过JDK5提供的枚举来做枚举类 */public enum Direction2 FRONT(前), BEHIND(后), LEFT(左), RIGHT(右); private String name; private Direction2(String name) t
2、his.name = name; public String getName() return name; / Override / public String toString() / return 我爱林青霞; / Direction3.javapackage cn.itcast_02;/* * 通过JDK5提供的枚举来做枚举类 */public enum Direction3 FRONT(前) Override public void show() System.out.println(前); , BEHIND(后) Override public void show() System.
3、out.println(后); , LEFT(左) Override public void show() System.out.println(左); , RIGHT(右) Override public void show() System.out.println(右); ; private String name; private Direction3(String name) this.name = name; public String getName() return name; public abstract void show();DirectionDemo.javapacka
4、ge cn.itcast_02;public class DirectionDemo public static void main(String args) Direction d = Direction.FRONT; System.out.println(d); / FRONT / public String toString()返回枚举常量的名称,它包含在声明中。 System.out.println(-); Direction2 d2 = Direction2.FRONT; System.out.println(d2); System.out.println(d2.getName();
5、 System.out.println(-); Direction3 d3 = Direction3.FRONT; System.out.println(d3); System.out.println(d3.getName(); d3.show(); System.out.println(-); Direction3 dd = Direction3.FRONT; dd = Direction3.LEFT; switch (dd) case FRONT: System.out.println(你选择了前); break; case BEHIND: System.out.println(你选择了后
6、); break; case LEFT: System.out.println(你选择了左); break; case RIGHT: System.out.println(你选择了右); break; EnumMethodDemo.javapackage cn.itcast_02;public class EnumMethodDemo public static void main(String args) / int compareTo(E o) Direction2 d21 = Direction2.FRONT; Direction2 d22 = Direction2.BEHIND; Di
7、rection2 d23 = Direction2.LEFT; Direction2 d24 = Direction2.RIGHT; System.out.println(pareTo(d21); System.out.println(pareTo(d24); System.out.println(pareTo(d21); System.out.println(-); / String name() System.out.println(d21.name(); System.out.println(d22.name(); System.out.println(d23.name(); Syste
8、m.out.println(d24.name(); System.out.println(-); / int ordinal() System.out.println(d21.ordinal(); System.out.println(d22.ordinal(); System.out.println(d23.ordinal(); System.out.println(d24.ordinal(); System.out.println(-); / String toString() System.out.println(d21.toString(); System.out.println(d2
9、2.toString(); System.out.println(d23.toString(); System.out.println(d24.toString(); System.out.println(-); / T valueOf(Class type,String name) Direction2 d = Enum.valueOf(Direction2.class, FRONT); System.out.println(d.getName(); System.out.println(-); / values() / 此方法虽然在JDK文档中查找不到,但每个枚举类都具有该方法,它遍历枚举
10、类的所有枚举值非常方便 Direction2 dirs = Direction2.values(); for (Direction2 d2 : dirs) System.out.println(d2); System.out.println(d2.getName(); 3 二进制字面量package cn.itcast_03;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.ArrayList;public class Demo public stati
11、c void main(String args) / 二进制字面量 int x = 0b100101; System.out.println(x); / 数字字面量可以出现下划线 int y = 1_1123_1000; / 不能出现在进制标识和数值之间 int z = 0x111_222; / 不能出现在数值开头和结尾 int a = 0x11_22; / 不能出现在小数点旁边 double d = 12.3_4; / switch 语句可以用字符串?自己回顾 / 泛型简化 ArrayList array = new ArrayList(); / 异常的多个catch合并 method();
12、 private static void method() / try-with-resources 语句 / try(必须是java.lang.AutoCloseable的子类对象) try FileReader fr = new FileReader(a.txt); FileWriter fw = new FileWriter(b.txt); int ch = 0; while (ch = fr.read() != -1) fw.write(ch); fw.close(); fr.close(); catch (IOException e) e.printStackTrace(); / 改
13、进版的代码 try (FileReader fr = new FileReader(a.txt); FileWriter fw = new FileWriter(b.txt);) int ch = 0; while (ch = fr.read() != -1) fw.write(ch); catch (IOException e) e.printStackTrace(); 4 计算一段代码的运行时间ForDemo.javapackage cn.itcast_01;public class ForDemo extends GetTime Override public void code() f
14、or (int x = 0; x 100000; x+) System.out.println(x); GetTime.javapackage cn.itcast_01;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public abstract class GetTime / 需求:请给我计算出一段代码的运行时间 pub
15、lic long getTime() long start = System.currentTimeMillis(); / for循环 / for (int x = 0; x 10000; x+) / System.out.println(x); / / 视频 / try / BufferedInputStream bis = new BufferedInputStream( / new FileInputStream(a.avi); / BufferedOutputStream bos = new BufferedOutputStream( / new FileOutputStream(b.
16、avi); / byte bys = new byte1024; / int len = 0; / while (len = bis.read(bys) != -1) / bos.write(bys, 0, len); / / bos.close(); / bis.close(); / catch (IOException e) / e.printStackTrace(); / / 再给我测试一个代码:集合操作的,多线程操作,常用API操作的等等. code(); long end = System.currentTimeMillis(); return end - start; public
17、 abstract void code();GetTimeDemo.javapackage cn.itcast_01;public class GetTimeDemo public static void main(String args) / GetTime gt = new GetTime(); / System.out.println(gt.getTime() + 毫秒); GetTime gt = new ForDemo(); System.out.println(gt.getTime() + 毫秒); gt = new IODemo(); System.out.println(gt.
18、getTime() + 毫秒); IODemo.javapackage cn.itcast_01;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class IODemo extends GetTime Override public void code() try BufferedInputStream bi
19、s = new BufferedInputStream( new FileInputStream(a.avi); BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(b.avi); byte bys = new byte1024; int len = 0; while (len = bis.read(bys) != -1) bos.write(bys, 0, len); bos.close(); bis.close(); catch (IOException e) e.printStackTrace
20、(); 5 手机彩铃IPhone.javapackage cn.itcast_02;public class IPhone implements Phone Override public void call() System.out.println(手机可以打电话了); MusicPhoneDecorate.javapackage cn.itcast_02;public class MusicPhoneDecorate extends PhoneDecorate public MusicPhoneDecorate(Phone p) super(p); Override public void
21、 call() super.call(); System.out.println(手机可以听音乐); Phone.javapackage cn.itcast_02;public interface Phone public abstract void call();PhoneDecorate.javapackage cn.itcast_02;public abstract class PhoneDecorate implements Phone private Phone p; public PhoneDecorate(Phone p) this.p = p; Override public
22、void call() this.p.call(); PhoneDemo.javapackage cn.itcast_02;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.util.Scanner;public class PhoneDemo public static void main(String args) Phone p = new IPhone(); p.
23、call(); System.out.println(-); / 需求:我想在接电话前,听彩铃 PhoneDecorate pd = new RingPhoneDecorate(p); pd.call(); System.out.println(-); / 需求:我想在接电话后,听音乐 pd = new MusicPhoneDecorate(p); pd.call(); System.out.println(-); / 需求:我要想手机在接前听彩铃,接后听音乐 / 自己提供装饰类,在打电话前听彩铃,打电话后听音乐 pd = new RingPhoneDecorate(new MusicPhon
24、eDecorate(p); pd.call(); System.out.println(-); / 想想我们在IO流中的使用 / InputStream is = System.in; / InputStreamReader isr = new InputStreamReader(is); / BufferedReader br = new BufferedReader(isr); BufferedReader br = new BufferedReader(new InputStreamReader(System.in); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( System.out); Scanner sc = new Scanner(System.in); RightPhoenDemo.javapackage cn.itcast_02;public class RingPhoneDecorate extends PhoneDecorate public RingPhoneDecorate(Phone p) super(p); Override public void call() System.out.println(手机可以听彩铃); super.call();
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1