FAQ | This is a LIVE service | Changelog

Skip to content
Snippets Groups Projects
Commit 86d7caa1 authored by Andrew Rice's avatar Andrew Rice
Browse files

Lecture 10

parent 7b9a7e6a
No related branches found
No related tags found
No related merge requests found
package uk.ac.cam.acr31.oop.democode1920.lecture10;
public class AnonymousDemo {
public static void main(String[] args) {
Object o = new Object();
Object o2 =
new Object() {
@Override
public String toString() {
return "Not An Object";
}
};
System.out.println(o.getClass());
System.out.println(o2.getClass());
}
}
package uk.ac.cam.acr31.oop.democode1920.lecture10;
abstract class AnonymousRepeater {
abstract void doWork(int i);
void run(int count) {
for (int i = 0; i < count; i++) {
doWork(i);
}
}
public static void main(String[] args) {
int y = 3;
AnonymousRepeater r =
new AnonymousRepeater() {
@Override
void doWork(int i) {
System.out.println(i + y);
}
};
r.run(10);
AnonymousRepeater r2 =
new AnonymousRepeater() {
@Override
void doWork(int i) {
System.out.println("Not counting");
}
};
r2.run(10);
}
}
package uk.ac.cam.acr31.oop.democode1920.lecture10;
interface Repeatable {
void doWork(int i);
}
public class LambdaRepeater {
private final Repeatable repeatable;
public LambdaRepeater(Repeatable repeatable) {
this.repeatable = repeatable;
}
void run(int c) {
for (int i = 0; i < c; i++) {
repeatable.doWork(i);
}
}
public static void main(String[] args) {
LambdaRepeater r = new LambdaRepeater(System.out::println);
r.run(10);
}
}
package uk.ac.cam.acr31.oop.democode1920.lecture10;
public class Outer {
private static int y;
private int x;
static class StaticInner {
private int z;
void doStuff() {
z++;
Outer.y++;
}
}
class InstanceInner {
private int z;
void doStuff() {
z++;
x++;
}
}
InstanceInner doMoreStuff() {
StaticInner staticInner = new StaticInner();
staticInner.z++;
class MethodLocal {
void doThings() {
System.out.println(staticInner + " " + x);
}
}
MethodLocal m = new MethodLocal();
m.doThings();
return new InstanceInner();
}
public static void main(String[] args) {
Outer o = new Outer();
Outer.y = 5;
o.x = 4;
StaticInner i = new StaticInner();
i.doStuff();
InstanceInner i2 = o.doMoreStuff();
System.out.println(o.x);
i2.doStuff();
System.out.println(o.x);
}
}
package uk.ac.cam.acr31.oop.democode1920.lecture10;
import java.util.List;
class Polygon {}
class Triangle extends Polygon {}
class A {
void setShape(Triangle t) {
System.out.println("A.setShape()");
}
Polygon getShape() {
System.out.println("A.getShape()");
return new Polygon();
}
}
class B extends A {
@Override
void setShape(Triangle t) {
// ...
}
void setShape(Polygon t) {
System.out.println("B.setShape()");
}
@Override
Triangle getShape() {
System.out.println("B.getShape()");
return new Triangle();
}
}
public class Variance {
static void drawShape(Polygon shape) {
System.out.println("It's a " + shape.getClass().getSimpleName());
}
static void process(A o) {
drawShape(o.getShape());
o.setShape(new Triangle());
}
static void array() {
String[] s = new String[] {"a", "b", "c"};
Object[] o = s;
Object v = o[0];
o[1] = new Triangle(); // causes a runtime exception
}
public static void main(String[] args) {
A a = new A();
B b = new B();
process(b);
List<String> s = List.of("a", "b", "c");
List<? extends Object> o = s;
Object v = o.get(0);
// o.set(1,new Triangle()); - doesn't compile
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment