Begreber og Redskaber til Programmering

Download Report

Transcript Begreber og Redskaber til Programmering

Begreber og Redskaber 3
Plan for idag
•
•
•
•
Om metoder, parametre, returværdier
Overblik over klasser,objekter,nedarvning
Et par ord om objekt-orientering
Håndkøring af programmer
Håndkøring 1
public class
Application{
public static void
main(String args[]){
int i,j;
i=1; j=3; •
while(j>0){
i=i*2;
j=j-1;
} } }
Application.main
args = null
i=1
j=3
Håndkøring 1
public class
Application{
public static void
main(String args[]){
int i,j;
i=1; j=3;
while(j>0){
i=i*2;
j=j-1; •
} } }
Application.main
args = null
i=1 2
j=3 2
Håndkøring 1
public class
Application{
public static void
main(String args[]){
int i,j;
i=1; j=3;
while(j>0){
i=i*2;
j=j-1; •
} } }
Application.main
args = null
i=1 2 4
j=3 2 1
Håndkøring 1
public class
Application{
public static void
main(String args[]){
int i,j;
i=1; j=3;
while(j>0){
i=i*2;
j=j-1; •
} } }
Application.main
args = null
i=1 2 4 8
j=3 2 1 0
Metoder i klasser
• Klasser kan indeholde metoder - dvs
underprogrammer
class A{
int i,j;
void udskriv(){
System.out.println(i+”,”+j);}
}
A a = new A(); a.udskriv();
Virkefelt
• Java er blokstruktureret (som Pascal, C...)
• Navne kan genbruges i forskellige blokke
class A{
int i;
void p(){ int i; ...}
}
Klassen har felt i, metoden lokal variabel i
Funktioner
• Metoder kan have returværdi
class A{
int i,j;
int iogj(){return i+j; }
}
A a = new A();
System.out.println(a.iogj());}
Overlæsning
• Man kan have flere metoder med samme
navn – bare argumenttyper er forskellige
p(String s){...}
p(int i){ ... }
p(A a){ ...}
p(”hej”); p(3); p(new A());
Håndkøring 2
public class
Application{
public static int
f(int x,int y){
x++;
return x+y+1;
}
public static void
main(String args[]){
int i,j; •
i=1; i++;
j=f(i,4);
} }
Application.main
args = null
i=
j=
Håndkøring 2
public class
Application{
public static int
f(int x,int y){
x++;
return x+y+1;
}
public static void
main(String args[]){
int i,j;
i=1; i++; •
j=f(i,4);
} }
Application.main
args = null
i=1 2
j=
Håndkøring 2
public class
Application{
public static int
f(int x,int y){•
x++;
return x+y+1;
}
public static void
main(String args[]){
int i,j;
i=1; i++;
j=f(i,4);
} }
Application.main
args = null
i=1 2
j=
f
x=2
y=4
Håndkøring 2
public class
Application{
public static int
f(int x,int y){
x++; •
return x+y+1;
}
public static void
main(String args[]){
int i,j;
i=1; i++;
j=f(i,4);
} }
Application.main
args = null
i=1 2
j=
f
x=2 3
y=4
Håndkøring 2
public class
Application{
public static int
f(int x,int y){
x++;
return x+y+1;
}
public static void
main(String args[]){
int i,j;
i=1; i++;
j=f(i,4); •
} }
Application.main
args = null
i=1 2
j=8
f -> 8
x=2 3
y=4
Eksempel på objekter
class A{
int i,j;
}
..
A a; //a kan have hægte til obj
a = new A(); // a peger på obj.
a.i = 1; // dot-notation
Oprettelse af objekter
• Hvis felter skal initialiseres under
oprettelse kan det ske i en konstruktør
class A{
int i,j;
A(){ i = 0; j = 1; }
}
Oprettelse
• Argumenter til oprettelsen
class A{
int i,j
A(int x, int y){ i=x; j = y;}
}
....
A a = new A(2,3);
Hægter til objekter
• Variable med klasse som type er
hægter til objekter – initielt null
class A{ ... }
A a = new A();
A b = a;
a
b
A obj.
this refererer til objektet
class A{
private int i;
void setI(int i){this.i=i;}
int getI(){int i=this.i;
return i;}
void addToI(int j){i=i+j;}
}
toString metoder
Lad alle klasser have en metode:
public String toString(){ ...}
som returnerer tekstuel version af objektet
Bruges en klasse som tekststreng kaldes denne
metode:
System.out.println(”a = ”+a);
Håndkøring 3
class A{int x;}
public class Application
{ public static void
main(String args[])
{A a,b,t; •
a=new A();
b=new A();
a.x=1; b.x=2;
t=a;
a=b;
b=t
;}}
Application.main
args = null
a=
b=
t=
Håndkøring 3
class A{int x;}
public class Application
{ public static void
main(String args[])
{A a,b,t;
a=new A(); •
b=new A();
a.x=1; b.x=2;
t=a;
a=b;
b=t
;}}
Application.main
args = null
a=
b=
t=
A
X=0
Håndkøring 3
class A{int x;}
public class Application
{ public static void
main(String args[])
{A a,b,t;
a=new A();
b=new A(); •
a.x=1; b.x=2;
t=a;
a=b;
b=t
;}}
Application.main
args = null
a=
b=
t=
A
X=0
A
X=0
Håndkøring 3
class A{int x;}
public class Application
{ public static void
main(String args[])
{A a,b,t;
a=new A();
b=new A();
a.x=1; b.x=2; •
t=a;
a=b;
b=t
;}}
Application.main
args = null
a=
b=
t=
A
X=0 1
A
X=0 2
Håndkøring 3
class A{int x;}
public class Application
{ public static void
main(String args[])
{A a,b,t;
a=new A();
b=new A();
a.x=1; b.x=2;
t=a; •
a=b;
b=t
;}}
Application.main
args = null
a=
b=
t=
A
X=0 1
A
X=0 2
Håndkøring 3
class A{int x;}
public class Application
{ public static void
main(String args[])
{A a,b,t;
a=new A();
b=new A();
a.x=1; b.x=2;
t=a;
a=b; •
b=t;
}}
Application.main
args = null
a=
b=
t=
A
X=0 1
A
X=0 2
Håndkøring 3
class A{int x;}
public class Application
{ public static void
main(String args[])
{A a,b,t;
a=new A();
b=new A();
a.x=1; b.x=2;
t=a;
a=b;
b=t; •
}}
Application.main
args = null
a=
b=
t=
A
X=0 1
A
X=0 2
Metoder og parametre
class A{ int i=0; ... }
void p1(int i){i=3;}
void p2(A a){a.i=3;}
int j=2; p1(j); // j==2
A b=new A(); p2(b); // b.i==3
public/private
• Synlighed: Felter og metoder kan være
private for en klasser – usynlige udenfor
class A{
private int i,j;
public int getI(){return i;}
public void setI(int x){i=x;}
} // a.i=3 ej ok , a.setI(3) ok
Accessor/mutator
• God stil: Felter gøres private
• Værdier hentes med accessormetoder
• Felter ændres med mutatormetoder
accessor:
public int getI(){return i;}
mutator
public void setI(int x){i=x;}
Statiske felter
class A{
static private int nr=0;
A(){nr++;}
static int getNr(){return nr;}
}
System.out.println(A.getNr());
A a = new A();
System.out.println(a.getNr());
Initialisering af statiske felter
class A{
static int i;
static { i = 0; }
}
Statisk initialiseringsblok – Udføres når
programmet starter (i god tid før
objekter oprettes)
Nedarvning
class A{ int i; }
class B extends A { int j;}
//B er subtype af A
A a = new A(); a.i = 1;
B b = new B(); b.i = 1; b.j = 2;
A aa = new B(); aa.i = 1; //ej aa.j
// ej B bb = new A();
Subtyper
A a = new A(); a.i = 1;
B b = new B(); b.i = 1; b.j = 2;
A aa = new B(); aa.i = 1; //ej aa.j
b = (B) aa; // casting - typecheck
b = (B) a; // køretidsfejl
if(aa instanceof B) b = (B) aa;
//typecheck
Subtyper
A
B
a
b
a;
b = new B();
= b; // ingen casting
= (B) a // casting nødvendig
A er supertype for B, B subtype af A
Værdier af subtype må bruges som supertype
Overskrivning
class A{
void hej(){System.out.println(”AA”);} }
class B extends A{
void hej(){System.out.println(”BB”);} }
A a = new A(); a.hej(); // AA
B b = new B(); b.hej(); // BB
A c = new B(); c.hej(); // BB
I B:
void hej1(){super.hej();}
b.hej1(); // AA
((B) c).hej1(); // AA