1 / 27

ITK:P1 Lektion 2

ITK:P1 Lektion 2. Klassarv, polymorfism och grafiska komponenter. DSV Peter Mozelius. Lektion 21. public abstract class Figur { private String färg; public Figur(){ färg = "svart"; } //defaultkonstruktor public Figur(String färg) { this.färg = färg;

vern
Télécharger la présentation

ITK:P1 Lektion 2

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. ITK:P1 Lektion 2 Klassarv, polymorfism och grafiska komponenter DSV Peter Mozelius

  2. Lektion 21 public abstract class Figur { private String färg; public Figur(){ färg = "svart"; }//defaultkonstruktor public Figur(String färg) { this.färg = färg; }//konstruktor som tar argument

  3. Lektion 21 public String visaFärg() { return färg; } public abstract double visaArea(); }//Figur

  4. Lektion 21 public class Triangel extends Figur { private double bas, höjd; public Triangel(double bas, double höjd){ super(); this.bas = bas; this.höjd = höjd; }//konstruktor som tar två argument

  5. Lektion 21 public Triangel(double bas, double höjd, String färg) { super(färg); this.bas = bas; this.höjd = höjd; }//konstruktor som tar tre argument public double visaArea() { return bas * höjd / 2; }//visaArea

  6. Lektion 21 }//Triangel

  7. Lektion 21 public class Rektangel extends Figur { private double bredd, höjd; public Rektangel(double bredd, double höjd) { super(); this.bredd = bredd; this.höjd = höjd; }//konstruktor som tar två argument

  8. Lektion 21 public Rektangel(double bredd, double höjd, String färg) { super(färg); this.bredd = bredd; this.höjd = höjd; }//konstruktor som tar tre argument public double visaArea() { return bredd * höjd; }//visaArea

  9. Lektion 21 }//Rektangel

  10. Lektion 21 public class Cirkel extends Figur { final float PI = 3.14159f; private double radie; public Cirkel(double radie) { super(); this.radie = radie; }//konstruktor som tar ett argument

  11. Lektion 21 public Cirkel(double radie, String färg) { super(färg); this.radie = radie; }//konstruktor som tar två argument public double visaArea() { return PI * radie * radie; }//visaArea

  12. Lektion 21 }//Cirkel RAST??

  13. Lektion 21 public class Lektion21 { public static void main(String[] args) { //Vi testkör cirkelklassen Figur cirkel = new Cirkel(4, "gul"); System.out.println("Cirkelns färg är: " + cirkel.visaFärg()); System.out.println("Cirkelns area är: " + cirkel.visaArea()+" ae\n");

  14. Lektion 21 //Vi testkör Rektangelklassen Figur rekt = new Rektangel(4, 4); System.out.println("Rektangelns färg är: " + rekt.visaFärg()); System.out.println("Rektangelns area är: " + rekt.visaArea()+" ae\n");

  15. Lektion 21 //Vi testkör Triangelklassen Figur tri = new Triangel(4, 1,"röd"); System.out.println("Triangelns färg är: " + tri.visaFärg()); System.out.println("Triangelns area är: " + tri.visaArea()+" ae\n"); } }

  16. Lektion 22 • Några viktiga begrepp: • Klassvariabel • Instansvariabel • Lokal variabel • Åtkomstmetod

  17. Lektion 22 • Några viktiga modifierare: • public • protected • private • final • static • abstract

  18. Lektion 23 import java.awt.*; public class Lektion23 extends Frame { private Button nordKnapp; private Button östKnapp; private Button sydKnapp; private Button västKnapp; private Button centrumKnapp;

  19. Lektion 23 public Lektion23(){ super("Lektion23"); //bestäm fönstrets storlek this.setSize(300, 300); //bestäm layoutmodell this.setLayout(new BorderLayout());

  20. Lektion 23 //skapa 5 st knappar nordKnapp = new Button("NORD"); östKnapp = new Button("ÖST"); sydKnapp = new Button("SYD"); västKnapp = new Button("VÄST"); centrumKnapp = new Button("CENTRUM");

  21. Lektion 23 //placera ut knapparna i fönstret add("North", nordKnapp); add("East", östKnapp); add("South", sydKnapp); add("West", västKnapp); add("Center", centrumKnapp);

  22. Lektion 23 //gör fönstret synligt this.setVisible(true); }//konstruktor public static void main(String[] args) { Lektion23 l23 = new Lektion23(); } }

  23. Lektion 24 import java.awt.event.*; import javax.swing.*; public class Lektion24 extends JFrame implements ActionListener { private JButton nordKnapp; private JLabel sydEtikett;

  24. Lektion 24 public Lektion24(){ //sätt titel och storlek på fönstret super("Lektion24"); setSize(300, 80); //skapa Swingkomponenter nordKnapp = new JButton("Tryck här för en hälsning"); sydEtikett = new JLabel("Här ska hälsningen hamna", JLabel.CENTER);

  25. Lektion 24 //koppla en lyssnare till knappen nordKnapp.addActionListener(this); //placera ut komponenterna getContentPane().add("North", nordKnapp); getContentPane().add("South", sydEtikett);

  26. Lektion 24 //gör fönstret synligt och stängbart setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); }//konstruktor //här tar vi emot knapptryckningar public void actionPerformed(ActionEvent e) { sydEtikett.setText("Hello Stockholm!"); }//actionPerformed

  27. Lektion 24 public static void main(String[] args) { Lektion24 l24 = new Lektion24(); } }//Lektion24 Tack för idag!!!

More Related