ImageVerifierCode 换一换
格式:DOCX , 页数:13 ,大小:17.56KB ,
资源ID:6658289      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/6658289.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(java枚举基础入门.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

java枚举基础入门.docx

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