码迷,mamicode.com
首页 > 其他好文 > 详细

重写Object类中的toString方法

时间:2019-03-10 00:09:05      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:turn   ack   ext   注释   override   str   string   system   over   

package com.ftf.o;
/**
 * 重写祖宗类Object类中的toString方法
 * @author 房廷飞
 *
 */

public class TextObject {
    public static void main(String[] args) {
        //    Object obj;
            TextObject to=new TextObject();
            System.out.println(to.toString());
            Person2 p2=new Person2("房廷飞",20);
            System.out.println(p2.toString());
            
        }
    //重写Object类中的toString方法
    public String toString() {
        return "重写toString方法";
    }
}

class Person2{
    String name;
    int age;
    public Person2(String name,int age) {
        this.name=name;
        this.age=age;
    }
    @Override   //注释一下,方法是被重写的     //重写Object类中的toString方法
    public String toString() {
        return name+"年龄"+age;
        
    
    }
}

 

重写Object类中的toString方法

标签:turn   ack   ext   注释   override   str   string   system   over   

原文地址:https://www.cnblogs.com/fangtingfei/p/10503490.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!