常用类
1.object
1.1介绍
object可以成为基类,超类,是所有类的父类或者祖宗类 如果一个类没有手动声明父类,那么它的父类则为object类 object类型的引用,可以指向任何类型的数据
object作为参数类型,可以赋值任何类型的 object作为返回类型
package com. qfedu ;
public class Student {
public static void main ( String [ ] args) {
Object o = new Student ( ) ;
Object a = new A ( ) ;
Object b = new B ( ) ;
useObject ( new Student ( ) ) ;
useObject ( new A ( ) ) ;
useObject ( new B ( ) ) ;
}
public static void useObject ( Object o) {
}
public static Object returnObject ( ) {
return new B ( ) ;
}
}
class A {
}
class B extends A {
}
1.2.1 getClass()
package com. qfedu ;
public class Demo01 {
public static void main ( String [ ] args) {
C c = new C ( ) ;
System . out. println ( c. getClass ( ) ) ;
Object o = new C ( ) ;
Class c1 = o. getClass ( ) ;
System . out. println ( c1) ;
Object o2 = new D ( ) ;
System . out. println ( o2. getClass ( ) ) ;
}
}
class C {
}
class D {
}
1.2方法
package java. lang ;
public class Object {
private static native void registerNatives ( ) ;
static {
registerNatives ( ) ;
}
public final native Class < ? > getClass ( ) ;
public native int hashCode ( ) ;
public boolean equals ( Object obj) {
return ( this == obj) ;
}
protected native Object clone ( ) throws CloneNotSupportedException ;
public String toString ( ) {
return getClass ( ) . getName ( ) + "@" + Integer . toHexString ( hashCode ( ) ) ;
}
public final native void notify ( ) ;
public final native void notifyAll ( ) ;
public final native void wait ( long timeout) throws InterruptedException ;
public final void wait ( long timeout, int nanos) throws InterruptedException {
if ( timeout < 0 ) {
throw new IllegalArgumentException ( "timeout value is negative" ) ;
}
if ( nanos < 0 || nanos > 999999 ) {
throw new IllegalArgumentException (
"nanosecond timeout value out of range" ) ;
}
if ( nanos > 0 ) {
timeout++ ;
}
wait ( timeout) ;
}
public final void wait ( ) throws InterruptedException {
wait ( 0 ) ;
}
protected void finalize ( ) throws Throwable { }
}
1.2.2 hashCode()
package com. qfedu ;
public class Demo02 {
public static void main ( String [ ] args) {
String str = "ab" ;
int hash1 = str. hashCode ( ) ;
System . out. println ( hash1) ;
E e = new E ( ) ;
System . out. println ( e. hashCode ( ) ) ;
}
}
class E {
int a;
int b;
public int hashCode ( ) {
return 3 ;
}
}
1.2.3 equals()
package com. qfedu ;
public class Demo03 {
public static void main ( String [ ] args) {
F f1 = new F ( ) ;
boolean b = f1. equals ( 1 ) ;
System . out. println ( b) ;
F f2 = new F ( ) ;
f2. a = 5 ;
F f3 = new F ( ) ;
f3. a = 5 ;
System . out. println ( f2. equals ( f3) ) ;
System . out. println ( f2 == f3) ;
System . out. println ( f2. equals ( 2 ) ) ;
}
}
class F {
int a;
public boolean equals ( Object obj) {
if ( this . getClass ( ) != obj. getClass ( ) ) {
return false ;
}
F f = ( F ) obj;
return this . a == f. a;
}
}
package com. qfedu ;
public class Demo03_2 {
public static void main ( String [ ] args) {
Person p1 = new Person ( ) ;
p1. setName ( "张钧甯" ) ;
p1. setGender ( "女" ) ;
p1. setCardId ( "666888" ) ;
Person p2 = new Person ( "高圆圆" , "女" , "666555" ) ;
Person p3 = new Person ( "高圆圆" , "女" , "666555" ) ;
boolean r = p1. equals ( p2) ;
boolean r2 = p2. equals ( p3) ;
System . out. println ( r2) ;
System . out. println ( "p2的hashCode:" + p2. hashCode ( ) ) ;
System . out. println ( "p3的hashCode:" + p3. hashCode ( ) ) ;
}
}
class Person {
private String name;
private String gender;
private String cardId;
public Person ( ) {
}
public Person ( String name, String gender, String cardId) {
this . name = name;
this . gender = gender;
this . cardId = cardId;
}
@Override
public int hashCode ( ) {
final int prime = 31 ;
int result = 1 ;
result = prime * result + ( ( cardId == null ) ? 0 : cardId. hashCode ( ) ) ;
return result;
}
@Override
public boolean equals ( Object obj) {
if ( this == obj) {
return true ;
}
if ( obj == null ) {
return false ;
}
if ( getClass ( ) != obj. getClass ( ) ) {
return false ;
}
Person other = ( Person ) obj;
if ( cardId == null ) {
if ( other. cardId != null ) {
return false ;
}
} else if ( ! cardId. equals ( other. cardId) ) {
return false ;
}
return true ;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public String getGender ( ) {
return gender;
}
public void setGender ( String gender) {
this . gender = gender;
}
public String getCardId ( ) {
return cardId;
}
public void setCardId ( String cardId) {
this . cardId = cardId;
}
}
1.2.4 clone()
package com. qfedu ;
public class Demo04 {
public static void main ( String [ ] args) throws CloneNotSupportedException {
G g = new G ( ) ;
g. a = 3 ;
g. b = 5 ;
G g2 = g. getClone ( ) ;
System . out. println ( g2. a+ "," + g2. b) ;
}
}
class G implements Cloneable {
int a;
int b;
public G getClone ( ) throws CloneNotSupportedException {
return ( G ) this . clone ( ) ;
}
}
1.2.5 toString()
package com. qfedu ;
public class Demo05 {
public static void main ( String [ ] args) {
J j = new J ( 1 , 6 ) ;
System . out. println ( j) ;
String str = j. toString ( ) ;
System . out. println ( str) ;
}
}
class J {
private int a;
private int b;
public J ( ) {
}
public J ( int a, int b) {
super ( ) ;
this . a = a;
this . b = b;
}
public int getA ( ) {
return a;
}
public void setA ( int a) {
this . a = a;
}
public int getB ( ) {
return b;
}
public void setB ( int b) {
this . b = b;
}
@Override
public String toString ( ) {
return "J [a=" + a + ", b=" + b + "]" ;
}
}
1.2.6 finalize()
package com. qfedu ;
public class Demo06 {
public static void main ( String [ ] args) {
new H ( ) ;
System . gc ( ) ;
}
}
class H {
public void finalize ( ) throws Throwable {
System . out. println ( "我被over了,请记住我!!!" ) ;
}
}
2.包装类
1.为什么需要包装类
很多方法的参数为object,可以赋值基本类型的值,需要把基本类型看作一种引用类型的对象 再实际需求中,我们也需要把字符串类型的数据转化为指定基本类型的值的方法,需要指定类
package com. qfedu ;
public class Demo01 {
public static void main ( String [ ] args) {
String str = "abc" ;
str. equals ( 1 ) ;
String numStr = "1" ;
String price = "9.9" ;
String r = numStr + 2 ;
System . out. println ( "++++++++++++++++++++" ) ;
K k = new K ( ) ;
double i = 1 ;
k. equals ( i) ;
}
}
class K {
public boolean equals ( Object obj) {
System . out. println ( obj. getClass ( ) ) ;
return true ;
}
}
2.自动装箱,拆箱
package com. qfedu ;
public class Demo02 {
public static void main ( String [ ] args) {
Integer i = new Integer ( 1 ) ;
Integer i2 = new Integer ( "1" ) ;
Integer i3 = 5 ;
int ii = i3. intValue ( ) ;
Double d1 = new Double ( 1.1 ) ;
Double d2 = 1.2 ;
double dou = d2;
}
}
3.String转化为基本类型
package com. qfedu ;
public class Demo03 {
public static void main ( String [ ] args) {
String num = "3" ;
byte b = Byte . parseByte ( num) ;
short s = Short . parseShort ( num) ;
int i = Integer . parseInt ( num) ;
long l = Long . parseLong ( num) ;
String price = "9.9" ;
float f = Float . parseFloat ( price) ;
double d = Double . parseDouble ( price) ;
String bo = "true" ;
boolean boo = Boolean . parseBoolean ( bo) ;
}
}