Download presentation
Presentation is loading. Please wait.
Published byramaic Κοτζιάς Modified over 5 years ago
1
Polymorphism Sometimes it is necessary to “see” an object of a certain class as being (also) of another: Polymorphism An object can be seen as being of the finally defined class or any one of the superclasses in the object hierarchy up to the Object class This makes that different class of objects may be considered from the same class But classes can extend only one other class. Sometimes it is necessary that classes which are extensions of different other classes look like the same class This can be achieved by interfaces
2
Inheritance Object GraphicObject RectangleObject CircleObject
GraphicTest.java
3
The Drawing Methods drawLine(x1,y1,x2,y2) drawRect(x,y,w,h)
fillRect(x,y,w,h) drawOval(x,y,w,h) fillOval(x,y,w,h) w x,y h w x,y h
4
The Drawing Methods drawPolygon(x[],y[],n) fillPolygon(x[],y[],n)
setColor(color.colorname) colorname = black, blue, cyan, darkGray, gray, orange, white, red, pink, yellow setColor(new color(red,green,blue)) x[1],y[1] x[4],y[4] x[2],y[2] n = 5 x[3],y[3]
5
Drawing Strings and Changing the letters font
drawString(str, x,y) We can change the standard Font by creating a new one Font afont = new font(“Arial”,font.Bold,20) c.setFont(afont); x This is the string y These Methods are the same used for painting over any window object of Java (Frame, Jframe, Panel, etc)
6
Interfaces Vektor Clock VisualVektor VisualClock VisualVektor
INTERFACE: GraphicObjectInterface GraphicTestInterface.java
7
Reflection Reflection is the ability to “explore” an unknown class
For example: Which are the variables of a class ? Which are the methods of a class ? Which are the declared methods ? Which are the declared variables ? What do I get if I perform a certain method of the class ?
8
The Most Important Methods
Field [ ] getFields() Field [ ] getDeclaredFields() Method [ ] getMethods() Method [ ] getDeclaredMethods() Constructor [ ] getConstructors() Constructor[ ] getDeclaredConstructors() These are performed over an object of the class Class
9
How do I get a Class Object ?
Class c = Classname.class; Console.class String.class etc.. Class c = object.getClass(); Console cs = new Console(); Class c = cs.getClass(); Class c = Class.forName(String); Class c = Class.forName(“Console”); ReflectionTest ReflectionTest2 ReflectionTest3
10
Objects doing things by themselves
We will program a clock which updates itself (almost) every second For this, the clock must be of the class thread Every object of the thread class can start an independent execution line (“thread”) which will me executed in parallel to the programs of the main method
11
Making things in parallel The Thread
Main Thread1 Thread2 Thread3
12
Making things in parallel The Thread
A thread is an independent, parallel line of statements execution It consists basically of method (run) written in a special way which can be activated by another method When a method activates a thread it continues executing its own statements while the thread starts executing the statements of the method called run() There are different ways to write threads, we will see the most easy to write
13
Implementation of Threads
One way (perhaps the most clear one) is to define a new class which is an extention of the Thread class and overwrite its run() method. Thread is an existing class This thread class has a run method (originally with no statements) which will be executed in parallel when activated. In order to execute this method a program must create an object of the extended class and apply the start() method to it. The definition of the new Thread class should look like this: public class MyThread extends Thread { And somewhere should appear: public void run() { //instrucction to be executed in parallel } NOTAS
14
Example public class SimpleThread extends Thread {
public SimpleThread(String str) { super(str); } public void run() { for (int i = 0; i < 10; i++) { System.out.println(i + " " + getName()); try { this.sleep((int)(Math.random() * 1000)); } catch (InterruptedException e) {} System.out.println("DONE! " + getName()); The this.sleep(milisegundos) must appear in a try and catch block NOTAS
15
Use of this new class public class TwoThreadsTest { public static void main (String[] args) { SimpleThread t1,t2; t1 = SimpleThread("Jamaica"); t2 = SimpleThread("Fiji"); t1.start(); t2.start() } The start() triggers the execution of the run method of the thread. There are other methods which can be applied to a thread: suspend(), resume(), stop(). These are however deprecated because their use can easily lead to deadlocks NOTAS
16
The clock has a thread to advance the current time
ActiveClock Two threads
17
Sometimes you cannot program the server as an extension of a thread
For example, if the server has to implement a graphical interface it should be declared as an extension of a Frame, if it is programmed as an Applet it must extend the Applet class The we can use the interface Runnable, which means the Server will also be seen as a Runnable class and has to implement the run method run(). To initiate a parallel execution of the run method a Thread object must be created. In the creation a pointer to the server object (this) should be passed as parameter. With this, the Thread object will have access to the run method. The run method implemented in the server class will be executed by performing start() on the new created Thread object. NOTAS
18
Example with interface Runnable
See and execute the program NoSincron.java Note that the Threads created in this way will have access to all resources of the server. De la otra forma es más bien una opción, (pasar un puntero al servidor cuando se crea un objeto thread) De cualquier forma, será frecuente el compartir recursos y por lo tanto la admistración de ellos es importante NOTAS
19
Critical regions and semaphores in Java
Java provides basically two methods for controlling concurrent access to resources You can declare a whole method as a critical region (ver sincron1) You can use the semaphore of any object (ver sincron2) NOTAS
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.