3 14 16
play

3/14/16 Review Class/Object Type Class Keyword class - PDF document

3/14/16 Review Class/Object Type Class Keyword class class Point { // Fields int x; Object Declares a new type int y; color c; Data fields ( class variables)


  1. 3/14/16 Review ¡ Class/Object ¡Type ¡ • Class ¡ • Keyword ¡ class class Point { // Fields int x; • Object ¡ • Declares ¡a ¡new ¡type ¡ int y; color c; • Data ¡fields ¡( ¡class ¡variables) ¡ // Constructor Point() { • Constructor ¡ x = 0; y = 0; • Methods ¡(class ¡func>ons) ¡ c = color(255, 255, 255); } – update/move ¡ // Methods – display/draw ¡ void update() { } void display() { noStroke(); fill(c); ellipse(x, y, 10, 10); } } this ¡Keyword ¡ Crea8ng ¡a ¡set ¡of ¡Graphic ¡Object ¡Classes ¡ • Within ¡an ¡instance ¡method, ¡ this ¡is ¡a ¡reference ¡to ¡ • All ¡have… ¡ the ¡current ¡object ¡– ¡the ¡object ¡whose ¡method ¡is ¡ being ¡called ¡ • X, ¡Y ¡loca>on ¡ • width ¡and ¡height ¡fields ¡ class Ball { • fill ¡and ¡stroke ¡colors ¡ // Fields int w; int h; // width and height of ball • A ¡display() ¡method ¡ float x; // x position • An ¡update() ¡method ¡defining ¡how ¡they ¡move ¡ float y; // y position // ... • Implementa>on ¡varies ¡from ¡class ¡to ¡class ¡ // Constructor Ball(int x, int y) { w = h = 20; this.x = x; this.y = y; } // ... } Ball b1 = new Ball(0, 0); Ball b2 = new Ball(20, 20); Crea8ng ¡a ¡set ¡of ¡Graphic ¡Object ¡Classes ¡ Graphic Object Hierarchy • Problems ¡ X,Y fields Graphic ¡ display() method … How ¡would ¡you ¡hold ¡all ¡your ¡objects? ¡ More Specific More General – Array? ¡ Rectangle ¡ Ellipse ¡ Triangle ¡ Arc ¡ Curve ¡ What ¡if ¡one ¡class ¡had ¡extra ¡methods ¡or ¡special ¡arguments? ¡ diameter Square ¡ Circle ¡ Inheritance gives you a way to relate your objects in a hierarchical manner 1

  2. 3/14/16 // Ellipse base class // Circle derived class class Ellipse { class Circle extends Ellipse { Inheritance ¡ float X; float Y; Circle(float X, float Y, float D) { float W; super (X, Y, D, D); • Superclass ¡(parent ¡class) ¡– ¡higher ¡in ¡the ¡hierarchy ¡ float H; // Circles are always green • Subclass ¡(child ¡class) ¡– ¡lower ¡in ¡the ¡hierarchy ¡ // Ellipses are always red fillColor = color(0,255,0); color fillColor = } } • A ¡subclass ¡is ¡ derived ¡from ¡ from ¡a ¡superclass ¡ color(255,0,0); • Subclasses ¡ inherit ¡the ¡ fields ¡and ¡ methods ¡of ¡their ¡ Ellipse(float X, float Y, • The ¡ extends ¡keyword ¡creates ¡ float W, float H){ hierarchical ¡rela>onship ¡between ¡ superclass. ¡ this .X = X; classes. ¡ this .Y = Y; – I.e. ¡subclasses ¡automa>cally ¡ "get" ¡ stuff ¡in ¡superclasses ¡ this .W = W; • The ¡Circle ¡class ¡gets ¡all ¡fields ¡and ¡ this .H = H; • Subclasses ¡can ¡ override ¡a ¡superclass ¡method ¡by ¡ methods ¡of ¡the ¡Ellipse ¡class, ¡ } automa>cally. ¡ redefining ¡it. ¡ void display() { • The ¡ super ¡keyword ¡refers ¡to ¡the ¡base ¡ ellipseMode(CENTER); – They ¡can ¡replace ¡anything ¡by ¡redefining ¡locally ¡ fill(fillColor); class ¡in ¡the ¡rela>onship. ¡ ellipse(X, Y, W, H); } • The ¡ this ¡keyword ¡refers ¡to ¡the ¡object ¡ } itself. ¡ Graphics.pde // Graphics // Graphics2 Ellipse e = new Ellipse(150, 250, 150, 50); Ellipse[] es = new Ellipse[20]; Circle c = new Circle(350, 250, 75); void setup() { size(500, 500); void setup() { size(500, 500); for (int i=0; i< es.length; i++) { } float X = random(0, width); float Y = random(0, height); void draw() { float W = random(10, 100); e.display(); float H = random(10, 100); c.display(); // Ellipses and Circles are } // stored in the same array if (random(1.0) < 0.5) es[i] = new Ellipse(X,Y,W,H); else es[i] = new Circle(X,Y,W); } } void draw() { for (int i=0; i<es.length; i++) es[i].display(); } Ellipses and Circles in the same array! Graphics2.pde Graphics.pde // Circle derived class // Ellipse base class class Ellipse { class Circle extends Ellipse { float X; // Graphics3 float Y; Circle(float X, float Y, float D) { Ellipse[] es = new Ellipse[20]; float W; super(X, Y, D, D); float H; // Circles are always green void setup() { // Ellipses are always red fillColor = color(0,255,0); size(500, 500); color fillColor = } //code now shown … color(255,0,0); } // Change color of circle when clicked void mousePressed() { Ellipse(float X, float Y, if (dist(mouseX, mouseY, X, Y) < 0.5*W) void draw() { float W, float H){ fillColor = color(0,0,255); for (int i=0; i<es.length; i++) this.X = X; this.Y = Y; } es[i].display(); this.W = W; } } this.H = H; } void mousePressed() { • The ¡mousePressed ¡behavior ¡of ¡the ¡ for (int i=0; i<es.length; i++) void display() { es[i].mousePressed(); ellipseMode(CENTER); Circle ¡class ¡ overrides ¡the ¡default ¡ fill(fillColor); } behavior ¡of ¡the ¡Ellipse ¡class. ¡ ellipse(X, Y, W, H); } // Do nothing void mousePressed() {} } Graphics3.pde Graphics3.pde 2

  3. 3/14/16 Example ¡ A ¡few ¡more ¡rules ¡about ¡inheritance ¡… ¡ • ballDropInheritance ¡ • A ¡child’s ¡constructor ¡is ¡responsible ¡for ¡calling ¡the ¡ parent’s ¡constructor ¡ • The ¡first ¡line ¡of ¡a ¡child’s ¡constructor ¡should ¡use ¡the ¡ super ¡reference ¡to ¡call ¡the ¡parent’s ¡constructor ¡ • The ¡ super ¡reference ¡can ¡also ¡be ¡used ¡to ¡reference ¡ other ¡variables ¡and ¡methods ¡defined ¡in ¡the ¡parent’s ¡ class ¡ 3

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend