public class Zlot { // Data of the Zlot class public int legs, eyes; // number of Zlot legs & eyes public double radius; // Zlot head radius // Methods of the Zlot class public double intelligence() { return ((3.141 * radius * radius) - eyes); } public int speed() { return legs; } } |
Zlot tim; |
Zlot tim; tim = new Zlot(); |
Zlot tim = new Zlot(); tim.eyes = 13; tim.legs = 1; tim.radius = 4; |
Zlot tim = new Zlot(); int v; double iq; tim.eyes = 13; tim.legs = 1; tim.radius = 4; v = tim.speed(); iq = tim.intelligence(); |
public double intelligence() { return ((3.141 * this.radius * this.radius) - this.eyes); } |
public class Zlot { public int legs, eyes; // number of Zlot legs & eyes public double radius; // Zlot head radius // Here's the constructor method... public Zlot (int legs, int eyes, double radius) { this.legs = legs; this.eyes = eyes; this.radius = radius; } public double intelligence() { return ((3.141 * radius * radius) - eyes); } public int speed() { return legs; } } |
tim = new Zlot(1, 13, 4.0); |
public class Zlot { public int legs, eyes; // number of Zlot legs & eyes public double radius; // Zlot head radius public static int num_Zlots = 0; // class variable: number of Zlots // Here's the constructor method again public Zlot (int legs, eyes, radius) { // Zlot initialization omitted num_Zlots++; } // Other methods omitted } |
System.out.println("Number of Zlots born:" + Zlot.num_Zlots); |
public class Zlot { public int legs, eyes; // number of Zlot legs & eyes public double radius; // Zlot head radius public static final double PI = 3.14159265358932; // Methods of the Zlot class public double intelligence() { return ((PI * radius * radius) - eyes); } public int speed() { return legs; } } |
int myFunction() { Zlot marigold = new Zlot(1, 3, 4.0); // Do stuff with marigold the Zlot // No need to delete or free marigold, // she will be collected in the garbage // (Poor marigold) return 1; } |
int myFunction() { Zlot marigold = new Zlot(1, 3, 4.0); // Do stuff with marigold the Zlot // Speed up garbage collection of marigold the Zlot // by removing the only reference to her. // (Poor marigold is garbage collected when she's forgotten) marigold = null; return 1; } |
protected void finalize() { /* stuff here */ } |
public class WingedZlot extends Zlot { // All variables and methods of the Zlot are inherited // automatically by wingedZlots so only WingedZlot // paticulars must be specified here. public int wings; public int airSpeed() { return (wings - legs); } } |
double iq = zelda.intelligence(); Zlot michael = zelda; |
public final class Clootz { ... } |
public WingedZlot(int legs, int eyes, double radius, int wings) { super (legs, eyes, radius); this.wings = wings; } |
public class WingedZlot extends Zlot { boolean legs; // shadows Zlot's variable: int legs int wings; public int airSpeed() { return (wings - super.legs); } } |
public class WingedZlot extends Zlot { boolean legs; // shadows Zlot's variable: int legs int wings; public int airSpeed() { return (wings - ((Zlot)this).legs); } } |