Object 的方法
2 equals 是用来比较两个对象是否相等的方法。
区别:引用相等 与 对象相等
引用相等:使用“==”
对象相等: .equals method
equals 在Object类中声明,默认的比较规则是:比较引用建议覆盖,实现对象的比较(比较对象的就是比较对象的数据)。
覆盖
public boolean equals(Object obj)
指示其他某个对象是否与此对象“相等”。
equals
方法在非空对象引用上实现相等关系:
- 自反性:对于任何非空引用值
x
,x.equals(x)
都应返回true
。 - 对称性:对于任何非空引用值
x
和y
,当且仅当y.equals(x)
返回true
时,x.equals(y)
才应返回true
。 - 传递性:对于任何非空引用值
x
、y
和z
,如果x.equals(y)
返回true
,并且y.equals(z)
返回true
,那么x.equals(z)
应返回true
。 - 一致性:对于任何非空引用值
x
和y
,多次调用 x.equals(y) 始终返回true
或始终返回false
,前提是对象上equals
比较中所用的信息没有被修改。 - 对于任何非空引用值
x
,x.equals(null)
都应返回false
。
Object
类的 equals 方法实现对象上差别可能性最大的相等关系;即,对于任何非空引用值 x
和 y
,当且仅当 x
和 y
引用同一个对象时,此方法才返回 true
(x == y
具有值 true
)。
注意:当此方法被重写时,通常有必要重写 hashCode 方法,以维护 hashCode 方法的常规协定,该协定声明相等对象必须具有相等的哈希码。
3 hashCode()
1 hashCode() 方法要求与equals方法 一同覆盖
a: when two object compare is True ,the two Object should be the same hashCode() value
b: when two object compare is False ,the two Object should be the different hashCode() value
c: hashCode() 's value must be steady and consistency a object be created should not be changed
2 default value of hashCode() is a exchanging Integer of current address of Object, not the address
3 generally we use Object's Oid value as the hashCode's value .Oid is object's uniqueness identifier.on project we alaways use DB to create it,is DB's "Primary-Key".
抽象类--不具体的类abstract Object
1 Abstract Method:only the concept of action ,no concreted action
use : abstract keywords to modify ,and have no methodbody
2 the Class include abstract method is a abstract Class
use: abstract keywords to modify and could include the abstract method.
eg.plane graph must to caculate area;
public abstract class CRMSystem{
public abstract Client addClient(String name,String tel);
}
3 abstract method and abstract class is very suitable to be a System's analysis and design tool
4 abstact class can not to create instance directly .could define reference variable
5 abstract class only can be extended and a concreted Class extends a abstract ,
must implement all the abstract method!
接口:(interface)
1 all the methods are abstract,all the attributes are constant.
interface is to express pure abstract concept , have none concreted methods and attributes
2 can not be instance ,but can define variable.
3 interface variable could reference concreted achieved class's instance
4 interface only can be implement,a concreted class implement the interface ,have to use all the abstract method.
5 between interfaces can extends each other
6 a concreted class cn implement a lot a interfaces, 实现多继承现象,表示:一个概念即是xxx也是xxx
7 the attributes in interfaces ,default is "public static final";
8 the methods in the interface must be "public abstract"
9 concrete a interface , use keywords implements ,concrete in fact is a entends relationship .
interface and concreted father class is father and child!
1 interface is apoint , apoint the action of the implement Class ,and the Object 's action , implements the
same inteface's Object have the Same aspect!
2 interface is standard , is the visited view of standard---interface
3 interface descripte the pure Abstract concept!
4 interface's implement is a standard component, 可实现相互插接
Sun have defined a lot standard of Java
JDBC standard DOM, SAX,
homework:
1实现课堂代码
2实现完整的五子棋赢的判断
提示,要为五子棋提供equals方法
postcode:\d{6}
telphone:
date: \d{4}-\d{1,2}-\d{1,2}
example: ^\s*[0-9a-fA-F](,|\s)\s*[0-9a-fA-F]\s*$
Object
1 finalize() 当对象被垃圾回收时候,由垃圾搜集器调用,垃圾收集器是一个后台,低优先级的线程。
可以覆盖,填写一些资源回收代码。
但是有可能不可靠。
if cover it , must use super.finalize();
finalize() is seldom used in reality work
unsuggested be covered (finalize)
2 clone()
1 one class can override clone(), to create itself transcript
a general use Object's clone() to create the instance of transcript
b when use Object's clone , this Class must to implement Cloneable
c the system offered clone() method is 浅层复制
包装类
1八个基本类型有8个包装类
2 包装类实例内部就是一个基本类型
3 包装类可以将数值统一到java的继承体系中。
4 包装类是final的,包装类采用不变模式设计。
5 自动 包装!自动拆包,是Java5以后 提供的语法
注意一点,自动 包装以后 的类型是“包装类”类型
6 包装类还是对应数据类型的工具方法库
Java的时间
1 long 使用太不方便
2 Date 是Java早期提供的日期时间类型,现在不推荐使用
3 Calendar 是新的日期类型,提供了完善的日期操作方法
Calendar 是日历,历法,是抽象类。
GregorianCalendar 是历法的公历实现。
Calendar.getInstance()是一个工厂方法,
默认的实例包含当前时间。
4 简单的日期运算
5 日期的输出与输入 SimpleDateFormat
6 日期应用,如人的生日
7 包装类可以表达NULl这个状态statement,表示没有,不清楚,
有的时候比基本类型,能够更准确的表示业务逻辑!
4 Calendar 有些设计过度,造成使用繁琐。使用的不是十分广泛
Date尽管不推荐了,还是有很多软件在使用。
第三方的API提供了对日期的很好支持。
工厂方法:屏蔽了复杂的事物创建过程,提供了简洁的方法。
工厂方法经常用来创建接口或者抽象类的实例。
第三方包的使用:
1 在项目文件夹下创建文件夹lib,
2 复制commons-lang-2.5.jar 到lib
3 在Eclipse中刷新项目(F5)
4 选择项目的Properties->Java Build Path->
Libraribs->Add Jars->选择 lib\commons-lang-2.5.jar
*完成以后相当于将commons-lang-2.5.jar放置到了
CLASSPATH上了。
作业:
1 熟悉课堂代码,洗牌操作
2 熟悉使用commons-lang-2.5.jar, StringUitls
RandomUtils NumberUtils
DateUtils DateFormatUtils 参考源代码的实现。
3 实现构造器:public Card(String cardName){...}
预习:内部类和Java 集合框架
String
1 any String's 字面量 is instance of String
2 the Object of String couldn't change---Unchanged Pattern (死脑筋--一根筋)
3 inside of the String contains a char[]
4 String the Same 字面量 always is the same reference of a String
include haven been optimized by the javac's String
5 to Compare two object is the same of not : .equals()
字符串的编码
1 Unicode,八万多字段编码,中-20013
A:100 0001, 中:0100 1110 0010 1101
2 Java char 65535,采用定长16位编码(UTF-16BE)
A:0x0041, 中:0x4e2d
A:0000 0000 0100 0001 中:0100 1110 0010 1101
3 UTF-8,采用变长方案,支持1~6字节,英文1字节,中文三字节
A:0100 0001(以0开始,128个英文)
中:1110xxxx 10xxxxxx 10xxxxxx
11100100 10111000 10101101
e 4 b 8 a d
3 GBK 中国国家标准。
GB2312: 变长方案 1~2字节,1字节与ASCII一样,2字节为中文。 ASCII +中文
早期支持6000多汉字。包含:“玥”,“喆”
A:0x41 中:d6d0
GBK是GB2312的超集,包含20000
4 ASCII 0~127 英文 数字 和 符号
扩展到 0 ~255
练习:‘中‘ -> uft-8;
char c = '中'
byte[] utf8 = new byte[3];
char[], String,StringBuilder 都是字符序列
StringBuffer and StringBuilder
1 StringBuilder 是变长字符序列
2 StringBuilder methods:append() , insert() ....all return back the builder's own reference itself
3 if the soft need a great numbers of chars to deal with , suggest to use StringBuilder
4 String s = s1 + s2;//Java just do it like below
String s = new StringBuilder(s1).append(s2).toString();
String s = s1+s2+s3+s4;
String s = new StringBuilder(s1).append(s2).append(s3).append(s4).toString();
5 StringBuffer 's API is almost the same with StringBuilder
StringBuffer is offered by Java Early Time! a little slow, ThreadSafe
StringBuilder is offered after Java5 , fast speed,NO ThreadSafe
正则表达式
demands:
1 could understand complex expression
2 can write easy expression
语法:
1 字符类[]
[abc] 匪兵[甲乙丙丁]
[abc] a、b 或 c(简单类)
[^abc] 任何字符,除了 a、b 或 c(否定)
[a-zA-Z] a 到 z 或 A 到 Z,两头的字母包括在内(范围)
[a-d[m-p]] a 到 d 或 m 到 p:[a-dm-p](并集)
[a-z&&[def]] d、e 或 f(交集)
[a-z&&[^bc]] a 到 z,除了 b 和 c:[ad-z](减去)
[a-z&&[^m-p]] a 到 z,而非 m 到 p:[a-lq-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 次
String 对正则表达式支持
1 split , replaceAll, match
作业:
1 一个正则表达式,匹配:
1,3
1, 3
a,b
A,B
A B
^\s*[\da-fA-F],[\da-fA-F]\s*
1 写一个StringUtils提供如下方法:
1) 返回一个字符串在另外一个字符串中出
现多次的位置(以数组的方式返回)
2) 检查一个字符串是否为“回文”
abcddcba
2 在StringUtils中增加如下方法:
截取一个email字符串中的用户名并返回
统计一个字符在某一个字符串中出现的次数
(连续出现的多个字符算一次)并返回
aabbccddeeaa
3 实现字符串左部填充
"1234" -> "######1234"(10位)
public static String leftPad(
String str, int length, char c)
预习:
包装类