⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 module7.lst

📁 Mcgraw-Hill - Java 2 - A Beginner S Guide, 2Nd Ed - 2003 -prog.
💻 LST
📖 第 1 页 / 共 2 页
字号:
// Build a subclass of Vehicle for trucks. 
class Vehicle {    
  private int passengers; // number of passengers    
  private int fuelcap;    // fuel capacity in gallons   
  private int mpg;        // fuel consumption in miles per gallon   
   
  // This is a constructor for Vehicle.  
  Vehicle(int p, int f, int m) {  
    passengers = p;  
    fuelcap = f;  
    mpg = m;  
  }  
 
  // Return the range.   
  int range() {   
    return mpg * fuelcap;   
  }   
   
  // Compute fuel needed for a given distance.  
  double fuelneeded(int miles) {   
    return (double) miles / mpg;   
  } 
 
  // Access methods for instance variables. 
  int getPassengers() { return passengers; } 
  void setPassengers(int p) { passengers = p; } 
  int getFuelcap() { return fuelcap; } 
  void setFuelcap(int f) { fuelcap = f; } 
  int getMpg() { return mpg; } 
  void setMpg(int m) { mpg = m; } 
   
}    
  
// Extend Vehicle to create a Truck specialization.    
class Truck extends Vehicle {  
  private int cargocap; // cargo capacity in pounds  
  
  // This is a constructor for Truck.  
  Truck(int p, int f, int m, int c) {  
    /* Initialize Vehicle members using 
       Vehicle's constructor. */ 
    super(p, f, m);  
 
    cargocap = c;  
  }  
 
  // Accessor methods for cargocap. 
  int getCargo() { return cargocap; } 
  void putCargo(int c) { cargocap = c; } 
}  
    
class TruckDemo {    
  public static void main(String args[]) {    
  
    // construct some trucks 
    Truck semi = new Truck(2, 200, 7, 44000);    
    Truck pickup = new Truck(3, 28, 15, 2000);    
    double gallons;   
    int dist = 252;   
   
    gallons = semi.fuelneeded(dist);    
    
    System.out.println("Semi can carry " + semi.getCargo() +  
                       " pounds."); 
    System.out.println("To go " + dist + " miles semi needs " +   
                       gallons + " gallons of fuel.\n");   
       
    gallons = pickup.fuelneeded(dist);    
    
    System.out.println("Pickup can carry " + pickup.getCargo() +  
                       " pounds."); 
    System.out.println("To go " + dist + " miles pickup needs " +   
                       gallons + " gallons of fuel.");  
  }    
}

listing 10
// Create an off-road vehicle class 
class OffRoad extends Vehicle { 
  private int groundClearance; // ground clearance in inches 
 
  // ... 
}

listing 11
// A multilevel hierarchy. 
class TwoDShape { 
  private double width; 
  private double height; 
 
  // A default constructor. 
  TwoDShape() { 
    width = height = 0.0; 
  } 
 
  // Parameterized constructor. 
  TwoDShape(double w, double h) { 
    width = w; 
    height = h; 
  } 
 
  // Construct object with equal width and height. 
  TwoDShape(double x) { 
    width = height = x; 
  } 
 
  // Accessor methods for width and height. 
  double getWidth() { return width; } 
  double getHeight() { return height; } 
  void setWidth(double w) { width = w; } 
  void setHeight(double h) { height = h; } 
 
  void showDim() { 
    System.out.println("Width and height are " + 
                       width + " and " + height); 
  } 
} 
 
// Extend TwoDShape. 
class Triangle extends TwoDShape { 
  private String style; 
   
  // A default constructor. 
  Triangle() { 
    super(); 
    style = "null"; 
  } 
 
  Triangle(String s, double w, double h) { 
    super(w, h); // call superclass constructor 
 
    style = s;  
  } 
 
  // Construct an isosceles triangle. 
  Triangle(double x) { 
    super(x); // call superclass constructor 
 
    style = "isosceles";  
  } 
 
  double area() { 
    return getWidth() * getHeight() / 2; 
  } 
 
  void showStyle() { 
    System.out.println("Triangle is " + style); 
  } 
} 
 
// Extend Triangle. 
class ColorTriangle extends Triangle { 
  private String color; 
 
  ColorTriangle(String c, String s, 
                double w, double h) { 
    super(s, w, h); 
 
    color = c; 
  } 
 
  String getColor() { return color; } 
 
  void showColor() { 
    System.out.println("Color is " + color); 
  } 
} 
 
class Shapes6 { 
  public static void main(String args[]) { 
    ColorTriangle t1 =  
         new ColorTriangle("Blue", "right", 8.0, 12.0); 
    ColorTriangle t2 =  
         new ColorTriangle("Red", "isosceles", 2.0, 2.0); 
 
    System.out.println("Info for t1: "); 
    t1.showStyle(); 
    t1.showDim(); 
    t1.showColor(); 
    System.out.println("Area is " + t1.area()); 
 
    System.out.println(); 
 
    System.out.println("Info for t2: "); 
    t2.showStyle(); 
    t2.showDim(); 
    t2.showColor(); 
    System.out.println("Area is " + t2.area()); 
  } 
}

listing 12
// Demonstrate when constructors are called. 
 
// Create a super class. 
class A { 
  A() {  
    System.out.println("Constructing A."); 
  } 
} 
 
// Create a subclass by extending class A. 
class B extends A { 
  B() { 
    System.out.println("Constructing B."); 
  } 
} 
 
// Create another subclass by extending B. 
class C extends B { 
  C() { 
    System.out.println("Constructing C."); 
  } 
} 
 
class OrderOfConstruction { 
  public static void main(String args[]) {
    C c = new C(); 
  } 
}

listing 13
// This will not compile. 
class X { 
  int a; 
 
  X(int i) { a = i; } 
} 
 
class Y { 
  int a; 
 
  Y(int i) { a = i; } 
} 
 
class IncompatibleRef { 
  public static void main(String args[]) { 
    X x = new X(10); 
    X x2;  
    Y y = new Y(5); 
 
    x2 = x; // OK, both of same type 
 
    x2 = y; // Error, not of same type 
  } 
}

listing 14
// A superclass reference can refer to a subclass object. 
class X { 
  int a; 
 
  X(int i) { a = i; } 
} 
 
class Y extends X { 
  int b; 
 
  Y(int i, int j) { 
    super(j); 
    b = i; 
  } 
} 
 
class SupSubRef { 
  public static void main(String args[]) { 
    X x = new X(10); 
    X x2;  
    Y y = new Y(5, 6); 
 
    x2 = x; // OK, both of same type 
    System.out.println("x2.a: " + x2.a); 
 
    x2 = y; // still Ok because Y is derived from X 
    System.out.println("x2.a: " + x2.a); 
 
    // X references know only about X members 
    x2.a = 19; // OK 
//    x2.b = 27; // Error, X doesn't have a b member 
  } 
}

listing 15
class TwoDShape { 
  private double width; 
  private double height; 
 
  // A default constructor. 
  TwoDShape() { 
    width = height = 0.0; 
  } 
 
  // Parameterized constructor. 
  TwoDShape(double w, double h) { 
    width = w; 
    height = h; 
  } 
 
  // Construct object with equal width and height. 
  TwoDShape(double x) { 
    width = height = x; 
  } 
 
  // Construct an object from an object. 
  TwoDShape(TwoDShape ob) { 
    width = ob.width; 
    height = ob.height; 
  } 
 
  // Accessor methods for width and height. 
  double getWidth() { return width; } 
  double getHeight() { return height; } 
  void setWidth(double w) { width = w; } 
  void setHeight(double h) { height = h; } 
 
  void showDim() { 
    System.out.println("Width and height are " + 
                       width + " and " + height); 
  } 
} 
 
// A subclass of TwoDShape for triangles. 
class Triangle extends TwoDShape { 
  private String style; 
   
  // A default constructor. 
  Triangle() { 
    super(); 
    style = "null"; 
  } 
 
  // Constructor for Triangle. 
  Triangle(String s, double w, double h) { 
    super(w, h); // call superclass constructor 
 
    style = s;  
  } 
 
  // Construct an isosceles triangle. 
  Triangle(double x) { 
    super(x); // call superclass constructor 
 
    style = "isosceles";  
  } 
 
  // Construct an object from an object. 
  Triangle(Triangle ob) { 
    super(ob); // pass object to TwoDShape constructor 
    style = ob.style; 
  } 
 
  double area() { 
    return getWidth() * getHeight() / 2; 
  } 
 
  void showStyle() { 
    System.out.println("Triangle is " + style); 
  } 
} 
 
class Shapes7 { 
  public static void main(String args[]) { 
    Triangle t1 =  
         new Triangle("right", 8.0, 12.0); 
 
    // make a copy of t1 
    Triangle t2 = new Triangle(t1); 
 
    System.out.println("Info for t1: "); 
    t1.showStyle(); 
    t1.showDim(); 
    System.out.println("Area is " + t1.area()); 
 
    System.out.println(); 
 
    System.out.println("Info for t2: "); 
    t2.showStyle(); 
    t2.showDim(); 
    System.out.println("Area is " + t2.area()); 
  } 
}

listing 16
// Method overriding. 
class A { 
  int i, j; 
  A(int a, int b) { 
    i = a; 
    j = b; 
  } 
 
  // display i and j 
  void show() { 
    System.out.println("i and j: " + i + " " + j); 
  } 
} 
 
class B extends A {
  int k; 
 
  B(int a, int b, int c) { 
    super(a, b); 
    k = c; 
  } 
 
  // display k 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -