Presentation is loading. Please wait.

Presentation is loading. Please wait.

Paradigmer i Programmering 4 a. Java 1.5 -Generics -Autoboxing/unboxing -New for sætning -Static imports -enum.

Similar presentations


Presentation on theme: "Paradigmer i Programmering 4 a. Java 1.5 -Generics -Autoboxing/unboxing -New for sætning -Static imports -enum."— Presentation transcript:

1 Paradigmer i Programmering 4 a

2 Java 1.5 -Generics -Autoboxing/unboxing -New for sætning -Static imports -enum

3 Java 1.5: generics I stedet for List words = new ArrayList(); skriver man List words = new ArrayList (); Og String title = ((String) words.get(i)).toUppercase(); skriver man String title = words.get(i).toUppercase();

4 Java 1.5: generics class Box { private T val; void setValue( T val ) { this.val = val; } T getValue() { return val; } } Box stringBox = new Box (); Box intBox = new Box (); Box pointBox = new Box (); double x = pointBox.getValue().getX();

5 Java 1.5: for sætning main(String args[]){ for(String arg : args) System.out.println(arg); List words = new ArrayList (); … for(String word : words) System.out.println(word); }

6 Java 1.5: autobox/unbox Map m = new TreeMap (); for (String word : args) { m.put(word, m.get(word) + 1); }

7 Java 1.5: enum public enum Coin { penny(1), nickel(5), dime(10), quarter(25); Coin(int value) { this.value = value; } private final int value; public int value() { return value; } } public class CoinTest { public static void main(String[] args) { for (Coin c : Coin.VALUES) System.out.println(c + ": \t" + c.value() +"¢ \t" + color(c)); } private enum CoinColor { copper, nickel, silver } private static CoinColor color(Coin c) { switch(c) { case Coin.penny: return CoinColor.copper; case Coin.nickel: return CoinColor.nickel; case Coin.dime: case Coin.quarter: return CoinColor.silver; default: throw new AssertionError("Unknown coin: " + c); } } }

8 Java 1.5: static import int x = Math.max(y,z); I stedet import static java.lang.Math.max; class StatiskImport { static int max3( int a, int b, int c ) { return max( a, max(b, c) ); }

9 C#: static void Main(string[] args) { foreach (string s in args) Console.WriteLine(s); }

10 C#: properties public class Button { private string caption; public string Caption { get { return caption; } set { caption = value; Repaint(); } }} Button b = new Button(); b.Caption = "ABC"; // set; causes repaint string s = b.Caption; // get b.Caption += "DEF"; // get & set; causes repaint

11 C#: delegates void MultiCall(SimpleDelegate d, int count) { for (int i = 0; i < count; i++) { d(); } } … class Test{ static void F() { System.Console.WriteLine("Test.F"); } static void Main() { SimpleDelegate d = new SimpleDelegate(F); MultiCall(d,7); }


Download ppt "Paradigmer i Programmering 4 a. Java 1.5 -Generics -Autoboxing/unboxing -New for sætning -Static imports -enum."

Similar presentations


Ads by Google