System. Out. println (object)

Source: Internet
Author: User
 1 class Person { 2     private String name; 3     private int age; 4     public String getName() { 5         return this.name; 6     } 7      8     public void setName(String name) { 9         this.name = name;10     }11     12     public int getAge() {13         return this.age;14     }15     16     public void setAge(int age) {17         this.age = age;18     }19     20     public int getAge() {21         return this.age;22     }23     24     public String toString() {25         return "姓名: " + this.name + ", 年龄: " + this.age;26     }27 }28 29 public class InstanceDemo01 {30     public static void main(String args[]) {31         Class<?> c = null;32         try {33             c = Class.forName("Person");34         } catch(ClassNotFoundException e) {35             e.printStackTrace();36         }37         38         Person per = null;39         try {40             per = (Person)c.newInstance();    // 实例化Person对象41         } catch(Exception e) {42             e.printStackTrace();43         }44         45         per.setName("李兴华");46         per.setAge(30);47         System.out.println(per);            // 内容输出: 调用toString()48     }49 }

 

System.out.println(对象)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.