Object、String、Date

Object:
概述:Object 是类层次结构的根类。其他所有类都使用 Object 作为超类。
构造方法:
Object(),
为什么子类构造方法默认调用父类的无参构造。
如果没写构造方法,为什么系统默认会提供一个无参构造方法。

成员方法:
	1) public final Class getClass()
		返回对象的运行时类。
		Class: 类类型,用来描述类型的类。
			String getName(): 返回该类的全限定名
		
		注意事项:
			a. 返回的是运行时类
			b. 用final修饰, 所有对象该方法的行为一致。
	
	2) public String toString()
		一个对象的字符串形式的简介,要求简单易懂。
		默认实现:
			getClass().getName() + "@" + Integer.toHexString(hashCode())
			
		默认实现不能很好的体现对象,所以推荐子类都重写toString()
		重写:
			根据关键域去重写
			快捷键生成
	
	3) public boolean equals(Object obj)
		判断两个对象是否"相等";
		默认实现:
			return this == obj;
			
		实体类:不同对象代表不同实体,采用默认实现即可。
		值类:关键域相等,就认为两个对象相等,应该重写equals()
		
		equals方法的协定:
			a. 自反性
			b. 对称性
			c. 传递性
			d. 一致性
			e. 非空性
		Java语法没有强制要求遵循这些规则,但是如果违反的话,可能会给程序带来灾难性的后果。
		
		重写:
			a. 子类没有新的要比较的属性,就用instanceof进行判断
			Demo:
			public boolean equals(Object obj) {
				if(obj instanceof Demo) {
					Demo demo = (Demo) obj;
					// 用关键属性进行比较
				}
				return false;
			}

			遵守里式替换原则
			
			b. 子类有新的要比较的属性, 就用 getClass() 方法进行判断
			Demo:
			public boolean equals(Object obj) {
				if (obj == null || obj.getClass() != getClass()) {
					return false;
				}
				Demo demo = (Demo) obj;
				// 用关键属性进行比较
			}
			没有遵循里式替换原则
		
			c. 快捷键生成 (b方式)
		
		== 和 equals()的比较
			== :	如果比较是基本数据类型, 判断他们的值是否相等
				如果比较是引用数据类型, 判断他们是否是同一个对象
			
			equals():	不能比较基本数据类型
				比较引用数据类型, 如果没有重写Object的equals(),默认判断是否是同一个对象。
				如果重写了,一般是根据该对象的值进行判断
	4) public int hashCode()
		获取该对象的散列码
		
		优秀hash算法:模拟随机映射。
		默认实现:将对象的内存地址映射成一个整数值。
		
		hashCode()的协定:
			a. 一致性
			b. 如果两个对象是"相等"的,那么他们的hashcode也一定相等。
				如果重写equals(),一定也要重写hashcode().
			c. 如果两个对象"不相等", 不要求他们的hashcode一定也不相等。但是最好不一样,这样提高hash表的效率。
		
		重写:
			a.
				int hash= c;
				// 根据每一个关键域的hashcode去生成
				31 * i == (i << 5) - i;	
				hash= 31 * field.hashCode + hash
				...
				return hash
			b. 快捷键生成
	
	5) protected void finalize()
		垃圾回收器回收该对象之前,会调用该方法。
		作用:可以释放系统资源
		
		注意事项:
			a. 如果直接调用,相当于手动释放资源,并不会回收该对象。
			b. 释放系统资源最好不要放在该方法里面,推荐用 try...catch...finally ,延迟性。
	
	6) protected Object clone()
		返回该对象的一个克隆
		默认:浅拷贝
		如何实现深拷贝:
			a. 实现Cloneable接口
			b. 调用super.clone(), 完成父类中定义的数据和基本数据类型的拷贝。
			c. 一级一级实现深拷贝。

总结:
	理解: finalize(), clone(), getClass()
	掌握: toString(), equals(), hashcode()
  1. String类的概述和使用
    (1)概述:表示一个不可变的字符序列
    (2)构造方法:
    public String()
    public String(byte[] bytes)
    public String(byte[] bytes,int offset,int length)
    public String(char[] value)
    public String(char[] value,int offset,int count)
    public String(String original)
    public String(StringBuffer sb)
    String s = “hello world”;
    (3)字符串的特点
    A:字符串一旦被赋值,就不能改变。
    注意事项:这个字符串对象的值不能改变,但是指向这个字符串的引用变量可以改变
    B:字面值作为字符串对象和通过构造方法创建字符串对象的不同
    String s = “hello”;
    String s = new String(“hello”);
    (4)字符串的面试题(看程序写结果)
    A:和equals()
    String s1 = new String(“hello”);
    String s2 = new String(“hello”);
    System.out.println(s1
    s2);
    System.out.println(s1.equals(s2));

     	String s3 = new String("hello");
     	String s4 = "hello";
     	System.out.println(s3==s4);
     	System.out.println(s3.equals(s4));
     
     	String s5 = "hello";
     	String s6 = "hello";
     	System.out.println(s5==s6);
     	System.out.println(s5.equals(s6));	
     B:字符串的拼接
     	String s1 = "hello";
     	String s2 = "world";
     	String s3 = "helloworld";
     	System.out.println(s3==(s1+s2));
     	System.out.println(s3.equals(s1+s2));
    
     	System.out.println(s3==("hello"+"world"));
     	System.out.println(s3.equals("hello"+"world"));
    

    (5)字符串的功能
    A:判断功能
    boolean equals(Object obj)
    boolean equalsIgnoreCase(String str)
    boolean contains(String str)
    boolean startsWith(String str)
    boolean endsWith(String str)
    boolean isEmpty()
    B:获取功能
    int length()
    char charAt(int index)
    int indexOf(int ch)
    int indexOf(String str)
    int indexOf(int ch,int fromIndex)
    int indexOf(String str,int fromIndex)
    String substring(int start)
    String substring(int start,int end)

     C:转换功能
     	byte[] getBytes()
     	char[] toCharArray()
     	static String valueOf(char[] chs)
     	static String valueOf(int i)
     	String toLowerCase()
     	String toUpperCase()
     	String concat(String str)
    
     D:其他功能
     	a:替换功能 
     		String replace(char old,char new)
     		String replace(String old,String new)
     	b:去空格功能
     		String trim()
     	c:按字典比较功能
     		int compareTo(String str)
     		int compareToIgnoreCase(String str) 
     	d: 分隔
     		String[] split(String regex)
    
  2. StringBuffer
    (1)概述:表示一个字符串缓冲区,它的值时可变的。
    (2)StringBuffer的构造方法
    public StringBuffer()
    public StringBuffer(int capacity)
    public StringBuffer(String str)

    (3)StringBuffer的常见功能
    A:添加功能
    public StringBuffer append(String str)
    public StringBuffer insert(int offset,String str)
    B:删除功能
    public StringBuffer deleteCharAt(int index)
    public StringBuffer delete(int start, int end)
    C:替换功能
    public StringBuffer replace(int start,int end,String str)
    D:反转功能
    public StringBuffer reverse()
    E:截取功能(注意这个返回值)
    public String substring(int start)
    public String substring(int start,int end)

    (4)面试题
    A:String,StringBuffer,StringBuilder的区别
    B:StringBuffer和数组的区别?
    C:String作为形式参数,StringBuffer作为形式参数。
    不可变的对象当做参数传递,相当于值传递。

3:Date/DateFormat
(1)Date是日期类,可以精确到毫秒。
A:构造方法
Date() 表示创建对象的系统时间
Date(long time) 基准时间:epoch(1970年1月1日 00:00:00)
B:成员方法
setTime(long time);
getTime()
获取当前时间的毫秒值:
new Date().getTime();
System.currentTimeMillis(); (推荐使用)
C:日期和毫秒值的相互转换
案例:你来到这个世界多少天了?
(2)DateFormat针对日期进行格式化和针对字符串进行解析的类,但是是抽象类,所以使用其子类SimpleDateFormat
A:构造方法:
SimpleDateFormat(); // 默认模式
SimpleDateFormat(String pattern); // 指定模式
模式字符:
年: y
月:M
日:d
时:H
分:m
秒:s
B:成员方法
String format(Date date)
Date parse(String s)
C:案例:
制作了一个针对日期操作的工具类。(封装)
4: Math
(1)Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
(2)方法
public static int abs(int a)
public static double ceil(double a)
public static double floor(double a)
public static int max(int a,int b)
public static double pow(double a,double b)
public static double random() // 伪随机数
public static int round(float a) // 四舍五入
public static double sqrt(double a) // 平方根,如果是负数,返回NaN
public static double sin(double a)
public static double asin(double a)
public static double cos(double a)
public static double acos(double a)
public static double tan(double a)
public static double atan(double a)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章