OOP, null, Graphics Brooks Townsend
Value types vs Reference Types Value Types Simple data types, ex: string, number, boolean Reference Types Complex data types, ex: arrays, classes/objects
Value types vs Reference Types let x: number = 15; let y: number = x; let d: string = is a number ; x = 30; print(x + d); print(y + d); class Foo { x: number = 30; let a: Foo = new Foo(); let b: Foo = a; b.x = 50; print(a.x); print(b.x);
Classes and Objects Classes and Objects A class defines a new Data Type (with specific properties) An object is an instance of a class Every object of a class has the same properties, but different values for those properties
null A value that is not a value Think back to linked lists class Node { data: string; next: Node null;
null Other programming languages have null pointers PS04 Linked List checking for null let lastnode: Node = head; while (lastnode.next!== null) { lastnode = lastnode.next; return lastnode;
Object Oriented Programming So far, all objects have been are collections of properties What really makes objects powerful are the methods (and constructor) of an object
Methods A method is a function defined in a class Everything is the same as a function, except you no longer need the function keyword. Ex: <methodname>(<param>: <ptype>): <return type> { //Code goes here
This Methods use the this keyword to refer to the object that it was called on Class Declaration class ReviewSession { reviewer: string; whoisteaching(): string { return this.reviewer; Main method function main(): void { let rs: ReviewSession; rs = new ReviewSession(); rs.reviewer = Brooks ; let rs2: ReviewSession; rs2 = new ReviewSession(); rs2.reviewer = Jeffrey ; print(rs.whoisteaching()); print(rs2.whoisteaching());
Constructors Special type of method that is called when an object is initialized. Constructors allow us to instantiate an object with initial values of its properties. constructor(<parameters>) { // Initialize variables / class setup here
class Something { p1: string; p2: string; constructor(p1: string, p2: string) { this.p1 = p1; this.p2 = p2; tostring(): string { return this.p1 + + this.p2; let s: Something = new Something( Go, Heels! ); print(s.tostring());
tostring() Notice on the last slide, we called tostring() on an object However, if a class implements a tostring() method it is automatically called if you attempt to print an instance of that class Implicitly called, print(object.tostring()) === print(object)
class Something { p1: string; p2: string; constructor(p1: string, p2: string) { this.p1 = p1; this.p2 = p2; tostring(): string { return this.p1 + this.p1 + this.p1; let s: Something = new Something( Go, Heels! ); print(s);
Assignment operators Some shorthand syntax that is interesting x = x + 1; x ++; x = x - 1; x ; x = x + somenumber; x += somenumber; x = x * somenumber; x *= somenumber; x = x / somenumber; x /= somenumber;
Modulo When you want to get the remainder of integer division 0 % 2 == 0 1 % 2 == 1 2 % 2 == 0 3 % 2 == 1 17 % 5 == 2 22 % 21 == 1 3 % 5 == 3
Optional Parameters Parameters that do not have to be specified in the creation of an object class Circle { radius: number = 0; color: string = black ; constructor(r: number, c?: string){ this.radius = r; if (c!== undefined) { this.color = c; let c: Circle; let d: Circle; c = new Circle(15); d = new Circle(20, blue ); print(c.color); print(d.color);
Shapes let svgtag: SVG = new SVG("artboard"); let group: Group = new Group(); let axes: Axes = new Axes(50, 50); group.add(axes.getshapes()); let rectangle: Rectangle = new Rectangle(50, 50, -25, -25); group.add(rectangle); svgtag.render(group);
Object composition But what is an axes? Why axes.getshapes();? export class Axes { width: number; height: number; constructor(width: number, height: number) { this.width = width; this.height = height; getshapes(): Group { let group: Group = new Group(); let x: Rectangle = new Rectangle(100, 0.1, -50, 0); let y: Rectangle = new Rectangle(0.1, 100, 0, -50); group.add(x); group.add(y); return group;
Chaining Can happen with methods, or with properties group.children[1].transform = group.children[1].transform.rotate(45); We chain into the group object here, through 4 properties / methods
Another chaining example class Foo { bar: Bar; class Bar { baz: Baz; coolmethod(): Baz { print( cool ); return this.baz; class Baz { s: string; let foo: Foo = new Foo(); let bar: Bar = new Bar(); let baz: Baz = new Baz(); baz.s = Will this work? bar.baz = baz; foo.bar = bar; print(foo.bar.coolmethod()); // Prints // cool // Will this work? Yes!" tostring(): string { return this.s + Yes! ;