在 Eclipse 中自动生成 toString 方法的快捷方式是什么? [英] What are the shortcut to Auto-generating toString Method in Eclipse?

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

问题描述

为一些简单的类自动生成 toString 方法是好是坏?

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

我正在考虑生成类似下面的内容,它采用变量名称并生成一个 toString 方法,该方法打印名称后跟其值.

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(可能还有更早的版本)已经提供了这个特性.如果您在编辑器中右键单击,您会在 Source -> Generate toString()... 下找到它

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()...

要回答关于自动生成 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?

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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆