loading黑马程序员之Java一些对象介绍(1-1)

来源:互联网 发布:bluefish淘宝 编辑:程序博客网 时间:2024/04/28 13:10
-------android培训 、java培训、期待与您交流! ----------
Java的对象和方法成千上万,我们不可能学完,也完全没有这个必要。
我们只需要掌握一些常用的类和方法即可,然后触类旁通。
当然,英文是必不可少的,至少连的读懂API也是一种基本功。
官方API:http://docs.oracle.com/javase/7/docs/api/
系统属性类:Properties
package com.heima;import java.util.Iterator;import java.util.Properties;import java.util.Set;/** * 说明: * 1.静态和动态设置系统属性 * 2.获取并遍历系统属性 */public class PropertiesDemo {/** * @param args */public static void main(String[] args) {// 获取系统属性Properties properties = System.getProperties();// 静态设置系统属性【在javac的时候设置】properties.setProperty("myStaticPropertiesKey","myStaticPropertiesValue");// 动态设置系统属性【在java的时候设置,也就是jvm启动时】// :java -DmyDynamicPropertiesKey=myDynamicPropertiesValue 当前类名// 因为peoperties继承Hashtable<K,V>,所以可以直接获取键集,用来遍历// 或则用Properties自带的方法:stringPropertyNames()// 用增强for循环遍历,也可以用Iterator迭代System.out.println("-----------用增强for循环遍历所有系统属性-------------");for (String key : properties.stringPropertyNames()) {System.out.println(key + "--------------" + properties.get(key));}System.out.println("-----------用Iterator迭代所有系统属性-------------");Set<Object> set = properties.keySet();for (Iterator<Object> iterator = set.iterator(); iterator.hasNext();) {Object obj = iterator.next();System.out.println(obj + "--------------" + properties.get(obj));}}}
运行时类:Runtime
package com.heima;/** *用Java运行时类Runtime模拟window的cmd执行命令 */public class RuntimeDemo {public static void main(String[] args) {//得到一个Runtime的单例(single instance)Runtime runtime = Runtime.getRuntime();try {//模拟cmd命令行//如果没有指定全路径,则会在系统环境变量path里查找,找不到则会抛出IOException//用记事本打开当前目录下的RuntimeDemo.txt文本文件Process process = runtime.exec("notepad.exe RuntimeDemo.txt");//线程阻塞5秒Thread.sleep(5000);//杀死子进程,不过只能杀自己创建的进程。调用windows api才可以杀死windows系统进程process.destroy();} catch (Exception e) {e.printStackTrace();}}}


日期类:Date
package com.heima;import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;/** *  * 功能: * 通过SimpleDateForma实现格式化日期和解析日期字符串 * */public class DateDemo {public static void main(String[] args) {//初始化为系统当前日期Date date = new Date();//用yyyy年MM月dd日 E hh:mm:ss这种模式来格式化和解析日期DateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 E hh:mm:ss");try {System.out.println("格式化后的系统当前日期:"+sdf.format(date));System.out.println("解析后的指定日期:"+sdf.parse("2013年12月28日 星期六 11:11:11"));} catch (ParseException e) {e.printStackTrace();}}}
日历类:Calendar
package com.heima;import java.util.Calendar;/** * 说明: 1. 用Calendar格式化输出指定时间 2. 对指定时间"加减"年月日等 */public class CalendarDemo {public static void main(String[] args) {Calendar calendar = Calendar.getInstance();// 设置时间为2014年1月1日calendar.set(2014, 0, 1);printCalendar(calendar);//2014年1月1日的前一个月当前时刻calendar.add(Calendar.MONTH, -1);printCalendar(calendar);}// 格式化输出指定时间public static void printCalendar(Calendar calendar) {String[] months = new String[] { "一", "二", "三", "四", "五", "六", "七","八", "九", "十", "十一", "十二" };// 外国的第一天是星期天,并且从0开始String[] weeks = new String[] { "", "星期天", "星期一", "星期二", "星期三", "星期四","星期五", "星期六", };int year = calendar.get(Calendar.YEAR);int month = calendar.get(Calendar.MONTH);int day_of_month = calendar.get(Calendar.DAY_OF_MONTH);int hour_of_day = calendar.get(Calendar.HOUR_OF_DAY);int minute = calendar.get(Calendar.MINUTE);int second = calendar.get(Calendar.SECOND);int day_of_week = calendar.get(Calendar.DAY_OF_WEEK);sop(year + "年" + months[month] + "月" + day_of_month + "日" + "\t"+ hour_of_day + ":" + minute + ":" + second + "\t"+ weeks[day_of_week]);}public static void sop(Object obj) {System.out.println(obj);}}
数学类:Math
package com.heima;import java.text.DecimalFormat;import java.util.Random;/** *说明: *1.Math类的常用方法:ceil,floor,round,pow,random *2.Random类生成指定区间的伪随机数 */public class MathDemo {public static void main(String[] args) {// ceil返回大于参数的最小整数,floor反之,round表示四舍五入sop("ceil:\t 3.14->" + Math.ceil(3.14) + "\t -3.14->"+ Math.ceil(-3.14));// pow(a,b) a的b次幂sop("pow(2,3)->" + Math.pow(2, 3));// 【随机数的生成是根据某种算法来的,所以是有规律可循的】sop("--------随机返回一个(0.0,1.0]之间的伪随机数-----------");for (int i = 0; i < 10; i++) {double random = Math.random();sop(random);}sop("--------随机返回一个[1,6]之间的随机整数-----------");for (int i = 0; i < 10; i++) {int dice = (int) (Math.random() * 6 + 1);sop(dice);}sop("--------随机返回一个(1,10]之间的整数--------------");for (int i = 0; i < 10; i++) {int r = new Random().nextInt(10) + 1;sop(r);}sop("--------保留小数的后两位--------------");DecimalFormat df = new DecimalFormat("###.00");for (int i = 0; i < 10; i++) {double s = Math.random() * 10;String t = df.format(s);sop(s + "经过四舍五入并保留小数后两位后:" + t);}// for(int i=0;i<10;i++){// double s = Math.random();// double t = Math.round(s*100)/100.0;// sop(s+"经过四舍五入并保留小数后两位后:"+t);// }}public static void sop(Object obj) {System.out.println(obj);}}

-------android培训 、java培训、期待与您交流! ----------

0 0
原创粉丝点击