瘋狂JAVA講義P167【6.2處理對象 6.2.1列印對象和toString方法】渣渣筆記Ctrl+C+V

來源:互聯網
上載者:User

標籤:weight   gtest   java   自我   src   6.2   變數   stat   test   

例子2:
1 public class Orc 2 { 3 public static class A 4 { 5 public String toString() 6 { 7 return "this is A"; 8 } 9 }10 public static void main(String[] args)11 {12 A obj = new A();13 System.out.println(obj);14 }15 }   //輸出結果!!

例子2: 

 1 public class Orc 2 { 3        public static class A 4        { 5               public String getString() 6               { 7                      return "this is A"; 8               } 9        }10        public static void main(String[] args)11        {12               A obj = new A();13               System.out.println(obj);14               System.out.println(obj.getString());15        }16 }

 看出區別了嗎,toString的好處是在碰到“println”之類的輸出方法時會自動調用,不用顯式打出來。

 

 

 

 

 

一 概念簡介

1、列印對象和toString方法:toString方法是系統將會輸出該對象的“自我描述”資訊,用以告訴外界對象具有的狀態資訊。

2、Object 類提供的toString方法總是返回該對象實作類別的類名 + @ +hashCode值。

二 列印對象樣本

1、程式樣本

 1 class Person 2 { 3     private String name; 4     public Person(String name) 5     { 6         this.name = name; 7     } 8 } 9 public class PrintObject10 {11     public static void main(String[] args)12     {13         // 建立一個Person對象,將之賦給p變數14         Person p = new Person("林沖");15         // 列印p所引用的Person對象16         System.out.println(p);17     }18 }

2、運行結果

[email protected]

3、結果分析

當使用該方法輸出Person對象時,實際輸出的是Person對象的toString方法。

三 重寫toString方法樣本

1、程式樣本

 1 class Apple 2 { 3     private String color; 4     private double weight; 5     public Apple(){    } 6     //提供有參數的構造器 7     public Apple(String color , double weight) 8     { 9         this.color = color;10         this.weight = weight;11     }12 13     // color的setter和getter方法14     public void setColor(String color)15     {16         this.color = color;17     }18     public String getColor()19     {20         return this.color;21     }22 23     // weight的setter和getter方法24     public void setWeight(double weight)25     {26         this.weight = weight;27     }28     public double getWeight()29     {30         return this.weight;31     }33     // 重寫toString方法,用於實現Apple對象的"自我描述"34     public String toString()35     {36         return "一個蘋果,顏色是:" + color37             + ",重量是:" + weight;38     }39 40 //    public String toString()41 //    {42 //        return "Apple[color=" + color + ",weight=" + weight + "]";43 //    }44 45 }46 public class ToStringTest47 {48     public static void main(String[] args)49     {50         Apple a = new Apple("紅色" , 2.38);51         // 列印Apple對象52         System.out.println(a);53     }54 }

2、運行結果

一個蘋果,顏色是:紅色,重量是:2.38

3、結果分析

從上面的運行結果來看,通過重寫Apple類的toString方法,就可以讓系統在列印Apple對象時列印出該對象的“自我描述”資訊。

瘋狂JAVA講義P167【6.2處理對象 6.2.1列印對象和toString方法】渣渣筆記Ctrl+C+V

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.