Præsentation er lastning. Vent venligst

Præsentation er lastning. Vent venligst

Begreber og Redskaber 4. Plan for idag Om metoder, parametre, returværdier Et par ord om objekt-orientering Håndkøring af programmer.

Lignende præsentationer


Præsentationer af emnet: "Begreber og Redskaber 4. Plan for idag Om metoder, parametre, returværdier Et par ord om objekt-orientering Håndkøring af programmer."— Præsentationens transcript:

1 Begreber og Redskaber 4

2 Plan for idag Om metoder, parametre, returværdier Et par ord om objekt-orientering Håndkøring af programmer

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 j=3

4 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

5 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

6 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

7 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.i=7; a.j=3; a.udskriv(); Uddata: 7,3

8 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

9 Funktioner Metoder kan have returværdi class A{ int i,j; int iogj(){return i+j; } } A a = new A(); a.i=7; a.j=3; System.out.println(a.iogj());} Uddata: 10

10 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=

11 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=

12 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

13 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

14 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

15 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

16 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; } }

17 Oprettelse Argumenter til oprettelsen class A{ int i,j A(int x, int y){ i=x; j = y;} }.... A a = new A(2,3);

18 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.

19 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;} }

20 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=

21 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

22 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

23 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

24 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

25 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

26 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


Download ppt "Begreber og Redskaber 4. Plan for idag Om metoder, parametre, returværdier Et par ord om objekt-orientering Håndkøring af programmer."

Lignende præsentationer


Annoncer fra Google