重写 ToString方法

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/shengyao15/article/details/84524078
打印对象时能看到里面的属性值
tristan.User@b1c5fa[name=aa,age=bb,phone=cc]


package tristan;

import org.apache.commons.lang.builder.ToStringBuilder;

public class TestToStringBuilder {
public static void main(String[] args) {
User u = new User();
u.name="aa";
u.age="bb";
u.phone="cc";

System.out.println(u);
}
}

class User{
public String name;
public String age;
public String phone;

public String toString(){
return ToStringBuilder.reflectionToString(this);
}
}
展开阅读全文

重写Tostring()方法,get,set有关的问题。

05-05

using System;rnusing System.Collections.Generic;rnusing System.Linq;rnusing System.Text;rnrnnamespace QueryComplexObjectrnrn class Customerrn rn public string ID get; set; rn public string City get; set; rn public string Country get; set; rn public string Region get; set; rn public decimal Sales get; set; rn public override string ToString()rn rn return "ID:" + ID+" " + "City:" + City+" " + "Country:" + Country+" " + "Region:" + Region +" "+ "Sales:" + Sales;rn rn rnrnrnrn class Programrn rn static void Main(string[] args)rn rn List customers = new List rn rn new Customer ID = "A", City = "New YorK", Country = "USA", Region = "North USA", Sales = 9999,rn new Customer ID = "B", City = "MMubLik", Country = "India", Region = "Asia",Sales= 8888,rn new Customer ID = "C", City = "sdfasf", Country = "UK", Region = "PPa",Sales= 7777,rn new Customer ID = "D", City = "Mfdg", Country = "Dekl", Region = "Qcia",Sales= 6666,rn new Customer ID = "E", City = "MhjO", Country = "Dkka", Region = "Aivr",Sales= 5555,rn new Customer ID = "F", City = "ghjONO", Country = "Pool", Region = "Sdia",Sales= 4444,rn new Customer ID = "G", City = "cvNO", Country = "Ekkiv", Region = "Parti",Sales= 3333,rn new Customer ID = "H", City = "Palu", Country = "CllKu", Region = "Lric",Sales= 2222,rn new Customer ID = "I", City = "Mosco", Country = "Drro", Region = "Ytile",Sales= 1111,rn new Customer ID = "G", City = "Seml", Country = "Swrit", Region = "Hikru",Sales= 1000,rn new Customer ID = "K", City = "Mdsf", Country = "Adfin", Region = "Sxia",Sales= 2000,rn new Customer ID = "L", City = "istan", Country = "Cink", Region = "Nikal",Sales= 3000,rn new Customer ID = "M", City = "Shanghai", Country = "Asdsd", Region = "Lusia",Sales= 4000,rn new Customer ID = "N", City = "Wuhai", Country = "Gfgf", Region = "Srima",Sales= 5000,rn new Customer ID = "O", City = "Dalian", Country = "Efhj", Region = "Vgen",Sales= 6000,rn new Customer ID = "P", City = "Mexige", Country = "Bddfg", Region = "Waga",Sales= 7000,rn new Customer ID = "Q", City = "LLucy", Country = "Ehjgh", Region = "Ericsi",Sales= 8000,rn new Customer ID = "R", City = "London", Country = "Ffghfg", Region = "Fulli",Sales=9000,rn new Customer ID = "S", City = "Bejing", Country = "Cdsfdf", Region = "Jicci",Sales= 1001,rn new Customer ID = "T", City = "Limas", Country = "IOll", Region = "Malli",Sales= 2000,rnrn ;rn var queryResult = from c in customers where c.Region == "Asia" select c;rn Console.WriteLine("Customers in Asic");rn foreach (Customer cs in queryResult)rn rn Console.WriteLine(cs);rn rn Console.WriteLine("press anykey to go out");rn Console.ReadKey();rnrn rn rnrn首先是那个重写的方法ToString(),我怎么就没见这个方法被显示调用啊?但是知道去掉肯定不行,否则Console.writeLine(cs)就输出全变成了rn“ 命名空间.Customer”这种类型了(测试的),输出函数Console.writeLine是怎么调用ToString()的?另外就是定义的那几个属性,例如rnpublic string ID get; set; ,我把它们变成 成员变量public string ID ,运行程序也是一样的结果。我想问的是在这里使用属性有什么好处。rn谁能深入显出的文字表述下属性那?问的问题有点多,多加点分,希望好心人平和的回答。rn 论坛

没有更多推荐了,返回首页