18. november 2003 - 21:13Der er
11 kommentarer og 1 løsning
Why private and get and Set?
Hello!
Every body has read that you should incapsulate your mambers as god as you can if possible. Instead of public members you should declare them private and create get and set methods. But if you have this class:
public class A { private String a;
public String getA() { return a }
public void setA(String a) { this.a = a; } }
//and
public class B { public String b; }
I would say that for safety and memory reson this would be equal The memory would be the same (If instans of class A would be garbage collected we still have refereces to object a):
A classA; B classB; //... classB.b = "Hello"; String j = classB.b; //or classA.setA("Hello"); String i = classA.getA();
Please correct me if I am wrong?
If you would have something like this it would be better: public class A { private String a;
public String getA() { return a.clone(); }
public void setA(String a) { this.a = a.clone(); } }
Now if the instans of A would be gb the object a also would go.
So please correct me if I am wrong when I say that the first code of A wouldn't be any better then the code of B. Or is there any other performance or safety winning of coding like A?
Best regards Fredrik
PS Please give ansvers so you could get part of the points
I et inbound callcenter, hvor identiteten på den, der ringer ind, skal bekræftes, kan kontrollen nu foregå i telefonkøen. Det understøtter fem centrale KPI'er for callcentre.
nedenfor behøver vi ikke at ane om klassen A indeholder fødeår eller Alder eller noget helt tredie. vi får blot den værdi vi beder om med den valgte get___ metode
public class A { private int foedeAar; // fødeår ændres ikke selvom vi har den person i vor database i flere år.
public int getAlder() { return datoNow() - foedeAar; // } public int getFoedeAar() { return FoedeAar }
public void setAlder(int a) { if ( alder >= 0 && alder < 125 ) { this.foedeAar = datoNow() -a; } else { System.out.println( "fejl. forsøg på at sætte en lovlig usandsynlig alder." ); } } // vi kunne også lave en setFoedeAar metode til at sætte vor variabel (med en tilsvarende test) }
So the conclusion is that using private members and get and set methods would give us: better flexibilty like data checks no overrids by subclasses
But there is no guarantee that the memory or processing would be better.
Do you agree ?
I still wonder about the safety-reson, is there any safty-reason that I should use private members and get and set instead of public members? I am pretty sure that I have read that safety should be an issue for this.
Best regards Fredrik But please give answers so I could give you the points that you guys really deserve!
Perhaps you could give a small note or example what you mean by "It is a software programming issue" when you say safety reasons. It sounds really interesting!
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.