Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC1351 Class 6 Classes & Inheritance.

Similar presentations


Presentation on theme: "CSC1351 Class 6 Classes & Inheritance."— Presentation transcript:

1 CSC1351 Class 6 Classes & Inheritance

2 Patterns Singleton - The Sound class. Has just one static instance.
Model View Controller Model is Board, Piece, Player, Pos View is in Ball and BallViewer Listener classes provide control

3 Polymorphism // Use dynamic lookup to allow different data types to be manipulated // with a uniform interface. public class FlameThrower implements Weapon { public void inflict(Target t) { t.fireDamage(6); } public class Sword implements Weapon { t.cuttingDamage(3); ... Weapon w = new Sword(); w.inflict(target); // Dynamic lookup of Sword's inflict method w = new FlameThrower(); w.inflict(target); // Method comes from object type, not variable type

4 Events Mouse addMouseListener() MouseListener - interface
MouseAdapter - class with methods that do nothing MouseEvent Keyboard addKeyListener() KeyListener - interface KeyAdapter - class with methods that do nothing KeyEvent

5 Input Reader - based on chars BufferedReader - wraps a reader
String readLine() FileReader - opens files for reading StringReader - reads from a string InputStreamReader - wraps in input stream InputStream - based on bytes BufferedInputStream - wraps an input stream FileInputStream - opens files for reading ObjectInputStream - read whole objects ByteArrayInputStream - read from a byte array

6 Output Writer FileWriter - create / append to files
StringWriter - create strings BufferedWriter - wrapper for a writer PrintWriter - provides println() OutputStreamWriter - write to a stream OutputStream FileOutputStream - create / append to files ByteArrayOutputStream - create a byte array BufferedOutputStream - wraper for a stream PrintStream - provides println() ObjectOutputStream - write whole objects

7 Making your own javap java.io.Reader | grep abstract
int read(char[], int, int); void close(); javap java.io.InputStream | grep abstract int read(); javap java.io.Writer | grep abstract void write(char[], int, int) void flush(); javap javai.io.OutputStream | grep abstract void write(int);

8 NullOutputStream import java.io.OutputStream;
import java.io.PrintStream; public class NullOutputStream extends OutputStream { public void write(int a) {} public static void main(String[] args) { NullOutputStream nout = new NullOutputStream(); PrintStream ps = new PrintStream(nout); ps.println("Hello, world."); }

9 CapOutputStream import java.io.OutputStream;
import java.io.PrintStream; public class CapOutputStream extends OutputStream { public void write(int a) { char c = (char)a; System.out.write(Character.toUpperCase(c)); } public static void main(String[] args) { CapOutputStream cout = new CapOutputStream(); PrintStream ps = new PrintStream(cout); ps.println("Hello, world.");


Download ppt "CSC1351 Class 6 Classes & Inheritance."

Similar presentations


Ads by Google