8 对象 && Object类

面向对象三大特征:

封装:外部不可见(使用private来封装属性或方法)

继承:扩展类的功能

多态:方法的重载(方法名不变,参数类型或参数个数不同);对象的多态性

注:封装性在java中有很多形式,private只是最基本的一种形式,也是一种比较常见的形式。

 

每个类中肯定都会有一个构造方法。

如果一个类中没有声明一个明确的构造方法则会自动生成一个无参的什么都不做的构造方法。

构造方法的主要目的是为类中的属性初始化。

匿名对象:没有名字,在java中只使用一次的对象。只在堆内存开辟一个实例化对象,没有引用关系。

 

类的设计分析:

 

 

 

toString()方法

1 class Demo{    // 定义Demo类,实际上就是继承了Object类
2 };
3 public class ObjectDemo01{
4     public static void main(String args[]){
5         Demo d = new Demo() ;    // 实例化Demo对象
6         System.out.println("不加toString()输出:"+d) ;
7         System.out.println("加上toString()输出:"+d.toString()) ;
8     }
9 };

从以上代码的运行结构可以发现,加与不加toString()方法完成的功能都是一样的,也就是证明了对象在打印的时候一定会调用toString()方法,是默认调用的。

 

equals()方法

equals()方法的主要功能是进行对象的比较操作

String本类也是Object类的子类,所以已经覆盖了此方法

 1 class Person{
 2     private String name ;    // 定义name属性
 3     private int age ;        // 定义age属性
 4     public Person(String name,int age){
 5         this.name = name ;
 6         this.age = age ;
 7     }
 8     public boolean equals(Object obj){
 9         if(this==obj){        // 地址相等
10             return true ;    // 肯定是同一个对象
11         }
12         if(!(obj instanceof Person)){    // 不是Person对象
13             return false ;
14         }
15         Person per = (Person) obj ;    // 向下转型
16         if(per.name.equals(this.name)&&per.age == this.age){
17             return true ;    // 依次比较内容
18         }else{
19             return false ;
20         }
21     }
22     public String toString(){
23         return "姓名:" + this.name + ";年龄:" + this.age ;
24     }
25 };
26 public class ObjectDemo03{
27     public static void main(String args[]){
28         Person per1 = new Person("李兴华",30) ;     // 实例化Person
29         Person per2 = new Person("李兴华",30) ;     // 实例化Person
30         System.out.println(per1.equals(per2)?"是同一个人!" : "不是同一个人!") ;
31         System.out.println(per1.equals("hello")?"是同一个人!" : "不是同一个人!") ;
32         
33     }
34 };
View Code

注:instanceof关键字用于判断一个引用类型变量所指向的对象是否是一个类(或接口、抽象类、父类)的实例。 

public interface IObject {
}

public class Foo implements IObject{
}

public class Test extends Foo{
}

public class MultiStateTest {
        public static void main(String args[]){
                test();
        }

        public static void test(){
                IObject f=new Test();
                if(f instanceof java.lang.Object)System.out.println("true");
                if(f instanceof Foo)System.out.println("true");
                if(f instanceof Test)System.out.println("true");
                if(f instanceof IObject)System.out.println("true");
        }
}
View Code

输出结果:

true
true
true
true

 

 

Object不光可以接受对象,还可以接收任意的引用数据类型。

Object接收接口实例:

 1 interface A{
 2     public String getInfo();    
 3 }
 4 class B implements A{
 5     public String getInfo(){    // 覆写方法
 6         return "Hello World!!!" ;
 7     }
 8 };
 9 public class ObjectDemo04{
10     public static void main(String args[]){
11         A a = new B() ;    // 向上转型,为接口实例化
12         Object obj = a ;    // 使用Object接收,向上转型
13         A x = (A)obj ;    // 向下转型
14         System.out.println(x.getInfo()) ;
15     }
16 };
View Code

Object接收数组实例:

 1 public class ObjectDemo05{
 2     public static void main(String args[]){
 3         int temp[] = {1,3,5,7,9} ;    // 定义数组
 4         Object obj = temp ;    // 使用Object接收数组
 5         print(obj) ;
 6     }
 7     public static void print(Object o){
 8         if(o instanceof int[]){        // 判断是否是整型数组
 9             int x[] = (int[])o ;
10             for(int i=0;i<x.length;i++){
11                 System.out.print(x[i] + "\t") ;
12             }
13         }
14     }
15 };
View Code

 

总结:对象在进行向下转型之前一定要先发生向上转型,要使用instanceof关键字判断

posted @ 2015-02-06 10:24  闲来垂钓  阅读(148)  评论(0)    收藏  举报