public class Persona {
int Codigo;
String Nombre;
public Persona(int Codigo, String Nombre){
this.Codigo = Codigo;
this.Nombre = Nombre;
}
public void setCodigo(int Codigo){
this.Codigo = Codigo;
}
public int getCodigo(){
return this.Codigo;
}
public void setNombre(String Nombre){
this.Nombre = Nombre;
}
public String getNombre(){
return this.Nombre;
}
}
Or开发者_StackOverflow社区 is there a much shorter (realiable) way to do it?
It depends what you mean by "do it". Getters and setters are better than public fields - but do you really need them in the first place?
- Do you need to expose this information at all, or just work with it to accomplish other operations?
- If you do need to expose it, do you definitely need your type to be mutable with setters? Immutable types tend to be easier to reason about, deal with threading etc.
Just to "correct" your current design, assuming you do want getters and setters, I would change it to:
public final class Persona {
private int codigo;
private String nombre;
public Persona(int codigo, String nombre) {
this.codigo = codigo;
this.nombre = nombre;
}
public void setCodigo(int codigo) {
this.codigo = codigo;
}
public int getCodigo() {
return codigo;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getNombre() {
return nombre;
}
}
The main changes are in terms of capitalization of fields, and making the fields private. Personally I also don't use "this." where I don't have to. I also have a space between () and {. Those are more personal preferences than anything else.
Finally, I've made the class final
- I'm a believer in "design for inheritance or prohibit it" - if you do need to derive from this class, consider what kind of specialization you want to support.
You're doing it right. Unfortunately, Java doesn't have a more compact way of defining getters and setters like a lot of other languages do.
One thing to think about -- if you only plan on setting those values on object construction, there's no need to have the setters. And also, if you never plan on getting the values directly, don't define the getters. Just define them when you need them.
A thought-provoking (if a bit extreme) article on this is available here: Why getter and setter methods are evil.
This is the correct what to do it in Java (other than that variables usually start with a lowercase). You could also use groovy which automatically generates the getters/setters
class Persona {
int Codigo
String Nombre
}
This would effectively be the same
As others have mentioned though, if you don't need setters, you should avoid them and make your variables final
. Immutable objects are less likely to be a source of bugs later on.
This is correct for Java. Note that since you are passing both values into your constructor, you can remove the setters, mark the fields final, and this class would then be immutable.
That looks like fine setters/getters to me. However, if you pass both values into the constructor, are you sure both the setters/getters are needed? In this case, it looks like only the getters would be sufficient. Also, if you decide to stick with having Codigo
and Nombre
passed in to the constructor, and remove the setter classes, you should make them final
, i.e. immutable.
Another point is to not capitalize Codigo
and Nombre
, codigo
and nombre
is the recommended form. Camel-case for methods and fields, capitalize only classes, interfaces and constants.
Yes, it does look fine. Although you shouldn't capitalize you variable names.
You might want to make the member variables Nombre and Cordigo "private". Since, that's the whole point of using getters/setters.
It's more important that you understand why you're using getters/setters.
One benefit is that you can add additional checks (like limits if it's a number) inside these methods. Also, they are easier to maintain if your implementation changes in the future.
Your variables are not private
, they are default. So they are public to classes in the same package
. You probably want to make them private
. Also setter is not always needed, although getter
is almost needed always.
The fields should be private. The reason is Encapsulation. You want to hide the implementation details from any other Classes that user your Persona class. You might one day want to enforce a min and max on codigo
or make sure it is in fixed set of values, you could do that in the setCodigo()
method without any client code knowing about it. With just public fields you could not intercept the mutation of the field and affect it.
public final class Persona
{
private int codigo;
private String nombre;
public Persona(final int codigo, final String nombre)
{
this.codigo = codigo;
this.nombre = nombre;
}
public void setCodigo(final int codigo)
{
this.codigo = codigo;
}
public int getCodigo()
{
return this.codigo;
}
public void setNombre(final String nombre)
{
this.nombre = nombre;
}
public String getNombre()
{
return this.nombre;
}
}
and I fixed your braces alignment for you as well :-)
Using this.
for all instance variables everywhere is good coding practice as well. Explicit is better than implicit.
Like Jon said, you need to have a strong reason to need getter/setter. Consider an immutable class as an alternative of your case.
public final class Persona {
private final int codigo;
private final String nombre;
public Persona(int codigo, String nombre) {
this.codigo = codigo;
this.nombre = nombre;
}
public int getCodigo() {
return codigo;
}
public String getNombre() {
return nombre;
}
}
精彩评论