1 / 19

Oggetti

Oggetti. Java. Un esempio di oggetto. Punto nel piano cartesiano Definito da due coordinate: x e y. P. y. x. La classe Punto. public class Punto { private double x; private double y; public double getx(){ return x; } public void setx(double x){ this.x=x; }

Télécharger la présentation

Oggetti

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Oggetti Java

  2. Un esempio di oggetto • Punto nel piano cartesiano • Definito da due coordinate: x e y P y x

  3. La classe Punto public class Punto { private double x; private double y; public double getx(){ return x; } public void setx(double x){ this.x=x; } public double gety(){ return y; } public void sety(double y) { this.y=y; } }

  4. this • this è un riferimento all’oggetto • Es. this.x = x • In questo caso serve per differenziare l’attributo x dell’oggetto dal parametro x del metodo

  5. I metodi • La sintassi di un metodo è la seguente: <modificatore> <tipo> <nome> <parametri>){ <corpo> }

  6. Modificatore • Definisce la visibilità • public • private • protected • E altre informazioni • static (appartiene alla classe) • final (non modificabile)

  7. Firma (Segnatura) • La firma di un metodo è composta da: • Nome e lista ordinata dei tipi dei parametri • Es. la firma di setx è (setx,double) • La firma non comprende il tipo del metodo né i nomi dei parametri

  8. Overloading • Sovraccarico • Più metodi hanno lo stesso nome ma firma diversa (numero e/o tipo di parametri)

  9. Gli oggetti public class ProvaPunto { public static void main(String arg[]{ Punto p; p=new Punto(); System.out.print(p.getx()); } } ? P x y

  10. Inizializzazione degli attributi • E’ possibile definire un valore iniziale per gli attributi public class Punto { private double x=0; private double y=0; … }

  11. Costruttori • Il costruttore è un metodo che ha lo stesso nome della classe • Non ha modificatore e non ha tipo • Il costruttore viene chiamato quando si istanzia un oggetto della classe • Possono esistere più costruttori con firma (segnatura) diversa

  12. Costruttori per Punto Punto() { x=0; y=0; } Punto(double x) { this.x=x; y=0; } Punto(double x,double y) { this.x=x; this.y=y; } … … Punto p,q,r; p=new Punto(); q=new Punto(3.5); r=new Punto(5,3.2); … …

  13. Metodo distanzaOrigine • Restituisce la distanza del punto dall’origine degli assi cartesiani • Utilizziamo i metodi della classe Math public double distanzaOrigine() { return (Math.sqrt(Math.pow(x,2)+Math.pow(y,2))); }

  14. Metodo stampa • Visualizza le coordinate del punto nella forma (x,y) public void stampa(){ String rappresent="( "; rappresent=rappresent+x; rappresent=rappresent+" , "; rappresent=rappresent+y; rappresent=rappresent+" )"; System.out.print(rappresent); }

  15. Metodo toString • Restituisce una rappresentazione dell’oggetto sotto forma di stringa public String toString() { return "( "+x+" , "+y+" )"; }

  16. Ridefinizione del metodo stampa • Avendo definito il metodo toString possiamo rivedere l’implementazione del metodo stampa public void stampa(){ System.out.print(this.toString());}

  17. Metodo distanzaDaPunto • Restituisce la distanza da un punto passato come parametro

  18. Metodo piùVicinoOrigine • Restituisce true se il metodo in esame è più vicino all’origine di un punto che riceve come parametro

  19. Metodo quadrante • Restituisce il numero del quadrante 2 1 0,0 4 3

More Related