1 / 26

Begreber og Redskaber 4

Begreber og Redskaber 4. BRP. Plan for idag. Om metoder, parametre, returværdier Et par ord om objekt-orientering Håndkøring af programmer. Håndkøring 1. Application.main args = null i=1 j=3. public class Application{ public static void main(String args[]){ int i,j;

mayes
Télécharger la présentation

Begreber og Redskaber 4

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. Begreber og Redskaber 4 BRP

  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 Application.main args = null i=1 j=3 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; } } }

  4. Håndkøring 1 Application.main args = null i=1 2 j=3 2 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; • } } }

  5. Håndkøring 1 Application.main args = null i=1 2 4 j=3 2 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; • } } }

  6. Håndkøring 1 Application.main args = null i=1 2 4 8 j=3 2 1 0 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; • } } }

  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 Application.main args = null i= j= 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); } }

  11. Håndkøring 2 Application.main args = null i=1 2 j= 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); } }

  12. Håndkøring 2 Application.main args = null i=1 2 j= 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); } } f x=2 y=4

  13. Håndkøring 2 Application.main args = null i=1 2 j= 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); } } f x=2 3 y=4

  14. Håndkøring 2 Application.main args = null i=1 2 j=8 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); • } } 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 A obj. b

  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 Application.main args = null a= b= t= 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 ;}}

  21. Håndkøring 3 Application.main args = null a= b= t= 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 ;}} A X=0

  22. Håndkøring 3 Application.main args = null a= b= t= 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 ;}} A X=0 A X=0

  23. Håndkøring 3 Application.main args = null a= b= t= 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 ;}} A X=0 1 A X=0 2

  24. Håndkøring 3 Application.main args = null a= b= t= 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 ;}} A X=0 1 A X=0 2

  25. Håndkøring 3 Application.main args = null a= b= t= 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; }} A X=0 1 A X=0 2

  26. Håndkøring 3 Application.main args = null a= b= t= 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; • }} A X=0 1 A X=0 2

More Related