zoukankan      html  css  js  c++  java
  • 第07章 类

    第07章 类

    JDK即Java开发工具包(Java Development Kit,JDK)
    泛型(Generic)
    for-each循环
    自动装包/拆包(Autoboxing/unboxing)
    枚举(Enums)
    可变参数(Varargs)
    静态导入(Static Imports)
    模块化(Modularization)
    多语言支持(Multi-Language Support)
    开发者生产力(Developer Productivity)
    性能(Performance)
    通过查看API文档,知道“Math”类中的所有方法和字段都是可以直接访问的,在Java中,称之为静态方法和静态字段。
    如何访问静态成员:
    类名.方法名称
    类名.字段名称
    Math类中包含了两个静态常量:
    Math.PI //表示数学常量π
    Math.E //表示和e最可能接近的近似值
    Math类提供了常用的三角函数:
    Math.sin //表示正弦函数
    Math.cos //表示余弦函数
    Math.tan //表示正切函数
    Math.asin //表示反正弦函数
    Math.acos //表示反函数
    Math.atan //表示反正切函数
    Math.atan2 //表示反余切函数
    Math类还提供了幂函数、指数函数和自然对数函数:
    Math.pow //返回a的b次方
    Math.exp //返回e的a次方
    Math.log //返回a的常用对数值
    Math类提供了常用的数学运算函数:
    Math.abs(x) //返回绝对值
    Math.sqrt(x) //返回平方根
    Math.max(x,y) //返回最大值
    Math.min(x,y) //返回最小值
    Math类提供了角度与弧度相关的转换运算方法:
    Math.toDegrees(double angrad) //将弧度转换成角度值
    Math.toRadians(double angdeg) //将角度值转换成弧度
    Math类提供了四舍五入的运算及截断运算:
    Math.round(double e) //四舍五入运算
    Math.floor(double e) //返回不大于e的最大整数
    Math类提供了一个专门用来产生随机数的函数:
    Math.random() //用来产生随机数的函数

    测试时间和日期的类Date
    对象名.方法名
    对象名.字段名

    如何将类变成对象呢?因为类是模板,对象是实物。
    在Java中,从模板中创建一个实物是使用关键字“new”来实现的。
    针对非静态的类或方法,就要使用对象来操作,不能使用类来操作。

    new Date()
    Date(int year, int month, int date);
    Date(int year, int month, int date, int hrs, int min);

    import java.util.Date;
    public class Math{
    public static void main(String[] args){
    System.out.println(new Date()); //输出当前时间
    }
    }

    使用toString(),将时间日期按照字符串的形式显示出来。

    import java.util.Date;
    public class Math{
    public static void main(String[] args){
    System.out.println(new Date()); //输出当前时间

    Date ld = new Date();
    System.out.println(ld.toString());

    System.out.println(new Date().toString());
    }
    }

    测试日历的类:GregorianCalendar,它是Calendar类的一个扩展而已。
    getDay()、getMonth()等方法函数已不推荐使用。

    GregorianCalendar类的常用方法:
    public int get(int field)

    import java.util.*;
    public class GregorianCalendars{
    public static void main(String[] args){
    int x = gc.get(Calendar.MONTH);
    System.out.println(x);
    }
    }

    import java.util.Calendar;
    import java.util.GregorianCalendar;
    import java.text.SimpleDateFormat;
    public class gc{
    public static void main(String[] args){
    GregorianCalendar gc = new GregorianCalendar();
    //gc.set(Calendar.MONTH, 11); //将月份用指定值11替换。
    int y = gc.get(Calendar.YEAR);
    int x = gc.get(Calendar.MONTH) + 1; //数组返回0-11,因此返回值加1
    System.out.println(y + "-" + x);

    String out = new SimpleDateFormat("yyyyMMdd").format(Calendar.getInstance().getTime());
    System.out.println(out);
    }
    }

    //年 gc.get(Calendar.YEAR)
    //月 gc.get(Calendar.MONTH) + 1
    //日 gc.get(Calendar.DATE)
    //时 gc.get(Calendar.HOUR)
    //分 gc.get(Calendar.MINUTE)
    //秒 gc.get(Calendar.SECOND)

    //如何设置系统时间的方法:
    public final void set(int year, int month, int date)
    public final void set(int year, int month, int date, int hour, int minute)
    public final void set(int year, int month, int date, int hour, int minute, int second)

     

  • 相关阅读:
    AutoCAD利用VBA设置线型和添加用户自定义线性
    AutoVBA利用for循环创建同心圆弧
    AutoVBA利用Hacth对象填充图元对象
    AutoVBA利用AddArc方法创建Arc对象
    2011年6月5日星期天
    AutoVBA控件的tabindex和tabstop属性及with语句
    AutoVBA在绘图空间创建直线对象
    AutoVBA利用toolbar创建自己的工具栏
    AutoVBA调用AddCricle方法绘制圆
    AutoCAD利用VBA宏绘制多重平行线
  • 原文地址:https://www.cnblogs.com/QQ9888267/p/6115236.html
Copyright © 2011-2022 走看看