Java类的设计----Object 类

来源:互联网 发布:跆拳道功夫大师软件 编辑:程序博客网 时间:2024/06/15 01:09
Object类


Object类是所有Java类的根父类
如果在类的声明中未使用extends关键字指明其父类,则默认父类为Object类
  public class Person {
    ...
  }
等价于:
  public class Person extends Object {
.    ...
  }
例:
  method(Object obj){…}//可以接收任何类作为其参数
  Object o=new Person;
  method(o);

 

==操作符与equals方法

==操作符与equals方法的区别:
  ==:引用类型比较引用(是否指向同一个对象);
  Person p1=new Person(); Person p2=new Person();
  if (p1==p2){…}
  基本类型比较值;int a=5; if(a==6) {…}
用"=="进行比较时,符号两边的数据类型必须一致(可自动转换的基本数据类型除外),否则编译出错;

equals()方法是Object类的方法,由于所有类都继承Object类,也就继承了equals()方法。只能比较引用类型,其作用与“==”相同,比较是否指向同一个对象。格式:obj1.equals(obj2)

特例:当用equals()方法进行比较时,对类File、String、Date及封装类(Wrapper Class)来说,是比较类型及内容而不考虑引用的是否是同一个对象;
原因:在这些类中覆盖了equals()方法。

 

==操作符与equals方法举例

 

 1 class TestEquals {   2     public static void main(String[] args) { 3       MyDate m1 = new MyDate(14, 3, 1976); 4      MyDate m2 = new MyDate(14, 3, 1976); 5  6        if ( m1 == m2 ) { 7            System.out.println("m1==m2");  8         } else { 9           System.out.println("m1!=m2"); //m1 != m210         }11 12         if ( m1.equals(m2) ) {13           System.out.println("m1 is equal to m2"); // m1 is equal to m214         } else {15           System.out.println("m1 is not equal to m2");16         } 17     }18 }

 

 


 

 

 

Customer类

 1 public class Customer { 2      3     private String customerName; 4     private String email; 5      6     public Customer(String customerName, String email) { 7         this.customerName = customerName; 8         this.email = email; 9     }10     11     public String getCustomerName() {12         return customerName;13     }14     public void setCustomerName(String customerName) {15         this.customerName = customerName;16     }17     public String getEmail() {18         return email;19     }20     public void setEmail(String email) {21         this.email = email;22     }23     24     @Override25     public boolean equals(Object obj) {26         27         //1. 判定 obj 是否为 Customer 类型28         if(!(obj instanceof Customer)) {29             return false;30         }31         32         //2. 若为 Customer 类型, 则进行强制的类型转换33         Customer customer = (Customer) obj;34         35         //3. 比较其对应的属性是否相等. 比较字符串使用 equals 方法36         return this.customerName.equals(customer.getCustomerName())37                 && this.email.equals(customer.getEmail());38     }39 40     @Override41     public String toString() {42         return "Customer [customerName=" + customerName + ", email=" + email43                 + "]";44     }45 }

TestObject类

 1 public class TestObject { 2      3     public void method(Object object) { 4          5     } 6      7     public static void main(String[] args) { 8          9         TestObject to = new TestObject();10         11         to.method(new Person());12         to.method(new TestPerson()); 13         14         /**15          * Object 的 equals() 方法16          * 1. == 比较的是两个对象是否指向同一个对象. 即: 是否指向同一块內存空间. 17          *       且要求 == 兩边的类型必须一致或存在着父子关系, 否则编译出错.18          *       19          * 2. 需求: 有时候, 当两个对象的对应属性都一致时, 需要判定其相等! 使用 == 无法完成, 但可以借助于 equals() 方法20          * 21          * 3. equals()方法是Object类的方法, 由于所有类都继承Object类, 也就继承了equals()方法。22          * 23          * 4. equals() 只能比较引用类型,其作用与“==”相同,比较是否指向同一个对象. 可以比较任意兩个对象. 24          * 25          * 5. 可以在类中重写 equals 方法, 以达到定制比较相等的目的. 26          * 27          * 6. 对于 封装类, String, Date, File 等已经重写了其 equals 方法, 可以通过 equals 方法來判定其內容是否相同.28          *    具体参考其 API29          *    30          * 7. 比较两个字符串的內容是否相同, 一定要使用 equals() 方法, 而不能使用 ==    31          */32         Customer cust1 = new Customer("Tom", "tom@atguigu.com");33         Customer cust2 = new Customer("Tom", "tom@atguigu.com");34         35         //== 比较的是两个对象是否指向同一个对象. 即: 是否指向同一块內存空间. 36         System.out.println(cust1 == cust2); //false37         38         System.out.println("cust1.equals(cust2): " + cust1.equals(cust2)); 39         40         Man man = new Man();41         42         //要求 == 两边的类型必须一致, 否则编译出错. 43 //        System.out.println(cust1 == man); 44         System.out.println(cust1.equals(man));  45         46         Person p = new Person();47         System.out.println(p == man); 48         49         String str1 = new String("abcd");50         String str2 = new String("abcd");51         52         System.out.println(str1.equals(str2));  53     }54 }

 


 

 

MyDate类

 1 public class MyDate { 2  3     private int year; 4     private int month; 5     private int day; 6      7     public MyDate(int day, int month,int year) { 8         super(); 9         this.year = year;10         this.month = month;11         this.day = day;12     }13     14     public int getYear() {15         return year;16     }17     public void setYear(int year) {18         this.year = year;19     }20     public int getMonth() {21         return month;22     }23     public void setMonth(int month) {24         this.month = month;25     }26     public int getDay() {27         return day;28     }29     public void setDay(int day) {30         this.day = day;31     }32     33     @Override34     public boolean equals(Object obj) {35         36         if(!(obj instanceof MyDate)){37             return false;38         }39         40         MyDate md = (MyDate) obj;41         42         return this.year == md.year 43                 && this.month == md.month 44                 && this.day == md.day;45     }46 }

TestEquals类

 1 public class TestEquals { 2      3     public static void main(String[] args) { 4        MyDate m1 = new MyDate(14, 3, 1976); 5        MyDate m2 = new MyDate(14, 3, 1976); 6  7        if ( m1 == m2 ) { 8             System.out.println("m1==m2");  9        } else {10            System.out.println("m1!=m2"); //m1 != m211        }12 13            if ( m1.equals(m2) ) {14                System.out.println("m1 is equal to m2"); // m1 is equal to m215         } else {16             System.out.println("m1 is not equal to m2");17         } 19     }20 }

 

toString 方法

toString()方法在Object类中定义,其返回值是String类型,返回类名和它的引用地址。


在进行String与其它类型数据的连接操作时,自动调用toString()方法
  Date now=new Date();
  System.out.println(“now=”+now);
  相当于 System.out.println(“now=”+now.toString());//now=Date@122345


可以根据需要在用户自定义类型中重写toString()方法
如String 类重写了toString()方法,返回字符串的值。
s1=“hello”;
System.out.println(s1);//相当于System.out.println(s1.toString());
在ToString1.java中的类A里覆盖toString方法,使其输出类A对象的cint属性值。

基本类型数据转换为String类型时,调用了对应封装类的 toString()方法int a=10; System.out.println(“a=”+a);

 


 

 

 1 public class TestToString { 3     public static void main(String[] args) { 5         Customer cust = new Customer("TongGang", "TongGang@atguigu.com"); 7         /** 8          * toString():  9          * 1. Object 类定义的方法, 所以任何对象都可以来调用 toString() 方法10          * 2. 默认情况下, toString() 方法 全类名@hash码11          * 3. 可以根据需要重写 toString() 方法, 通常用于测试. 个别时候用于显示. 12          * 4. JDK 中的很多类都重写了 toString() 方法13          * 14          */15         System.out.println(cust); 16         17         String str2 = new String("atguigu");18         System.out.println(str2.toString()); 19         20         Date date = new Date();21         System.out.println(date.toString()); 22     }23 }

 

原创粉丝点击