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DYDODQJ
œ 6\VWHPqXQDFODVVHGLVLVWHPDFKHUDSSUHVHQWDLO
VLVWHPDVRWWRVWDQWHTXDOXQTXHHVVRVLD
œ RXWqXQRJJHWWRVWDWLFGHOODFODVVH6\VWHPH
UDSSUHVHQWDLOGLVSRVLWLYRVWDQGDUGGLXVFLWD
œ VXOO
RJJHWWRRXWqSRVVLELOHLQYRFDUHLOPHWRGRSULQWOQ
INTRODUZIONE ALLA OOP IN JAVA -- 4
8135,02(6(03,2+HOOR:RUOG,,
/** File Esempio0.java
* Applicazione Java da linea di comando
* che stampa la classica frase di benvenuto
*/
public class Esempio0 {
public static void main(String args[]){
System.out.println("Hello World!");
}
8135,02(6(03,2+HOOR:RUOG,,,
/** File Esempio0.java
* Applicazione Java da linea di comando
* che stampa la classica frase di benvenuto
*/
public class Esempio0 {
public static void main(String args[]){
System.out.println("Hello World!");
}
}
}
œ 3HUFRPSLODUH
MDYDF(VHPSLRMDYD
SURGXFH(VHPSLRFODVV
œ 3HUHVHJXLUH
MDYD(VHPSLR
-DYDFRQVHQWHGLJHQHUDUHDXWRPDWLFDPHQWHOD
GRFXPHQWD]LRQHGHOOHFODVVLFRQWHQXWHQHOILOHGD
FRPSLODUHOLPLWDWDPHQWHDTXHOOHQRQSULYDWH
MDYDGRF(VHPSLRMDYD
,OULVXOWDWRqXQLQVLHPHGLILOH+70/
,OIRUPDWRFRPSLODWRFODVVE\WHFRGH-DYDqSRUWDELOHH
LQGLSHQGHQWHGDOODSLDWWDIRUPD
“XQDFODVVHFRPSLODWDVXXQVLVWHPDIXQ]LRQHUjVXTXD
OXQTXHDOWURVLVWHPD0DF8QL[:LQGRZV/LQX[$OSKD«
SHUFXLVLDGLVSRQLELOHLO-DYD5XQWLPH(QYLURQPHQWQHOOD
VWHVVDYHUVLRQH
'LVWLQ]LRQHIUD-DYD'HYHORSPHQW.LW-'.±YHURHSURSULR
DPELHQWHGLVYLOXSSRHSURJUDPPD]LRQHH-DYD5XQWLPH
(QYLURQPHQW-5(±VRORVXSSRUWRDWHPSRGLHVHFX]LRQH
INTRODUZIONE ALLA OOP IN JAVA -- 5
INTRODUZIONE ALLA OOP IN JAVA -- 6
(6(03,26WDPSDGHJOL$UJRPHQWL
(6(03,2&UHD]LRQHH8VRGL2JJHWWL&RXQWHU
/** File Esempio1.java
* applicazione da linea di comando
* stampa gli argomenti passati al programma
*/
/** File Esempio2.java
* Prima applicazione che istanzia nuovi oggetti
*/
class Counter {
private int
public void
public void
public void
public int
}
public
class Esempio1 {
public static void main(String args[]){
if (args.length == 0)
System.out.println("Occorrono argomenti");
else
for(int i=0; i<args.length; i++)
System.out.println("arg" + (i+1) +
": " + args[i]);
}
}
œ 6WULQJqXQDFODVVHSUHGHILQLWDOHVWULQJKH-DYDVRQR
RJJHWWLQRQSH]]LGLPHPRULDDVVHJQDWDDXQSXQWDWRUH
FRPHLQ&
œ /¶RSHUDWRUHqSUHGHILQLWRVXOOHVWULQJKHHVHUYHD
FRQFDWHQDUOH
œ DUJVqXQYHWWRUHGLVWULQJKHFRPHWXWWLLYHWWRUL-DYDq
XQRJJHWWRLOFXLFDPSRSXEEOLFROHQJWKLQGLFDLO
QXPHURGLHOHPHQWLQXPHUDWLGDDOHQJWK
val;
reset()
inc()
dec()
rd()
{
{
{
{
val = 0; }
val++; }
val--; }
return val; }
public class Esempio2 { // contiene solo il main
public static void main(String args[]){
Counter c1, c2;
c1 = new Counter(); c2 = new Counter();
c1.reset(); c2.reset();
c1.inc(); c1.inc(); c2.inc();
System.out.println("c1 vale " + c1.rd());
System.out.println("c2 vale " + c2.rd());
}
œ &RPSLODQGRTXHVWRILOHVLRWWHQJRQRGXHILOHFODVVXQR
SHURJQLFODVVHGHILQLWD(VHPSLRFODVVH
&RXQWHUFODVV
œ /DULJD&RXQWHUFLQWURGXFHXQULIHULPHQWRD
&RXQWHUFLRqXQDVRUWDGLSXQWDWRUHDGXQRJJHWWRGLTXHO
WLSRFKHSHUz121qGLUHWWDPHQWHPDQLSRODELOH
G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QWHU
class Counter {
private int val;
public Counter()
public Counter(int x)
public void reset() {
public void inc()
{
public void dec()
{
public int rd()
{
}
{ val=1; } // default
{ val=x; }
val = 0; }
val++; }
val--; }
return val; }
INTRODUZIONE ALLA OOP IN JAVA -- 9
/¶(6(03,235(&('(17(«ULVWUXWWXUDWR
/** FILE Counter.java
* Questa classe definisce il concetto di
contatore avanti/indietro.
*/
public class Counter {
private int val;
public Counter()
{
public Counter(int x)
public void reset() {
public void inc()
{
public void dec()
{
public int rd()
{
}
val=1; } // default
{ val=x; }
val = 0; }
val++; }
val--; }
return val; }
/** FILE Esempio2bis.java
* Prima applicazione che istanzia nuovi oggetti
*/
public class Esempio2bis {
public static void main(String args[]){
Counter c1, c2;
c1 = new Counter(); c2 = new Counter(5);
c1.inc(); c1.inc(); c2.inc();
System.out.println("c1 vale " + c1.rd());
System.out.println("c2 vale " + c2.rd());
}
}
œ /DFODVVH&RXQWHUUHVDSXEEOLFDqRUDLQXQILOHVHSDUDWR
œ PDLQLQ(VHPSLRELVSXzXVDUHODFODVVH&RXQWHU
VHQ]DGRYHULQFOXGHUHQXOODODSLDWWDIRUPD-DYD
VXSSRUWDLOFROOHJDPHQWRGLQDPLFR
$OSULPRXVRGL&RXQWHULOILOH&RXQWHUFODVVYHUUj
FHUFDWRHFDULFDWRGDOFODVVORDGHUGLVLVWHPD“DGDWWRD
FRGLFHPRELOHHDSSOLFD]LRQLVX,QWHUQHW
INTRODUZIONE ALLA OOP IN JAVA -- 10
,/&21&(772',3$&.$*(
8Q
DSSOLFD]LRQH DG RJJHWWL q TXDVL VHPSUH FRPSRVWD GL
PROWH FODVVL H DYHUOH WXWWH LQVLHPH QHOOD VWHVVD FDUWHOOD
GLUHFWRU\QRQqSUDWLFR
3HU TXHVWR -DYD LQWURGXFH LO FRQFHWWR GL VWUXWWXUD]LRQH LQ
SDFNDJHJUXSSLGLFODVVLDSSDUWHQHQWLDOORVWHVVRSURJHWWR
3$&.$*(','()$8/7
6HXQDFODVVHQRQGLFKLDUDHVSOLFLWDPHQWHDTXDOHSDFNDJH
DSSDUWLHQHYLHQHLPSOLFLWDPHQWHDVVHJQDWDDOSDFNDJHGL
GHIDXOWFKHIDULIHULPHQWRSHUFRQYHQ]LRQHDOGLUHWWRULR
FRUUHQWH
&DUDWWHULVWLFKHSULQFLSDOL
/HFODVVLGHOSDFNDJHGLGHIDXOWVLFRPSLODQRHVLULFKLDPDQR
VHQ]DSUHPHWWHUHDOFXQQRPHGLSDFNDJH
œ8QSDFNDJHSXzFRPSUHQGHUHPROWHFODVVLDQFKH
GHILQLWHLQILOHVHSDUDWL
6,67(0$'(,120,',3$&.$*(
œ(VLVWHXQDFRUULVSRQGHQ]DELXQLYRFDIUDQRPHGHO
SDFNDJHHSRVL]LRQHQHOILOHV\VWHPGHOOHFODVVLGHO
SDFNDJH
XQSDFNDJHGLQRPHSLSSRSUHVXSSRQHFKHWXWWHOHVXH
FODVVLVLWURYLQRLQXQDFDUWHOODGLUHFWRU\GLQRPHSLSSR
package pippo;
MDYDDZWSULQW%RRN
SLSSRSOXWRSDSHUR&RXQWHU
public class Counter {
...
}
package pippo;
public class Esempio2bis {
public static void main(String args[]){
...
}
3HUFRPSLODUHGDOGLUHWWRULRSDGUHGLSLSSR
pippo/Counter.java
pippo/Esempio2bis.java
3HUHVHJXLUHGDOGLUHWWRULRSDGUHGLSLSSR
java
MDYDDZWSULQW
SLSSRSOXWRSDSHUR
&RQVHJXHQWHPHQWHOHFODVVLGHILQLWHDOO¶LQWHUQRGLWDOL
SDFNDJHDYUDQQRFRPHQRPHDVVROXWR
(6(03,2
javac
,OVLVWHPDGHLQRPLGHLSDFNDJHqVWUXWWXUDWRÊTXLQGL
SRVVLELOHDYHUHQRPL³FRPSRVWL´FRPH
pippo.Esempio2bis
INTRODUZIONE ALLA OOP IN JAVA -- 11
3$&.$*(H,03257
4XDQGRVLXVDXQDFODVVHGHILQLWDLQXQDOWURSDFNDJH
RFFRUUHLQJHQHUDOHLQGLFDUHLOQRPHDVVROXWRGHOODFODVVH
/* classe definita nel package di default */
public class Esempio2ter {
public static void main(String args[]){
pippo.Counter c1, c2;
c1 = new pippo.Counter();
c2 = new pippo.Counter(5);
c1.inc(); c1.inc(); c2.inc();
... } }
INTRODUZIONE ALLA OOP IN JAVA -- 12
/¶LVWUX]LRQHLPSRUWFRQVHQWHGLLPSRUWDUHXQQRPHLQ
PRGRGDQRQGRYHUULSHWHUHLOQRPHGHOSDFNDJH
import pippo.Counter;
public class Esempio2ter {
public static void main(String args[]){
Counter c1, c2;
c1 = new Counter(); c2 = new Counter(5);
...
}
}
127$ O¶LVWUX]LRQHLPSRUWQRQqXQDLQFOXGHQRQ
LQFOXGHQXOODHYLWDVRORGLGRYHUULVFULYHUHOXQJKL
QRPLGLFODVVL
3HULPSRUWDUHWXWWHOHFODVVLGLXQSDFNDJHLQXQVROFROSR
LPSRUWSLSSR
œHYHQWXDOLDOWUHFODVVLGHILQLWHLQDOWULSDFNDJHQRQ
SRVVRQRDFFHGHUHDGDWLHPHWRGLGLTXHVWHFRPHVH
IRVVHURSULYDWL
127$ LOPDLQGHYHHVVHUHQHFHVVDULDPHQWHSXEOLFH
YDREEOLJDWRULDPHQWHGHILQLWRLQXQDFODVVHSXEOLF
$WWHQ]LRQH
$GLIIHUHQ]DGHO&RGHO&LQ-DYDQRQqSRVVLELOH
GHILQLUHFODVVLYLVLELOLVRORLQXQILOHLOILOHqVRORXQ
FRQWHQLWRUHILVLFRQRQGHOLPLWDXQDPELWRGLYLVLELOLWj
VFRSH
6HVLKDXQDQHFHVVLWjGHOJHQHUHODVROX]LRQHqGLGHILQLUH
XQDSSRVLWRSDFNDJHLQFXLLQVHULUHWDOLFODVVL
5LDVVXPHQGR
3$&.$*(H9,6,%,/,7¬
9DULHTXDOLILFKHGLYLVLELOLWjLQ-DYD
SXEOLF
SULYDWH
SDFNDJH
/DYLVLELOLWjSDFNDJH
œqLOGHIDXOWSHUFODVVLHPHWRGL
WLSRGLYLVLELOLWj
DFFH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WWRVLKDDGLVSRVL]LRQHLOPHWRGR
FORQHSUHGHILQLWRSHUWXWWHOHFODVVL
S SFORQH
(6(03,2
/**
* verifica il concetto di clonazione di oggetti
*/
import java.awt.Point;
/**
* verifica il concetto di Riferimento a oggetti
*/
import java.awt.Point;
class Esempio3 { // contiene solo il main
public static void main(String args[]){
Point p1 = new Point(100,10), p2 = p1;
System.out.println("p1: " + p1.x + ","
System.out.println("p2: " + p2.x + ","
p1.x = 400;
System.out.println("p1: " + p1.x + ","
System.out.println("p2: " + p2.x + ","
}
}
class Esempio3bis {
public static void main(String args[]){
Point p1 = new Point(100,10), p2=p1;
System.out.println("p1: " + p1.x + "," + p1.y);
System.out.println("p2: " + p2.x + "," + p2.y);
p2 = (Point) p1.clone();
p1.x = 400;
System.out.println("p1: " + p1.x + "," + p1.y);
System.out.println("p2: " + p2.x + "," + p2.y);
+ p1.y);
+ p2.y);
}
}
+ p1.y);
+ p2.y);
œ 2JQLPRGLILFDDSVLULSHUFXRWHDQFKHVXFLzFKHq
ULIHULWRGDSGXHULIHULPHQWLFKHUHIHUHQ]LDQRORVWHVVR
RJJHWWR
œ ,QTXHVWRPRGRSULPDQHLQDOWHUDWRDOYDORUH
PHQWUHSYLHQHPRGLILFDWRLQ
œ 'DQRWDUHO¶RSHUD]LRQHGLFDVWSHUFRQYHUWLUHLOULVXOWDWR
GLFORQHFKHqXQRJJHWWRJHQHULFR2EMHFWLQXQ
RJJHWWRGHOODFODVVH3RLQW
œ 3HUGXSOLFDUHGDYYHURO¶RJJHWWRRFFRUUHXVDUHXQPHWRGR
VSHFLILFRFORQHFKHqSUHGHILQLWRSHUWXWWHOHFODVVL
INTRODUZIONE ALLA OOP IN JAVA -- 17
INTRODUZIONE ALLA OOP IN JAVA -- 18
8*8$*/,$1=$',2**(77,
3RLFKpOHYDULDELOL-DYDVRQRULIHULPHQWLXQWHVWGL
XJXDJOLDQ]DGHOWLSRF FYHULILFDVHGXHULIHULPHQWL
SXQWDQRDOORVWHVVRRJJHWWR121VHGXHRJJHWWLKDQQR
ORVWHVVRYDORUH
3HUFRQWUROODUHVHGXHRJJHWWLKDQQRORVWHVVRYDORUHVLXVD
LOPHWRGRHTXDOVSUHGHILQLWRSHUWXWWHOHFODVVL-DYD
(6(03,2
/**
* verifica il concetto di uguaglianza fra oggetti
*/
import java.awt.Point;
class Esempio4 {
public static void main(String args[]){
Point p1 = new Point(100,10), p2=p1;
System.out.println("p1==p2? ” + (p1==p2));
System.out.println("p1.equals(p2)? ” +
p1.equals(p2));
p2 = (Point) p1.clone(); p1.x = 400;
System.out.println("p1==p2? ” + (p1==p2));
System.out.println("p1.equals(p2)? ” +
p1.equals(p2));
}
}
2XWSXW
675,1*+(,1-$9$
/HVWULQJKH-DYDVRQRRJJHWWLLVWDQ]HGHOODFODVVH
6WULQJXQRJJHWWR6WULQJUDSSUHVHQWDXQRVSHFLILFR
YDORUHFRQWHQXWRGHOODVWULQJDHGqQRQPRGLILFDELOH
,Q-DYDXQD6WULQJQRQqXQEXIIHU
SHULPPDJD]]LQDUHVWULQJKHPRGLILFDELOLVLXVD6WULQJ%XIIHU
œ 6WULQJKHSRVVRQRHVVHUHFRQFDWHQDWHFRQO¶RSHUDWRUH
œ /DFRQFDWHQD]LRQHGLFRVWDQWLVWULQJDqIDWWDDFRPSLOH
WLPHTXLQGLQRQLQWURGXFHLQHIILFLHQ]H
q 4XDQGRVLVFULYRQRFRVWDQWLVWULQJDIUDYLUJROHWWH
HV³FLDR´YLHQHFUHDWRDXWRPDWLFDPHQWHXQRJJHWWR
6WULQJLQL]LDOL]]DWRDWDOHYDORUH
q 8QDFRVWDQWHVWULQJDQRQSXzHFFHGHUHODULJD
GRYHQGRVFULYHUHFRVWDQWLSLOXQJKHqRSSRUWXQR
VSH]]DUOHLQSDUWLSLFRUWHHFRQFDWHQDUOHFRQ
O¶RSHUDWRUH
$WWHQ]LRQH
ODVHOH]LRQHGLXQFDUDWWHUHVLIDWUDPLWHLOPHWRGRFKDU$W
QRQFRQO¶RSHUDWRUH>@FRPHLQ&R&
6WULQJV ³1HOPH]]RGHOFDPPLQ³
FKDUFK VFKDU$W
(VLVWRQRGHFLQHGLRSHUDWRULSUHGHILQLWLVX6WULQJVLYHGDOD
GRFXPHQWD]LRQHGHOOH-DYD$3,$SSOLFDWLRQ3URJUDPPLQJ
,QWHUIDFHSHUPDJJLRULGHWWDJOL
p1==p2? true
p1.equals(p2)? true
p1==p2? false
p1.equals(p2)? false
INTRODUZIONE ALLA OOP IN JAVA -- 19
INTRODUZIONE ALLA OOP IN JAVA -- 20
$55$<,1-$9$
,Q-DYDJOLDUUD\VRQRRJJHWWLQRQHVLVWHXQYHURQRPHGL
FODVVHLQTXDQWRVRQRLGHQWLILFDWLGDOO¶RSHUDWRUH>@
'HILQLUHXQDUUD\-DYDVLJQLILFDGHILQLUHXQULIHULPHQWR
O¶DUUD\YHURHSURSULRYDFUHDWRFRQQHZ
DPHQRFKHQRQVLDVSHFLILFDWRWUDPLWHFRVWDQWL
/DOXQJKH]]DGHOO¶DUUD\qLQGLFDWDGDOFDPSRSXEEOLFRH
QRQPRGLILFDELOHOHQJWKGHOO¶DUUD\VWHVVR
$55$<,1-$9$
'HILQLUHXQDUUD\-DYDVLJQLILFDGHILQLUHXQULIHULPHQWR
O¶DUUD\YHURHSURSULRYDFUHDWRFRQQHZDPHQRFKHLOVXR
FRQWHQXWRQRQVLDVSHFLILFDWRWUDPLWHFRVWDQWL
127$
&UHDUHXQDUUD\FRQQHZVLJQLILFDFUHDUH
œ 1FHOOHGLXQWLSRSULPLWLYRVHO¶DUUD\FRQWLHQHHOHPHQWLGL
WLSRSULPLWLYR
œ 1ULIHULPHQWLDRJJHWWLWXWWLQXOOVHO¶DUUD\qGLRJJHWWL
1HOVHFRQGRFDVRTXLQGLLVLQJROLRJJHWWLGRYUDQQRHVVHUH
FUHDWLHVSOLFLWDPHQWHFRQQHZVHRSSRUWXQR
(6(03,2
/**
*
Primo esempio d’uso di array in Java
*/
(6(03,2
/**
class
*
*/
Esempio5 {
public static void main(String args[]){
int[] v1 = {8,12,3,4};
// vettore di 4 int
int[] v2; // riferimento a un vettore di int
v2 = new int[5]; // creazione vett. di 5 int
// (crea 5 celle int, inizializzate a 0)
class
Esempio5bis {
static void main(String args[]){
public
String
Secondo esempio d’uso di array Java
//
for(int i=1; i<=v2.length; i++) v2[i-1] = i*i;
for(int i=0; i<v1.length; i++)
System.out.print(v1[i] + '\t');
System.out.println(“”);
for(int i=0; i<v2.length; i++)
System.out.print(v2[i] + '\t');
}
v1[]; // RIFERIMENTO al futuro vettore
si può anche scrivere String[] v1;
v1
= new String[2];
creazione vettore di due (rif. a) String
// NB: le due String però ancora non esistono!
//
v1[0]
= "Ciao mondo"; // una String costante
v1[1] = new String(v1[0]);
// una String creata din. uguale alla prima
}
}
}
INTRODUZIONE ALLA OOP IN JAVA -- 21
INTRODUZIONE ALLA OOP IN JAVA -- 22
7,3,35,0,7,9,H&/$66,&255,6321'(17,
6SHVVRqQHFHVVDULRWUDWWDUHLWLSLSULPLWLYLFRPHRJJHWWLDG
HVHPSLRSHUSDVVDUOLSHUULIHULPHQWRDXQDIXQ]LRQHPD
QRQVROR
³ZUDSFODVVHV´XQDSHURJQLWLSRSULPLWLYR
7LSR
ERROHDQ
FKDU
E\WH
VKRUW
LQW
ORQJ
IORDW
GRXEOH
&ODVVH
FRUULVSRQGHQWH
%RROHDQ
&KDUDFWHU
%\WH
6KRUW
,QWHJHU
/RQJ
)ORDW
'RXEOH
œ DSDUWHODOHWWHUDLQL]LDOHODFODVVHKDTXDVLVHPSUHOR
VWHVVRQRPHGHOWLSRSULPLWLYRHFFHWWRFKDUHLQW
œ OHFODVVLIRUQLVFRQRPHWRGLSHUFRQYHUWLUHGDOWLSR
SULPLWLYRDXQRJJHWWRGHOODFODVVHHYLFHYHUVD
class Esempio6 {
public static void main(String args[]){
int x = 35;
// tipo primitivo
Integer ix = new Integer(x);
// conversione int “ Integer per costruzione
System.out.println("ix = " + ix);
// conversione implicita Integer “ String
x = ix.intValue()*4;
// conversione esplicita Integer “ int
System.out.println("x = " + x);
// conversione implicita int “ String
} }
INTRODUZIONE ALLA OOP IN JAVA -- 23
0(72',',&/$66(VWDWLF
,PHWRGLGHILQLWLGHQWURDXQDFODVVHHYLVWLILQRDTXHVWR
SXQWRDSDUWHLOPDLQ«HUDQRGHVWLQDWLDHVVHUHLQYRFDWLVX
XQ¶LVWDQ]DGLTXHOODFODVVH
class Counter {
private int val;
public Counter()
{
public Counter(int x)
public void reset() {
public void inc()
{
public void dec()
{
public int rd()
{
}
val=1; }
{ val=x; }
val = 0; }
val++; }
val--; }
return val; }
public class Esempio2bis {
public static void main(String args[]){
Counter c1, c2;
c1 = new Counter(); c2 = new Counter(5);
c1.inc(); c1.inc(); c2.dec();
System.out.println("c1 vale " + c1.rd());
System.out.println("c2 vale " + c2.rd());
}
}
0DOHFODVVL-DYDSRVVRQRIXQJHUHDQFKHGDFRQWHQLWRULGL
IXQ]LRQLDQFKHSHUFKpLQ-DYDQRQqFRQVHQWLWRGLGHILQLUH
IXQ]LRQL³HVWHUQH´FRPHLQ&
,PHWRGLGLFODVVHVWDWLFQRQVRQRGHVWLQDWLDHVVHUH
LQYRFDWLVXLVWDQ]HGHOODFODVVHPDFRPHIXQ]LRQL³VWDQG
DORQH´FKHIRUQLVFRQRIXQ]LRQDOLWjXWLOLLQTXDOFKHPRGR
FRUUHODWHFRQODFODVVHFKHOLLPSOHPHQWD
127$ LOPDLQGLXQDFODVVHSXEOLFqLOFDVRSLHYLGHQWHH
IUHTXHQWHGLPHWRGRVWDWLF
INTRODUZIONE ALLA OOP IN JAVA -- 24
0(72',',&/$66(±81(6(03,2
4XHVWRHVHPSLRUDSSUHVHQWDXQFDVROLPLWHLQFXLYHQJRQR
XWLOL]]DWLVRORPHWRGLGLFODVVH
class Esempio7 {
static int[] creaIntervallo(int inf, int sup){
// restituisce un (rif. a) un array di int
int[] v = new int[sup-inf+1];
for (int i=0; i<v.length; i++) v[i]=inf++;
return v;
}
static void print(int v[]){
for(int i=0; i<v.length; i++)
System.out.println(v[i]);
}
static void increment(int v[]){
for(int i=0; i<v.length; i++) v[i]++;
}
public static void main(String args[]){
int[] v1 = creaIntervallo(3,8);
print(v1);
// Esempio7.print(v1);
increment(v1);
// Esempio7.increment(v1);
print(v1);
// Esempio7.print(v1);
}
}
1HOOHFKLDPDWHLOQRPHGHOODFODVVHSXzTXLHVVHUHRPHVVR
LQTXDQWRLPHWRGLYHQJRQRLQYRFDWLGDDOWULPHWRGLGHOOD
FODVVHODVLQWDVVLFRPSOHWDREEOLJDWRULDSHUFKLDPDWH
SURYHQLHQWLDOGLIXRULGHOODFODVVHqLQGLFDWDDODWRFRPH
FRPPHQWR
INTRODUZIONE ALLA OOP IN JAVA -- 25
9$5,$%,/,',&/$66(VWDWLF
/HYDULDELOLGLFODVVHVRQRDVVRFLDWHDXQDFODVVHHQRQDG
XQVLQJRORRJJHWWR
F¶qXQDVRODFRSLDGHOODYDULDELOHLQGLSHQGHQWHPHQWH
GDOQXPHURGLLVWDQ]HGHILQLWHGLTXHOODFODVVH
/HYDULDELOLGLFODVVHVRQRGHILQLWHFRPHVWDWLF
class Esempio7ter {
static int x = 8;
// variabile di classe
public static void main(String args[]){
x--;
// OK (visib. package)
// Sintassi completa: Esempio7ter.x--;
}
}
&RPHSHULPHWRGLGLFODVVHDQFKHSHUOHYDULDELOLGLFODVVH
LOQRPHFRPSOHWRFRPSUHQGHLOQRPHGHOODFODVVHTXLORVL
SXzRPHWWHUHLQTXDQWRO¶XVRGL[qHIIHWWXDWRORFDOPHQWHDOOD
FODVVHVWHVVD
81(6(03,2127(92/(/$&/$66(0DWK
0DWKqXQDFODVVHGLVLVWHPDGHILQLWDLQMDYDODQJFKH
FRVWLWXLVFHODOLEUHULDPDWHPDWLFDFRPSUHQGHVRORPHWRGLH
GDWLVWDWLFL
œ YDULDELOLGLFODVVH (H 3,
œ PHWRGLGLFODVVH DEVDVLQDFRVDWDQ
PLQPD[H[SORJSRZVLQ
FRVWDQVTUWHWF
INTRODUZIONE ALLA OOP IN JAVA -- 26
&267$17,
2JQLYDULDELOH-DYDSXzHVVHUHFRQVLGHUDWDXQDFRVWDQWH
VHHWLFKHWWDWDGDOODSDURODFKLDYHILQDO
*HQHUDOPHQWHYHQJRQRXWLOL]]DWHFRVWDQWLSXEEOLFKH
UHDOL]]DWHWUDPLWHYDULDELOLGLFODVVHSXEOLFUHVHFRVWDQWL
DWWUDYHUVRO¶XVRGHOPRGLILFDWRUHILQDO
class Esempio7quater {
public static final int mymax = 8;
// costante pubblica (di classe)
}
ÊSRVVLELOHGHILQLUHDQFKHFRVWDQWL³DXVRLQWHUQR´VLD
VRWWRIRUPDGLYDULDELOLGLFODVVHQRQSXEEOLFKHYLVLELOLH
XWLOL]]DELOLLQWXWWDODFODVVHVLDGLYDULDELOLORFDOLDXQ
PHWRGRYLVLELOLHXWLOL]]DELOLVRORHQWURTXHOPHWRGR
class Esempio7quater {
static final int mymax = 8;
// costante a uso interno (di classe)
void metodo(){
final float radiceDi2 = 1.4142;
}
/¶,67$1=$&255(17(WKLV
/DSDURODFKLDYHWKLVFRVWLWXLVFHXQULIHULPHQWR
DOO¶RJJHWWRFRUUHQWHHSXzVHUYLUH
SHULQGLFDUHXQFDPSRGDWLLQFDVRGLRPRQLPLDFRQXQ
SDUDPHWURRFRQXQDYDULDELOHORFDOH
LQXQFRVWUXWWRUHSHUULFKLDPDUHXQDOWURFRVWUXWWRUHGHOOD
VWHVVDFODVVH
(6(03,2ULGHILQL]LRQHGHOODFODVVH&RXQWHU
class Counter {
private int val;
public Counter()
{ this(1); }
public Counter(int val) { this.val=val; }
public void reset() { val = 0; }
public void inc()
{ val++; }
public void dec()
{ val--; }
public int rd()
{ return val; }
}
,OFRVWUXWWRUH&RXQWHULQWYDOXWLOL]]DWKLVSHUGLVWLQ
JXHUHLOSDUDPHWURYDOGDOFDPSRGDWLGLQRPHYDOFKH
YLHQHHVSUHVVRFRQODQRWD]LRQHWKLVY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PSOPDQXDOH
,QIDVHGLLVWDQ]LD]LRQH
œSULPDVLFRVWUXLVFHO¶RJJHWWRFRQWHQLWRUH
œSRLVLFRVWUXLVFRQRFRQQHZJOLRJJHWWLLQWHUQL
œFUHDUHXQDQXRYDFODVVHFKHFRQWHQJDDOVXRLQWHUQRXQ
RJJHWWRGHOODFODVVHSUHHVLVWHQWH
“LFDPSLGDWLSULYDWLQRQVRQRGLUHWWDPHQWHPDQLSRODELOL
“ULVFULYHUHDQFKHLPHWRGLFKHULPDQJRQRXJXDOL
(6(03,2
class Counter {
int val;
public Counter()
{ val=1; }
public Counter(int c) { val=c; }
public void inc()
{ val++; }
public int rd()
{ return val; }
}
public class Esempio {
Counter c;
(6(03,2GD&RXQWHUEDVHD&RXQWHUFRQGHFUHPHQWR
class Counter2 {
Counter c;
public Counter2()
{c = new Counter(); }
public Counter2(int v) {c = new Counter(v);}
public void inc()
{ c.inc(); }
public int rd()
{ return c.rd(); }
public void dec()
{ c.val--; }
}
// oggetto contenitore
// oggetto contenuto
public Esempio() { //costruttore di Esempio
c = new Counter(3);
}
œ LOFDPSRGDWLFYDOqDFFHVVLELOHSHUFKpKDYLVLOLELWj
SDFNDJHHODFODVVH&RXQWHUqGHILQLWDQHOORVWHVVR
SDFNDJHGL&RXQWHU
public static void main(String args){
Esempio e = new Esempio();
}
œ qFRPXQTXHQHFHVVDULRULVFULYHUHLPHWRGLVRORSHUDGDW
WDUOLDOODQXRYDFODVVHPDGLIDWWRULVFULYHQGROLXJXDOL
VHQ]DDJJLXQJHUHQXRYHIXQ]LRQDOLWj
}
2FFRUUHSRWHUULXVDUHOHFODVVLHVLVWHQWLLQPRGRSL
IOHVVLELOH
EREDITARIETÀ E POLIMORFISMO IN JAVA
1
EREDITARIETÀ E POLIMORFISMO IN JAVA
2
(5(',7$5,(7¬
(5(',7$5,(7¬FRPH5(/$=,21()5$&/$66,
œ 6LYXROHULXVDUHWXWWRFLzFKHSXzHVVHUHULXVDWR
œ QRQqXWLOHQ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counter {
public void dec()
{ c.val--; }
}
œ &RXQWHUFRQWLHQHODVWHVVDLQWHUIDFFLDGL&RXQWHU
LFRVWUXWWRULQRQIDQQRVWUHWWDPHQWHSDUWHGHOO¶LQWHUIDFFLD
VLSXzRVVHUYDUHFKHqVWDWRXWLOL]]DWRXQRJJHWWR&RXQWHU
FVHQ]DDYHUQHGHILQLWRLOFRVWUXWWRUH
œRJQL&RXQWHUqDQFKHXQ&RXQWHURYYHUR
œRJQLRJJHWWRGLFODVVH&RXQWHUqLPSOLFLWDPHQWH
DQFKHGLFODVVH&RXQWHU
œPDQRQYLFHYHUVDSHUFKp&RXQWHUqSLULFFRGL
&RXQWHUVDIDUHFRVHFKHXQ&RXQWHUQRQVDIDUHH
SXzDYHUHGHLFDPSLGDWLLQHVLVWHQWLLQ&RXQWHU
œ-DYDQRQREEOLJDDGHILQLUHDOPHQRXQFRVWUXWWRUHSHU
RJQLFODVVHDQFKHVHFLzqIRUWHPHQWHUDFFRPDQGDWR
œLQDVVHQ]DGLFRVWUXWWRULGHILQLWLHVSOLFLWDPHQWH-DYD
GHILQLVFHDXWRPDWLFDPHQWHXQFRVWUXWWRUHGLGHIDXOW
œLOFRVWUXWWRUHGLGHIDXOWVLOLPLWDDULFKLDPDUHLO
FRVWUXWWRUHGLGHIDXOWGHOODVXSHUFODVVH
&RQVHJXHQ]D&21)250,7¬
,OFRGLFHSUHFHGHQWHHTXLYDOHFLRqD
œXQ¶LVWDQ]DGL&RXQWHUSXzVRVWLWXLUHXQRJJHWWR&RXQWHU
LQPDQLHUDWUDVSDUHQWHULVSHWWRDJOLRJJHWWLFKHOR
XWLOL]]DQR
class Counter2 extends Counter {
public void dec()
{ c.val--; }
// il costruttore di default generato da Java
public Counter2() { super(); }
}
œPDQRQLOFRQWUDULR
(6(03,2
/¶LQYRFD]LRQHVXSHUULFKLDPDLQYRFDLOFRVWUXWWRUHGL
GHIDXOWGHOODFODVVHEDVHTXDORUDQRQHVLVWDYLHQHVROOHYDWD
XQ¶HFFH]LRQH
class Esempio1 {
Counter c1 = new Counter(3);
Counter2 c2 = new Counter2();
c2.dec();
// OK: dec() esiste in Counter2
// c1.dec();
// NO: dec() non c’è in Counter
c1=c2;
// OK: ogni Counter2 è Counter
// c2=c1;
// NO: c1 è un semplice Counter
}
EREDITARIETÀ E POLIMORFISMO IN JAVA
9
EREDITARIETÀ E POLIMORFISMO IN JAVA
10
6XSHUHWKLV
(5(',7$5,(7¬H&26758=,21(',2**(77,
,QEUHYH
3HUFRVWUXLUHXQRJJHWWRGLXQDFODVVHGHULYDWDqVHPSUH
QHFHVVDULRFKLDPDUHXQFRVWUXWWRUHGHOODFODVVHEDVH
LQTXDQWR
œWKLVqXQULIHULPHQWRDOO¶LVWDQ]DFRUUHQWH
œVXSHUqXQULIHULPHQWRDOODFODVVHEDVH
œVRORLOFRVWUXWWRUHGHOODFODVVHEDVHSXzVDSHUHFRPH
LQL]LDOL]]DUHLFDPSLGDWLHUHGLWDWLGDWDOHFODVVHLQPRGR
FRUUHWWR
œqLOVRORPRGRSHUJDUDQWLUHO¶LQL]LDOL]]D]LRQHGLFDPSLGDWL
SULYDWLDFXLODVRWWRFODVVHQRQSXzDFFHGHUH
GLUHWWDPHQWH
œVLHYLWDXQ
LQXWLOHGXSOLFD]LRQHGLFRGLFHQHOODVRWWRFODVVH
0DTXDOHFRVWUXWWRUHGHOODFODVVHEDVHYLHQHFKLDPDWR"
œLOFRVWUXWWRUHGLGHIDXOWGHILQLWRGD-DYDFKLDPDLO
FRVWUXWWRUHGLGHIDXOWGHOODFODVVHEDVH
QRWD]LRQHXWLOHQHOFDVRLQFXLODVRWWRFODVVHGHILQLVFDXQ
FDPSRGDWLRPRQLPRFRQXQRGHOODFODVVHEDVH
œ VXSHUIULFKLDPDLOPHWRGRIGHOODFODVVHEDVH
QRWD]LRQHXWLOHQHOFDVRLQFXLODVRWWRFODVVHGHILQLVFDXQ
PHWRGRRPRQLPRGLXQRGHOODFODVVHEDVHRYHUULGLQJ
œSHULFRVWUXWWRULGHILQLWLGDOO¶XWHQWHRFFRUUHVSHFLILFDUH
HVSOLFLWDPHQWHTXDOHFRVWUXWWRUHGHOODFODVVHEDVHYDGD
FKLDPDWRPHGLDQWHODQRWD]LRQHVXSHU«FKHGHYH
HVVHUHODSULPDLVWUX]LRQHGHOFRUSRGHOFRVWUXWWRUH
6HPDQFDXQDFKLDPDWDHVSOLFLWDLOFRPSLODWRUH-DYD
LQVHULVFHDXWRPDWLFDPHQWHXQDFKLDPDWDDOFRVWUXWWRUHGL
GHIDXOWGHOODFODVVHEDVHVXSHUDPHQRFKHQRQFLVLD
JLjXQDFKLDPDWDDXQDOWURFRVWUXWWRUHGHOODVWHVVDFODVVH
PHGLDQWHODQRWD]LRQHWKLV
(6(03,2&RXQWHUGHILQLVFHXQFDPSRYDO
class DualCounter extends Counter {
double val;
// val indica ora il campo val di DualCounter
// mentre super.val indica il val di Counter
public void inc() { super.inc(); val++; }
// inc() indica il metodo inc() di DualCounter
// super.inc() chiama l’inc() di Counter
}
$GHVHPSLRO¶HVSUHVVLRQHVXSHU[LQYRFDLOFRVWUXWWRUH
GHOODFODVVHEDVHFRQXQDUJRPHQWRHFFH]LRQHVHXQ
FRVWUXWWRUHVLIIDWWRQRQHVLVWH
EREDITARIETÀ E POLIMORFISMO IN JAVA
6RQRHVSUHVVLRQLOHFLWH
œ WKLVYDOLQGLFDLOFDPSRYDOGHOODFODVVHGLLVWDQ]D
FRUUHQWH
œ WKLVIULFKLDPDLOPHWRGRIGHOODFODVVHGHOO¶LVWDQ]D
FRUUHQWH
œ WKLVULFKLDPDXQDOWURFRVWUXWWRUHGHOODVWHVVD
FODVVH
œ VXSHUULFKLDPDXQFRVWUXWWRUHGHOODFODVVHEDVH
œ VXSHUYDOLQGLFDLOFDPSRYDOGHOODFODVVHEDVH
11
EREDITARIETÀ E POLIMORFISMO IN JAVA
12
&267587725,12138%%/,&,
(6(03,25,$66817,923(5621((678'(17,
/**
,QDVVHQ]DGLFRVWUXWWRULGHILQLWLGDOO¶XWHQWH-DYDGHILQLVFH
VHPSUHXQFRVWUXWWRUHGLGHIDXOWSHURJQLFODVVHWDOH
FRVWUXWWRUHqSXEEOLFRLQPRGRGDFRQVHQWLUHDOODFODVVH
GLHVVHUHQRUPDOPHQWHLVWDQ]LDWD
la classe Persona
*/
class
,QDOFXQHFLUFRVWDQ]HWXWWDYLDVLSXzGHVLGHUDUHGLLPSHGLUH
FKHXQDFODVVHSRVVDHVVHUHLVWDQ]LDWDGDFKLXQTXH,Q
SDUWLFRODUHSXzHVVHUHQHFHVVDULR
œLPSHGLUHTXDOXQTXHLVWDQ]LD]LRQH
œFRQVHQWLUHVRORLVWDQ]LD]LRQLGDSDUWHGLVRWWRFODVVL
Persona {
String nome;
int anni;
Persona() { nome = "sconosciuto"; anni = 0; }
Persona(String n) { nome = n; anni = 0; }
Persona(String n, int a) { nome=n; anni=a; }
void print(){
System.out.print("Il mio nome e’ " + nome);
System.out.println(" e ho " + anni + " anni");
} }
/**
1HOSULPRFDVRO¶XWHQWHGHYHGHILQLUHXQFRVWUXWWRUH
SULYDWRQHOVHFRQGRXQFRVWUXWWRUHSURWHWWR
la classe Studente che eredita da Persona
*/
class
,QTXHVWRPRGRVLSUHYLHQHODJHQHUD]LRQHGHOFRVWUXWWRUH
SXEEOLFRDXWRPDWLFRGDSDUWHGL-DYD
Studente extends Persona {
int matr;
Studente() { super(); matr = 9999; }
Studente(String n) { super(n); matr = 8888; }
Studente(String n, int a) { super(n,a); matr=7;}
Studente(String n, int a, int m) {
super(n,a); matr=m; }
void print(){ super.print();
System.out.println("Matricola = " + matr); }
}
&/$66,),1$/,ILQDO
public class Esempio2 {
public static void main(String args[]){
Studente studente;
studente = new Studente();
studente.print();
&RPHVLqJLjYLVWRXQDYDULDELOHGLFKLDUDWDILQDOGLYHQWD
XQDFRVWDQWHRVVLDQRQqSLPRGLILFDELOH
$OORVWHVVRPRGRXQDFODVVHGLFKLDUDWDILQDOGLYHQWDXQD
FODVVHILQDOHRYYHURGDFXLQRQqSRVVLELOHGHULYDUH
VRWWRFODVVL
studente = new Studente("Tom", 30);
studente.print();
studente = new Studente("Laura", 20, 1234);
studente.print();
} }
EREDITARIETÀ E POLIMORFISMO IN JAVA
13
EREDITARIETÀ E POLIMORFISMO IN JAVA
14
(5(',7$5,(7¬(&203$7,%,/,7¬',7,32
32/,025),602
6HODFODVVH%qVRWWRFODVVHGHOODFODVVH$RJQLRJJHWWR
GLFODVVH%qDQFKHGLFODVVH$QRQYLFHYHUVD
ODFODVVH%qXQVRWWRWLSRGHOODFODVVH$
5LFRUGLDPRFKHXQDVH]LRQHGLFRGLFHVLGLFH
32/,025)$VHqFDSDFHGLRSHUDUHVXRJJHWWLGLWLSR
GLYHUVRVSHFLDOL]]DQGRLOVXRFRPSRUWDPHQWRLQEDVH
DOWLSRGHOO¶RJJHWWRVXFXLRSHUD
&LzLPSOLFDXQDFRPSDWLELOLWjFRQIRUPLWjGDVRWWRWLSRD
WLSRQRQYLFHYHUVDGDFXLVHJXHODSRVVLELOLWjGLXVDUHXQ
RJJHWWRGHOODFODVVHGHULYDWDDOSRVWRGLXQRGHOODFODVVH
EDVHQRQYLFHYHUVD
'LSHQGHVHSUHYDOHLOWLSRIRUPDOHGHOULIHULPHQWR
3HUVRQDQHOO¶HVHPSLRSUHFHGHQWHRLOWLSRHIIHWWLYR
GHOO¶LVWDQ]DFRUUHQWH6WXGHQWHQHOO¶HVHPSLRSUHFHGHQWH
(6(03,2
class Esempio3 {
Persona p = new Persona(“John”);
Studente s = new Studente(“Tom”);
p.print();
// stampa solo il nome
s.print();
// stampa nome e matricola
p=s;
p.print();
// COSA STAMPA ???
}
,Q-DYDSUHYDOHLOWLSRHIIHWWLYRGHOO¶LVWDQ]DFRUUHQWH
“/(*$0(',1$0,&2/$7(%,1',1*
/¶DVVHJQDPHQWRS V
(6(03,2
œqSRVVLELOHSHUFKpRJQL6WXGHQWHqDQFKHXQD3HUVRQD
œHQRQFRPSRUWDSHUGLWHGLLQIRUPD]LRQHSHUFKpVLWUDWWD
GLXQDVVHJQDPHQWRIUDULIHULPHQWLQRQIUDYDULDELOL
3UREOHPD
œ SqGHILQLWRFRPHULIHULPHQWRDXQDJHQHULFD3HUVRQD
œPDIDULIHULPHQWRLQUHDOWjDXQR6WXGHQWH
public class Esempio2 {
public static void main(String args[]){
Persona p = new Studente(“Anna”);
p.print(); // STAMPA NOME E MATRICOLA !!
}
}
6HEEHQHSVLDXQULIHULPHQWRD3HUVRQDSRLFKpO¶LVWDQ]D
FRUUHQWHqXQR6WXGHQWHYLHQHULFKLDPDWRLOPHWRGR
SULQWGHOODFODVVH6WXGHQWH
&RPHYLHQHLQWHUSUHWDWRRUDS"&RPHVHPSOLFH3HUVRQDR
FRPH6WXGHQWH"
EREDITARIETÀ E POLIMORFISMO IN JAVA
,Q-DYDODSRVVLELOLWjGLXVDUHXQRJJHWWRGHOODFODVVH%DO
SRVWRGLXQRGLFODVVH$LQWURGXFHODSRVVLELOLWjWHRULFDGL
DYHUHSROLPRUILVPR(LQSUDWLFD"
15
EREDITARIETÀ E POLIMORFISMO IN JAVA
16
,17(5)$&&(,1-$9$
2FFRUUHUHEEHURGXHJHUDUFKLHGLIIHUHQ]LDWH
½XQDSHUODSDUWHLPSOHPHQWDWLYDULXWLOL]]RGLFRGLFH
½XQDSHUO¶LQWHUIDFFLDHVWHUQDFDSDFHGLPDQWHQHUHOD
FRUUHWWH]]DFRQFHWWXDOH
8QDLQWHUIDFFLD-DYDFRVWLWXLVFHXQDSXUDVSHFLILFDGL
LQWHUD]LRQH
½FRQWLHQHVRORGLFKLDUD]LRQLGLPHWRGLHG
HYHQWXDOPHQWHFRVWDQWL
½121FRQWLHQHYDULDELOLQpGHILQL]LRQLGLPHWRGL
5HDO
&RPSOH[
&ODVV
LQKHULWDQFH
8QDFODVVH-DYDSXzLPSOHPHQWDUHXQDRSLLQWHUIDFFH
/DFODVVH'(9(GHILQLUHWXWWLLPHWRGLGHOOHLQWHUIDFFH
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HVLXVDLQFDSVXODUH6\VWHPLQLQXQRJJHWWRGRWDWRGL
PDJJLRULIXQ]LRQDOLWjFRPHDGHVHPSLRXQ%XIIHUHG5HDGHUFKH
IRUQLVFHDQFKHXQPHWRGRUHDG/LQH
import java.io.*;
class EsempioIn {
public static void main(String args[]){
int a = 0, b = 0;
BufferedReader in = new BufferedReader(
new InputStreamReader(System.in));
try { System.out.print("Primo valore: ");
a = Integer.parseInt(in.readLine());
System.out.print("Secondo valore:");
b = Integer.parseInt(in.readLine());}
catch (IOException e) {
System.out.println("Errore in input");
}
System.out.println("La somma vale " + (a+b));
} }
ECCEZIONI IN JAVA -- 2
4XLUHDG/LQHVROOHYDXQD,2([FHSWLRQLQFDVRGL
HUURUHLQIDVHGLLQSXWPHQWUH,QWHJHUSDUVH,QWVROOHYD
XQD1XPEHU)RUPDW([FHSWLRQQRQFDWWXUDWDVHODVWULQJD
UHVWLWXLWDGDUHDG/LQHQRQFRUULVSRQGHDOODVLQWDVVLGLXQ
QXPHURLQWHUR
8QDHFFH]LRQHQRQFDWWXUDWDVLSURSDJDYHUVRO¶HVWHUQR
GLEORFFRLQEORFFRVHUDJJLXQJHLOPDLQSURYRFDOD
WHUPLQD]LRQHGHOO¶DSSOLFD]LRQH
3RLFKpXQ¶HFFH]LRQHqXQRJJHWWRSXzFRQWHQHUHGDWLR
GHILQLUHPHWRGL
œ WXWWHOHHFFH]LRQLGHILQLVFRQRXQPHWRGRJHW0HVVDJH
FKHUHVWLWXLVFHLOPHVVDJJLRG¶HUURUHDVVRFLDWR
œ DOFXQHHFFH]LRQLGHILQLVFRQRGHLFDPSLFRPH
E\WHV7UDQVIHUUHGLQ,QWHUUXSWHG,2([FHSWLRQFKH
IRUQLVFRQRXOWHULRULLQIRUPD]LRQLXWLOLSHUPHJOLRJHVWLUH
O¶HYHQWRDQRPDOR
5,/$1&,$5((&&(=,21,
&26¶Ê81¶(&&(=,21(
8QDHFFH]LRQHqXQRJJHWWRLVWDQ]DGL
MDYDOD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