/ Forside / Teknologi / Udvikling / Java / Nyhedsindlæg
Login
Glemt dit kodeord?
Brugernavn

Kodeord


Reklame
Top 10 brugere
Java
#NavnPoint
molokyle 3688
Klaudi 855
strarup 740
Forvirret 660
gøgeungen 500
Teil 373
Stouenberg 360
vnc 360
pmbruun 341
10  mccracken 320
references
Fra : daxi jumbo


Dato : 18-05-02 22:23

Line 20: is there someway I can prevent this from happening?

In C++, the soulution would be to return a const reference to v. Is this
possible in Java?

001 import java.util.Vector;
002
003 class C {
004 private Vector v;
005
006 public C() { v = new Vector(); }
007 public Vector getV() { return v; }
008 public void print() { System.out.println("v = " + v); }
009 public void add(int element) {
010 if(element > 0)
011 v.add(new Integer(element));
012 }
013 }
014
015 class CTest {
016 public static void main(String args[]) {
017 C c = new C();
018
019 c.print();
020 c.getV().add(new Integer(-1)); //this should not be allowed
021 c.add(new Integer(-2)); //this is should be
022 //the only way to alter v
023 c.print();
024 }
023 }

thank you.



 
 
Martin Moller Peders~ (18-05-2002)
Kommentar
Fra : Martin Moller Peders~


Dato : 18-05-02 22:32

In <tCzF8.2151$fG3.75490@news2.ulv.nextra.no> "daxi jumbo" <daxi@jumbo.no> writes:

>Line 20: is there someway I can prevent this from happening?

>In C++, the soulution would be to return a const reference to v. Is this
>possible in Java?

>001 import java.util.Vector;
>002
>003 class C {
>004 private Vector v;
>005
>006 public C() { v = new Vector(); }
>007 public Vector getV() { return v; }
>008 public void print() { System.out.println("v = " + v); }
>009 public void add(int element) {
>010 if(element > 0)
>011 v.add(new Integer(element));
>012 }
>013 }
>014
>015 class CTest {
>016 public static void main(String args[]) {
>017 C c = new C();
>018
>019 c.print();
>020 c.getV().add(new Integer(-1)); //this should not be allowed
>021 c.add(new Integer(-2)); //this is should be
>022 //the only way to alter v
>023 c.print();
>024 }
>023 }

>thank you.

You could change the getV method, so it returns a copy of the private vector.

public Vector getV() { return v.clone() };

/Martin



daxi jumbo (18-05-2002)
Kommentar
Fra : daxi jumbo


Dato : 18-05-02 22:38

Det var ikke et dumt forslag nei!
Er det en tidkrevende / minnekrevende prosess å "clone" et objekt?

"Martin Moller Pedersen" <tusk@daimi.au.dk> wrote in message
news:ac6h7j$m3m$1@news.net.uni-c.dk...
> In <tCzF8.2151$fG3.75490@news2.ulv.nextra.no> "daxi jumbo" <daxi@jumbo.no>
writes:
>
> >Line 20: is there someway I can prevent this from happening?
>
> >In C++, the soulution would be to return a const reference to v. Is this
> >possible in Java?
>
> >001 import java.util.Vector;
> >002
> >003 class C {
> >004 private Vector v;
> >005
> >006 public C() { v = new Vector(); }
> >007 public Vector getV() { return v; }
> >008 public void print() { System.out.println("v = " + v); }
> >009 public void add(int element) {
> >010 if(element > 0)
> >011 v.add(new Integer(element));
> >012 }
> >013 }
> >014
> >015 class CTest {
> >016 public static void main(String args[]) {
> >017 C c = new C();
> >018
> >019 c.print();
> >020 c.getV().add(new Integer(-1)); //this should not be allowed
> >021 c.add(new Integer(-2)); //this is should be
> >022 //the only way to alter v
> >023 c.print();
> >024 }
> >023 }
>
> >thank you.
>
> You could change the getV method, so it returns a copy of the private
vector.
>
> public Vector getV() { return v.clone() };
>
> /Martin
>
>



Martin Moller Peders~ (18-05-2002)
Kommentar
Fra : Martin Moller Peders~


Dato : 18-05-02 22:56

In <yQzF8.2158$fG3.75750@news2.ulv.nextra.no> "daxi jumbo" <daxi@jumbo.no> writes:

>Det var ikke et dumt forslag nei!
>Er det en tidkrevende / minnekrevende prosess å "clone" et objekt?


Ja, det tager et stykke tid. Men hvorfor har du brug for en
getV metode ?

Mvh
Martin


>"Martin Moller Pedersen" <tusk@daimi.au.dk> wrote in message
>news:ac6h7j$m3m$1@news.net.uni-c.dk...
>> In <tCzF8.2151$fG3.75490@news2.ulv.nextra.no> "daxi jumbo" <daxi@jumbo.no>
>writes:
>>
>> >Line 20: is there someway I can prevent this from happening?
>>
>> >In C++, the soulution would be to return a const reference to v. Is this
>> >possible in Java?
>>
>> >001 import java.util.Vector;
>> >002
>> >003 class C {
>> >004 private Vector v;
>> >005
>> >006 public C() { v = new Vector(); }
>> >007 public Vector getV() { return v; }
>> >008 public void print() { System.out.println("v = " + v); }
>> >009 public void add(int element) {
>> >010 if(element > 0)
>> >011 v.add(new Integer(element));
>> >012 }
>> >013 }
>> >014
>> >015 class CTest {
>> >016 public static void main(String args[]) {
>> >017 C c = new C();
>> >018
>> >019 c.print();
>> >020 c.getV().add(new Integer(-1)); //this should not be allowed
>> >021 c.add(new Integer(-2)); //this is should be
>> >022 //the only way to alter v
>> >023 c.print();
>> >024 }
>> >023 }
>>
>> >thank you.
>>
>> You could change the getV method, so it returns a copy of the private
>vector.
>>
>> public Vector getV() { return v.clone() };
>>
>> /Martin
>>
>>



daxi jumbo (18-05-2002)
Kommentar
Fra : daxi jumbo


Dato : 18-05-02 23:07

Det dreier seg egentlig om en prinsippsak...

En faglærer jeg har mener at C++ ikke er "fullverdig" objektorientert fordi
det har pekere, og med pekere kan du endre en klasses private variable uten
å bruke metoder i klassen der variabelen ligger:

C++ kode:
class C {
public:
C() { x = 3; }
int * getX() { return &x; }
private:
int x;
};

void main() {
C c;
int *ptr;
ptr = c.getX();
*ptr = 5; //setter privat variabel uten å benytte set metode
i class C
//eller tilsvarende:
*c.getX() = 5;
}

Deretter argumenterte jeg for at problemet ganske enkelt kan løses med å
skrive om getX slik:
const int * getX() { return &x; }

sett at jeg har en klasse B som jeg ønsker å la være Cloneable, hvordan gjør
jeg det?

class B implements Cloneable {
private int x;
public B(int x) { this.x = x; }

protected Object clone() {
//???
try {
//???
}
catch(CloneNotSupportedException cnsex) {

}
//??
}
}

"Martin Moller Pedersen" <tusk@daimi.au.dk> wrote in message
news:ac6il0$ibo$1@news.net.uni-c.dk...
> In <yQzF8.2158$fG3.75750@news2.ulv.nextra.no> "daxi jumbo" <daxi@jumbo.no>
writes:
>
> >Det var ikke et dumt forslag nei!
> >Er det en tidkrevende / minnekrevende prosess å "clone" et objekt?
>
>
> Ja, det tager et stykke tid. Men hvorfor har du brug for en
> getV metode ?
>
> Mvh
> Martin
>
>
> >"Martin Moller Pedersen" <tusk@daimi.au.dk> wrote in message
> >news:ac6h7j$m3m$1@news.net.uni-c.dk...
> >> In <tCzF8.2151$fG3.75490@news2.ulv.nextra.no> "daxi jumbo"
<daxi@jumbo.no>
> >writes:
> >>
> >> >Line 20: is there someway I can prevent this from happening?
> >>
> >> >In C++, the soulution would be to return a const reference to v. Is
this
> >> >possible in Java?
> >>
> >> >001 import java.util.Vector;
> >> >002
> >> >003 class C {
> >> >004 private Vector v;
> >> >005
> >> >006 public C() { v = new Vector(); }
> >> >007 public Vector getV() { return v; }
> >> >008 public void print() { System.out.println("v = " + v); }
> >> >009 public void add(int element) {
> >> >010 if(element > 0)
> >> >011 v.add(new Integer(element));
> >> >012 }
> >> >013 }
> >> >014
> >> >015 class CTest {
> >> >016 public static void main(String args[]) {
> >> >017 C c = new C();
> >> >018
> >> >019 c.print();
> >> >020 c.getV().add(new Integer(-1)); //this should not be
allowed
> >> >021 c.add(new Integer(-2)); //this is should be
> >> >022 //the only way to alter v
> >> >023 c.print();
> >> >024 }
> >> >023 }
> >>
> >> >thank you.
> >>
> >> You could change the getV method, so it returns a copy of the private
> >vector.
> >>
> >> public Vector getV() { return v.clone() };
> >>
> >> /Martin
> >>
> >>
>
>



Søg
Reklame
Statistik
Spørgsmål : 177558
Tips : 31968
Nyheder : 719565
Indlæg : 6408926
Brugere : 218888

Månedens bedste
Årets bedste
Sidste års bedste