|
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs
I will give examples of objects using
class Song { String title; String artist; double length; // in seconds. boolean isCopyrighted; } |
class Robot { String id; String shape; boolean isTransformed; boolean isEvil; } |
Constructors are just functions:
You pass them arguments, and they return a value — in particular,
they return a newly-constructed object (a
Java forces you to write constructors a bit differently than most functions, though:
class Song extends Object120 { String title; String artist; double length; // in seconds. boolean isCopyrighted; // define the constructor: Song( String t, String a, double l, boolean c ) { //some voodoo (for now): super(t,a,l,c); } static void testSongs() { Song s; s = new Song( "Help!", "The Beatles", 122, true ); } } |
class Robot extends Object120 { String id; String shape; boolean isTransformed; boolean isEvil; // constructor Robot( String i, String s, boolean t, boolean e ) { super(i,s,t,e); } static void testRobots() { Robot r; r = new Robot("WX-47", "phonebox", true, false) ; } } |
to teacher: Reached this far Oct.04.
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs
©2012, Ian Barland, Radford University Last modified 2012.Oct.03 (Wed) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |