本文介绍了什么是快捷方式自动生成toString方法在Eclipse?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一些简单的类是好还是坏的做法自动生成的toString 的方法呢?

我想产生类似下面的地方取变量名,并产生打印的名字后面是其值的toString 方法。

 私人字符串名称;
私人诠释年龄;
私人双高;公共字符串的toString(){
   返回的String.format(姓名:%s的年龄:%d个身高%F,姓名,年龄,身高);
}


解决方案

Eclipse的3.5.2(甚至更早的版本)已经提供了这一功能。如果您在编辑器中单击鼠标右键,你会发现它的源下 - >生成的toString()...

要回答它是否是一个不好的做法,自动生成的toString(),我的看法是,这是不是你的问题。如果生成的code是非常相似的code,你会写自己,那么为什么还要打字吧?

Is it good or bad practice auto-generating toString methods for some simple classes?

I was thinking of generating something like below where it takes the variable names and produces a toString method that prints the name followed by its value.

private String name;
private int age;
private double height;

public String toString(){
   return String.format("Name: %s Age: %d Height %f", name, age, height);
}
解决方案

Eclipse 3.5.2 (and possibly earlier versions) already provides this feature. If you right-click within the editor, you'll find it under Source -> Generate toString()...

To answer your question about whether it's a bad practice to autogenerate toString(), my opinion is that it is not. If the generated code is very similar to the code you would have written yourself, then why bother typing it out?

这篇关于什么是快捷方式自动生成toString方法在Eclipse?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 00:54