Testing of override of toString()

本文介绍了如何在 Java 中为 POJO 类覆盖 toString 方法来方便地打印对象属性。通过使用 Apache Commons Lang 库中的 ToStringBuilder,可以简洁地实现这一功能,并展示了两种不同的实现方式及其输出效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

At fisrt,thanks for the tips of Xia,I just  run the situation he described.

        When we want to print the properties of POJO, we can override the function toString() for conveninence , here is my testing code:

        Created a object Student:

       

package mysrc;
import java.io.Serializable;

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

public class Student implements Serializable{

	int id;
	String name;
	//construct
	Student (){		
	}
	
	Student(int id,String name){
		this.id = id;
		this.name = name;
	}
	
	//override
	public  String toString(){		
		return ToStringBuilder.reflectionToString(this);
	}
	
	//the different way to implement this function
	public String toStr(){
		return new ToStringBuilder(this).append("id",id).append("name",name).toString();
	}

}

    Then test these two methods:

   

package mysrc;

public class ToStringTest {	
	public static void main(String args[]){		
		Student stu = new Student(10086,"tanglei");		
		System.out.println(stu.toString());
		System.out.println(stu.toStr());
	}
}

   Then the result is :

          mysrc.Student@a62fc3[id=10086,name=tanglei]
          mysrc.Student@a62fc3[id=10086,name=tanglei]

   Ok,that's all

在Java中,我们可以定义一个名为BookShop的类,它包含所需的属性如书名(title), 出版社(publisher), 单价(price), 出版年份(yearOfPublication) 和作者(author),以及对应的getter和setter方法、equals()方法、toString()方法,并提供构造函数。以下是这个类的一个示例: ```java public class BookShop { private String title; private String publisher; private double price; private int yearOfPublication; private String author; // 默认无参构造方法 public BookShop() { this.title = ""; this.publisher = ""; this.price = 0.0; this.yearOfPublication = 0; this.author = ""; } // 带参数的构造方法 public BookShop(String title, String publisher, double price, int yearOfPublication, String author) { this.title = title; this.publisher = publisher; this.price = price; this.yearOfPublication = yearOfPublication; this.author = author; } // Getter and Setter methods public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getPublisher() { return publisher; } public void setPublisher(String publisher) { this.publisher = publisher; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getYearOfPublication() { return yearOfPublication; } public void setYearOfPublication(int yearOfPublication) { this.yearOfPublication = yearOfPublication; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } // Override equals method for comparing bookshops @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null || getClass() != obj.getClass()) return false; BookShop bookShop = (BookShop) obj; return Double.compare(bookShop.getPrice(), getPrice()) == 0 && bookShop.getYearOfPublication() == getYearOfPublication() && Objects.equals(bookShop.getTitle(), getTitle()) && Objects.equals(bookShop.getPublisher(), getPublisher()) && Objects.equals(bookShop.getAuthor(), getAuthor()); } // toString method to display book shop information @Override public String toString() { return "BookShop{" + "title='" + title + '\'' + ", publisher='" + publisher + '\'' + ", price=" + price + ", yearOfPublication=" + yearOfPublication + ", author='" + author + '\'' + '}'; } // Main method for testing public static void main(String[] args) { BookShop book1 = new BookShop("Java Programming", "O'Reilly", 49.99, 2022, "John Doe"); System.out.println(book1); BookShop book2 = new BookShop("Python Basics", "Packt Publishing", 39.99, 2021, "Jane Smith"); System.out.println("Are book1 and book2 equal? " + book1.equals(book2)); } } ``` 在这个例子中,我们创建了一个简单的BookShop类用于演示,主方法`main()`用于测试类的行为。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值