day05(Object,tostring(),equals(),System,Date,SimpleDateFormat,拆装箱,正则表达式)

2022-11-03,,,,

Object类,

    是所应类的父类;

   拥有自己的方法:常用的    红颜色标记的为常用的方法

toString()

  用法:打印对象的地址值

getClass()  获取当前类的字节码文件
getName() 获取字节码文件的名字
Integer.toHexString 返回指定参数的16进制字符串
hashCode() 返回该对象的哈希码值 public String toString() { Object类中的tostring值 (地址值)
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

  eg:01

如果是字符串   则打印的还是   字符串本身

public class toStringDemo {
public static void main(String[] args) {
String s="a";//如果是字符串则打印的是字符串
String s1=new String("a");
System.out.println(s.toString());
System.out.println(s1.toString());
}
}

  

输出结果
a
a

 问题:toString()在这里打的是字符串值本身  而不是地址值

   查看源码:

public String toString() {
return this;
}

  可知   String类中重写了 toString()方法  返回值为他本身的值

 eg:02

public class Student {
private String name;
private int age;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

  

public class toStringDemo {
public static void main(String[] args) {
Student s=new Student(null,16);
Student s1=new Student(null,15);
System.out.println(s.toString());
System.out.println(s1.toString());
}
}

  

输出结果:
day05.Student@4d43691d
day05.Student@4aa168c

  原因:

    Student 类中没有重写toString()方法  所以实现的是Object类中的toString()方法

在Student类中重写toString();

	@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}

  

输出结果:
Student [name=null, age=16]
Student [name=null, age=15]

  

总结 :  如果该类中没有重写toString ()方法  则输出该对象的地址值。

equals()

比较地址值  和"=="是一个作用(继承了父类的equals())

 public boolean equals(Object obj) {
return (this == obj);
}

  如果相比较对象的内容,需要重写equals()

eg:03

public class StudentTest {
public static void main(String[] args) {
Student s=new Student(null,15);
Student s1=new Student(null,15);
if (s.equals(s1)) {
System.out.println(true);
}else{
System.out.println(false);
}
}
}

  (Student 没有重写equals方法)

    比较的是地址值  ( s 和 s1 的地址值不一样)

输出结果:
false

  Student 重写equals方法   (采用系统自动生成equals())

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}

  重写后的方法可以看出  比较的是对象中存储的内容

输出结果:
true

System类

    特点:1.不能创建对象

       2.很工具类相似

    成员方法:

//赋值数组

// static void     arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

src - 源数组。

srcPos - 源数组中的起始位置。

dest - 目标数组。

destPos - 目标数据中的起始位置。

length - 要复制的数组元素的数量。

public class Demo01 {
public static void main(String[] args) {
int[] arr={1,2,3,4,5};
int[] arr1=new int[6];
System.arraycopy(arr, 0, arr1, 0, 4);
for (int i = 0; i < arr1.length; i++) {//没有赋值的会存在默认值 int在堆中 默认值是0
System.out.print(arr1[i]+" ");
}
}
}

  

输出结果:
1 2 3 4 0 0

//获取当前系统时间的毫秒值      

static long     currentTimeMillis()

public class Demo01 {
public static void main(String[] args) {
long l = System.currentTimeMillis();//获取系统时间的毫秒数
long shi=l/1000/60/60;//转化为小时数
long nian=shi/24/365;//转化为年
System.out.println(nian+1970);//因为这个时间是由1970年开始算的 所以加1970
}
}

  

输出结果:
2017

  

//终止当前正在运行的 Java 虚拟机。参数用作状态码;根据惯例,非 0 的状态码表示异常终止。
    public static void exit(int status)
public class Demo01 {
public static void main(String[] args) {
for (int i = 0; i <100; i++) {
if (i==10) {
System.out.println(i);
System.exit(0);//虚拟机关闭
}
}
}
} 输出结果:
10

  //垃圾回收器

public static void gc()
public class Demo01 {
public static void main(String[] args) throws Throwable {
for (int i = 0; i < 280000; i++) {
new Test();
}
}
}
class Test{
@Override
protected void finalize() throws Throwable { System.out.println("垃圾回收器");
}
}

  

输出结果:
垃圾回收器
垃圾回收器
垃圾回收器
垃圾回收器

  

日期类

    Date

      构造方法

        Date();//获取当前系统时间的date对象;

        Date(long date);//获取指定毫秒值得date对象

public class Demo01 {
public static void main(String[] args) throws Throwable {
Date d=new Date();
System.out.println(d);
long l=System.currentTimeMillis();//获取当前系统时间的毫秒值
Date d1=new Date(l);
System.out.println(d1);
}
}

  

输出结果:
Sun Aug 27 19:11:14 GMT+08:00 2017
Sun Aug 27 19:11:14 GMT+08:00 2017

  这样的格式看起来不是特别友好  所以我们就存在数据格式化:

    SimpleDateFormat

/*
* SimpleDateFormat:
* 格式化:
* Date --- String
* 2049-8-26 2049年8月26日
* String format(Date date)
* 解析:
* String --- Date
* "2049-8-26"
* Date parse(String source)
*
* 构造方法:
* SimpleDateFormat() :使用默认的模式进行对象的构建
* SimpleDateFormat(String pattern) :使用的指定的模式进行对象的构建
*
* 注意:Exception in thread "main" java.text.ParseException: Unparseable date: "49年9月26日 下午1:29"
* 解析的字符串,模式必须和构建对象的模式一样
*
*/

  

      格式化    把不是特别友好的时间进行格式化   格式化我们想要的格式    时间  转化为  字符串

public class Demo01 {
public static void main(String[] args) throws Throwable {
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
//获取系统当前时间的Date对象d
Date d=new Date();
//格式化
String str = format.format(d);
System.out.println(str);
}
}

      解析   把字符串格式转化 时间对象

public class Demo01 {
public static void main(String[] args) throws Throwable {
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String str="2017年08月27日 19:22:23";
Date date = format.parse(str);
System.out.println(date.toLocaleString());
}
}

  

输出结果:
2017-8-27 19:22:23

  

1.1.1 Calendar类概述

Calendar是日历类,在Date后出现,替换掉了许多Date的方法。该类将所有可能用到的时间信息封装为静态成员变量,方便获取。

Calendar为抽象类,由于语言敏感性,Calendar类在创建对象时并非直接创建,而是通过静态方法创建,将语言敏感内容处理好,再返回子类对象,

如下:

Calendar类静态方法

Calendar c = Calendar.getInstance();  //返回当前时间

public class Demo01 {
public static void main(String[] args) throws Throwable {
Calendar c=Calendar.getInstance();//获取1970年日历
Date date=new Date();
c.setTime(date);//设置日历为系统年的日历 //查询
int year=c.get(c.YEAR);//获取系统年份
System.out.println(year);
int mouth=c.get(c.MONTH)+1;//因为美国人是从0月开始的 所以加1
System.out.println(mouth);
int day=c.get(c.DAY_OF_MONTH);//获取系统的月份的第几天
System.out.println(day);
int weekday=c.get(c.DAY_OF_WEEK)-1;//获取这个星期的第几天 美国人一个星期的第一天是星期天
if(weekday==0) { //如果是0则 让它返回为7
weekday=7;
}
System.out.println(weekday);
          System.out.println("------------");
//修改
c.set(c.YEAR, 2018);//修改年份为2018年
System.out.println(c.get(c.YEAR));
c.set(c.MONTH, 8);
System.out.println(c.get(c.MONTH));
c.set(c.DAY_OF_MONTH, 8);
System.out.println(c.get(c.DAY_OF_MONTH));
          System.out.println("------------");
//添加
c.add(c.YEAR, -1);//给年份+(-1)
System.out.println(c.get(c.YEAR));
c.add(c.MONTH, 1);
System.out.println(c.get(c.MONTH));
c.add(c.DAY_OF_MONTH, 1);
System.out.println(c.get(c.DAY_OF_MONTH)); }
}

  

输出结果:
2017
8
27
7
------------
2018
8
8
------------
2017
9
9

  

拆装箱,

   

                   

                                                                             4种8类

基本数据类型

装          箱

↓↓          ↑↑

箱           拆

包装类

   

    自动装箱  基本数据类型>>>>引用数据类型

                     拆箱  引用数据类型>>>>基本数据类型

public class Demo01 {
public static void main(String[] args) throws Throwable {
Integer i=10;//装箱
int s=i;//拆箱
System.out.println(s);
}
}

正则表达式

正则表达式是专门解决字符串规则匹配的工具。

正则表达式也是一个字符串,用来定义匹配规则。

参照帮助文档,在Pattern类中有简单的规则定义,可以结合字符串类的方法使用。

字符
x 字符 x
\\ 反斜线字符
字符类
[abc] a、b 或 c(简单类)
[^abc] 任何字符,除了 a、b 或 c(否定)
[a-zA-Z] a 到 z 或 A 到 Z,两头的字母包括在内(范围
预定义字符类
. 任何字符(与行结束符可能匹配也可能不匹配)
\d 数字:[0-9]
\D 非数字: [^0-9]
\s 空白字符:[ \t\n\x0B\f\r]
\S 非空白字符:[^\s]
\w 单词字符:[a-zA-Z_0-9]
\W 非单词字符:[^\w]
Greedy 数量词
X? X,一次或一次也没有
X* X,零次或多次
X+ X,一次或多次
X{n} X,恰好 n 次
X{n,} X,至少 n 次
X{n,m} X,至少 n 次,但是不超过 m 次

  

public class Demo01 {
public static void main(String[] args) throws Throwable {
String s="012365489";
/*判断qq号
8-13位
不能以0开头
不能含有字母*/
boolean b = s.matches("[1-9][0-9]{7,12}");
System.out.println(b);
}
}

  

day05(Object,tostring(),equals(),System,Date,SimpleDateFormat,拆装箱,正则表达式)的相关教程结束。

《day05(Object,tostring(),equals(),System,Date,SimpleDateFormat,拆装箱,正则表达式).doc》

下载本文的Word格式文档,以方便收藏与打印。