1. Programmazione 2 (PR2)
Maurizio ATZORI
Dipartimento di Matematica e Informatica
Università di Cagliari
email: atzori@unica.it
http:/riemann.unica.it/~atzori/
Lezione 2: Classi, oggetti, variabili
2. Etiquette
Telefoni spenti
No portatili, salvo diversa indicazione
No Facebook, email, chat ecc.
No bibite, cibo ecc.
Per qualsiasi dubbio chiedere alzando la mano
Interazione gradita
Materiale sul sito
3. Orario lezioni
Martedì 14:30-16:10
Giovedì 14:30-16:10
Venerdì 16:30-19:10
In laboratorio; TA: Francesco Usai
Sarete divisi in 2 gruppi:
16:30-17:45 Gruppo A
17:55-19:10 Gruppo B
Ricevimento Martedì dopo la lezione in aula
Obbligo di frequenza: firmare!
4. Pagina PR2 su Moodle
http://moodle.unica.it
Facoltà di Scienze MM.FF.NN
Programmazione 2
13. Codice Java polimorfico
Shape p1 = new Shape();
Shape p2 = new Circle();
Shape p3 = new Square();
Shape p4 = new Triangle();
... migliaia di righe di codice...
Tutte le variabili sono di tipo (appartengono alla classe) Shape,
ma a runtime:
p2 contiene un oggetto di tipo Circle
p3 conterrà un oggetto di tipo Square
p2.draw() // disegna un cerchio?
p3.draw() // disegna un quadrato?
18. Creating New Types
class MyNewType {
// definition here
}
D'ora in poi potremmo creare variabili di tipo
MyNewType
MyNewType x = new MyNewType();
19. Creating New Types
class MyNewType {
// attributi (o campi, o var membro)
// metodi (o funzioni membro)
}
Fields (a.k.a. member variables, data
members)
Methods (a.k.a. member functions)
20. Creating New Types
class MyNewType {
// attributi (o campi, o var membro)
// metodi (o funzioni membro)
}
class Car {
Engine motore = new Engine();
void accendi() {
return;
}
}
21. Class Members
Fields (a.k.a. member variables, data
members)
Methods (a.k.a. member functions)
class Car {
Engine motore = new Engine();
void accendi() {
motore.turnOn();
return;
}
}
22. Modificatori d'accesso
public
private
protected
“default” access, o anche:
package access
friendly access
static final syncronized
23. Class Members
public class Car {
private Engine motore = new Engine();
public void accendi() {
motore.turnOn();
return;
}
}
// somewhere
Car fiatPunto = new Car();
fiatPunto.accendi();
24. Java’s Primitive Types
Type Size Wrapper type
boolean - Boolean
char 16-bit Character
byte 8-bit Byte
short 16-bit Short
int 32-bit Integer
long 64-bit Long
float 32-bit Float
double 64-bit Double
void - Void
25. Let’s Write Something
// Our first program. File: HelloDate.java
// Note that the file name is exactly the same
// as the class name, including capitalization.
public class HelloDate {
public static void main(String[] args) {
String s = “world”;
int anno = 2011;
System.out.print(“Hello ”);
System.out.println(s);
System.out.println(anno);
}
}
26. Compilazione/Interpretazione
Editing
Use any text editor you like (not a word processor!); save
as HelloDate.java
Compiling
From a DOS or UNIX command line, type
> javac HelloDate.java
This should produce the file HelloDate.class
Running
Again from the command prompt, type
> java HelloDate