java核心技术卷一学习 2016.11.29

来源:互联网 发布:c语言编程简单乘法 编辑:程序博客网 时间:2024/06/03 17:04

面向对象程序设计OOP 第四章


使用预定义类

new Date()构造一个新的对象
String s = new Date().toString(); 将返回日期的字符串描述。


java类库中的LocalDate类

意义:将保存实践与给时间点命名分开。一个用来表示时间点的Date类,另外一个用来表示大家熟悉的日历表示法的LocalDate

    public static void b() {        LocalDate.of(1993, 12, 31);        LocalDate newYears = LocalDate.of(1999, 12, 31);        int Year = newYears.getYear();        int Month = newYears.getMonthValue();        int Day = newYears.getDayOfMonth();        System.out.println(Year + "-" + Month + "-" + Day);    }

其中方法LocalDate.of()会自动识别年月日的正确性。错误将无法通过编译。
LocalDate类中,plusDays方法用法

    public static void b() {        LocalDate.of(1993, 12, 31);        LocalDate newYears = LocalDate.of(1999, 12, 31);        int Year = newYears.getYear();        int Month = newYears.getMonthValue();        int Day = newYears.getDayOfMonth();        System.out.println(Year + "-" + Month + "-" + Day);        //aThoursanDaysLater为newYear的日期加1000天        LocalDate aThousanDaysLater = newYears.plusDays(1000);        Year = aThousanDaysLater.getYear();        Month = aThousanDaysLater.getMonthValue();        Day = aThousanDaysLater.getDayOfMonth();        System.out.println(newYears);        System.out.println(Year + "-" + Month + "-" + Day);    }

更改器方法与访问器方法

程序:应用LocalDate类,将显示当前月的日历。
日期: DateNow 月:month 日:day 每周里每日对应的值:value
getdayofweek()会获得这天是星期几的英文格式
getvalue()可得到这天的值
minusdays(n)生成当前日期之后或之前n天的日期。

import java.time.DayOfWeek;import java.time.LocalDate;import java.util.Scanner;public class DateNow {    public static void main(String[] args) {        // TODO Auto-generated method stub        // Scanner inScanner = new Scanner(System.in);        //        // System.out.println("Year:");        // int Year = inScanner.nextInt();        //        // System.out.println("Month:");        // int Month = inScanner.nextInt();        //        // System.out.println("Day:");        // int Day = inScanner.nextInt();        // LocalDate DateNow = LocalDate.of(Year, Month, Day);        LocalDate DateNow = LocalDate.now();        int Month = DateNow.getMonthValue();        int Day = DateNow.getDayOfMonth();        System.out.println(Day);        System.out.println(DateNow);        DateNow = DateNow.minusDays(Day - 1);        DayOfWeek Weekday = DateNow.getDayOfWeek();        int value = Weekday.getValue();        System.out.println(DateNow);        System.out.println(Weekday);        System.out.println(value);        System.out.println("MON TUE WED THU FRI SAT SUN");        for (int i = 1; i < value; i++) {            System.out.print("    ");        }        while (DateNow.getMonthValue() == Month) {            System.out.printf("%3d", DateNow.getDayOfMonth());            if (DateNow.getDayOfMonth() == Day) {                System.out.print("*");            } else {                System.out.print(" ");            }            DateNow = DateNow.plusDays(1);            if (DateNow.getDayOfWeek().getValue() == 1) {                System.out.println();            }        }        // LocalDate DateFirst = DateNow.plusDays(-Day + 1);        // System.out.println(DateFirst);    }}

用户自定义类

public class Employee {    private String name;    private double salary;    private LocalDate hireDay;    private int id;    private static int nextId = 1;    // private int idFor464 = assignId();    static {        System.out.println("this is static:");    }    public Employee(String name, double salary, int year, int month, int day) {        // super(name);        this.name = name;        this.salary = salary;        hireDay = LocalDate.of(year, month, day);    }    public String getName() {        return name;    }    public double getSalary() {        return salary;    }    public LocalDate getHireDay() {        return hireDay;    }    public void raiseSalary(double byPercent) {        double raise = salary * byPercent / 100;        salary += raise;    }}
    public static void a() {        Employee a = new Employee("a", 1000, 1990, 1, 1);        Employee b = new Employee("b", 2000, 2000, 12, 12);        System.out.println("a:" + a.getName());        System.out.println("b:" + b.getName());        a.setId();        b.setId();        swap(a, b);        System.out.println("a:" + a.getName());        System.out.println("b:" + b.getName());        Employee temp = a;        a = b;        b = temp;        System.out.println("x:" + a.getName());        System.out.println("y:" + b.getName());    }

从构造器开始

1 构造器与类同名
2 每个类可以有一个以上的构造器
3 构造器可以有0个、1个或多个参数
4 构造器没有返回值
5 构造器总是伴随着new操作一起调用


隐式参数与显示参数

Employee Junjie = new Employee("Junjie", 5000, 1993, 12, 31);Junjie.raiseSalary(10);

Junjie.raiseSalary(10)
第一参数成为隐式参数,是出现再方法名前的employee类对象。
第二参数位于方法名后面括号中的数值,这是一个显示参数。

在每一个方法中,关键字this表示隐式参数。

public void raiseSalary(double byPerecent){    double raise = this.salary*byPerecent / 100;    this.salary += raise;}

封装的优点

以下是典型的访问器方法。由于它们只返回实例域值,因此又称为域访问器。

    public String getName() {        return name;    }    public double getSalary() {        return salary;    }    public LocalDate getHireDay() {        return hireDay;    }

有些时候,需要获得或设置实例域的值。
1 一个私有的数据域
2 一个共有的域访问器方法
3 一个共有的域更改器方法

final实例域
必须确保在每一个构造器执行之后,这个域的值被设置,并且在后面的操作中,不能够再对它进行修改。

0 0
原创粉丝点击