Download moving from c to c++

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
// A simple class hierarchy.
// A class for two-dimensional objects.
class TwoDShape {
double width;
double height;
void showDim() {
System.out.println(″Width and height are ″ +
width + ″ and ″ + height);
}
}
// A subclass of TwoDShape for triangles.
class Triangle extends TwoDShape {
String style;
double area() {
return width * height / 2;
}
Triangle 继承了 TwoDSape
Triangle 可以引用 TwoDShape 的成员,
就像是它属于 Triangle 一样
void showStyle() {
System.out.println(″Triangle is ″ + style);
}
}
class Shapes {
public static void main(String args[]) {
Triangle t1 = new Triangle();
Triangle t2 = new Triangle();
t1.width = 4.0;
t1.height = 4.0;
t1.style = ″isosceles ″;
Triangle 的所有成员对 Triangle 对象都可用,即使
是那些从 TwoDShape 继承的对象
t2.width = 8.0;
t2.height = 12.0;
t2.style = ″right ″;
System.out.println(″Info for t1: ″);
t1.showStyle();
t1.showDim();
System.out.println(″Area is ″ + t1.area());
System.out.println();
216
Java 2 实用教程
System.out.println(″Info for t2: ″);
t2.showStyle();
t2.showDim();
System.out.println(″Area is ″ + t2.area());
}
}
Info for t1:
Triangle is isosceles
Width and height are 4.0 and 4.0
Area is 8.0
Info for t2:
Triangle is right
Width and height are 8.0 and 12.0
Area is 48.0
TwoDShape shape = new TwoDShape();
shape.width = 10;
shape.height = 20;
shape.showDim();
class subclass-name extends superclass-name {
// body of class
}
第7章
// A subclass of TwoDShape for rectangles.
class Rectangle extends TwoDShape {
boolean isSquare() {
if(width == height) return true;
return false;
}
double area() {
return width * height;
}
}
// Private members are not inherited.
// This example will not compile.
// A class for two-dimensional objects.
class TwoDShape {
private double width; // these are
private double height; // now private
void showDim() {
System.out.println(″Width and height are ″ +
width + ″ and ″ + height);
}
}
// A subclass of TwoDShape for triangles.
class Triangle extends TwoDShape {
String style;
不能访问超类的私有成员
double area() {
return width * height / 2; // Error! can′t access
}
void showStyle() {
System.out.println(″Triangle is ″ + style);
}
}
继承
217
218
Java 2 实用教程
// Use accessor methods to set and get private members.
// A class for two-dimensional objects.
class TwoDShape {
private double width; // these are
private double height; // now private
// 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; }
width 和 height 的访问器方法
void showDim() {
System.out.println(″Width and height are ″ +
width + ″ and ″ + height);
}
}
// A subclass of TwoDShape for triangles.
class Triangle extends TwoDShape {
String style;
使用超类提供的访问器方法
double area() {
return getWidth() * getHeight() / 2;
}
void showStyle() {
System.out.println(″Triangle is ″ + style);
}
}
class Shapes2 {
public static void main(String args[]) {
Triangle t1 = new Triangle();
Triangle t2 = new Triangle();
t1.setWidth(4.0);
t1.setHeight(4.0);
t1.style = ″isosceles ″;
t2.setWidth(8.0);
t2.setHeight(12.0);
第7章
t2.style = ″right ″;
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());
}
}
// Add a constructor to Triangle.
// A class for two-dimensional objects.
class TwoDShape {
private double width; // these are
private double height; // now private
// 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;
// Constructor
Triangle(String s, double w, double h) {
setWidth(w);
初始化对象的 TwoDShape
setHeight(h);
继承
219
220
Java 2 实用教程
style = s;
}
double area() {
return getWidth() * getHeight() / 2;
}
void showStyle() {
System.out.println(″Triangle is ″ + style);
}
}
class Shapes3 {
public static void main(String args[]) {
Triangle t1 = new Triangle(″isosceles ″, 4.0, 4.0);
Triangle t2 = new Triangle(″right ″, 8.0, 12.0);
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());
}
}
super(parameter-list);
// Add constructors to TwoDShape.
class TwoDShape {
private double width;
第7章
private double height;
// Parameterized constructor.
TwoDShape(double w, double h) {
width = w;
height = h;
}
// 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;
Triangle(String s, double w, double h) {
super(w, h); // call superclass constructor
style = s;
}
使用 super() 执行
TwoDShape 构造函数
double area() {
return getWidth() * getHeight() / 2;
}
void showStyle() {
System.out.println(″Triangle is ″ + style);
}
}
class Shapes4 {
public static void main(String args[]) {
Triangle t1 = new Triangle(″isosceles ″, 4.0, 4.0);
Triangle t2 = new Triangle(″right ″, 8.0, 12.0);
System.out.println(″Info for t1: ″);
t1.showStyle();
继承
221
222
Java 2 实用教程
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());
}
}
// Add more constructors to TwoDShape.
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 ″ +
第7章
继承
width + ″ and ″ + height);
}
}
// A subclass of TwoDShape for triangles.
class Triangle extends TwoDShape {
private String style;
// A default constructor.
Triangle() {
super();
style = ″null ″;
}
// Constructor
Triangle(String s, double w, double h) {
super(w, h); // call superclass constructor
style = s;
使用 super()调用 TwoDShape 的各种
形式
}
// 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);
}
}
class Shapes5 {
public static void main(String args[]) {
Triangle t1 = new Triangle();
Triangle t2 = new Triangle(″right ″, 8.0, 12.0);
Triangle t3 = new Triangle(4.0);
t1 = t2;
System.out.println(″Info for t1: ″);
223
224
Java 2 实用教程
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());
System.out.println();
System.out.println(″Info for t3: ″);
t3.showStyle();
t3.showDim();
System.out.println(″Area is ″ + t3.area());
System.out.println();
}
}
Info for t1:
Triangle is right
Width and height are 8.0 and 12.0
Area is 48.0
Info for t2:
Triangle is right
Width and height are 8.0 and 12.0
Area is 48.0
Info for t3:
Triangle is isosceles
Width and height are 4.0 and 4.0
Area is 8.0
// Using super to overcome name hiding.
第7章
class A {
int i;
}
// Create a subclass by extending class A.
class B extends A {
int i; // this i hides the i in A
B(int a, int b) {
super.i = a; // i in A
i = b; // i in B
}
这里, super.i 引用了 A 中的 i
void show() {
System.out.println(″i in superclass: ″ + super.i);
System.out.println(″i in subclass: ″ + i);
}
}
class UseSuper {
public static void main(String args[]) {
B subOb = new B(1, 2);
subOb.show();
}
}
i in superclass: 1
i in subclass: 2
// 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
继承
225
226
Java 2 实用教程
Vehicle′s constructor. */
super(p, f, m);
cargocap = c;
}
// Accessor methods for cargocap.
int getCargo() { return cargocap; }
void putCargo(int c) { cargocap = c; }
}
private int passengers; // number of passengers
private int fuelcap;
// fuel capacity in gallons
private int mpg;
// fuel consumption in miles per gallon
// 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;
}
第7章
// 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);
继承
227
228
Java 2 实用教程
System.out.println(″Pickup can carry ″ + pickup.getCargo() +
″ pounds. ″);
System.out.println(″To go ″ + dist + ″ miles pickup needs ″ +
gallons + ″ gallons of fuel. ″);
}
}
Semi can carry 44000 pounds.
To go 252 miles semi needs 36.0 gallons of fuel.
Pickup can carry 2000 pounds.
To go 252 miles pickup needs 16.8 gallons of fuel.
// Create an off-road vehicle class
class OffRoad extends Vehicle {
private int groundClearance; // ground clearance in inches
// ...
}
// 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) {
第7章
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;
继承
229
230
Java 2 实用教程
}
void showStyle() {
System.out.println(″Triangle is ″ + style);
}
}
// Extend Triangle.
class ColorTriangle extends Triangle {
private String color;
ColorTriangle 继承了 Triangle,它派生自
TwoDShape , 因 此 ColorTriangle 包 含 了
ColorTriangle(String c, String s,
Triangle 和 TwoDShape 的所有成员
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();
ColorTriangle 对象可以调用由它自身及其超类定
t2.showDim();
义的方法
t2.showColor();
System.out.println(″Area is ″ + t2.area());
}
第7章
}
Info for t1:
Triangle is right
Width and height are 8.0 and 12.0
Color is Blue
Area is 48.0
Info for t2:
Triangle is isosceles
Width and height are 2.0 and 2.0
Color is Red
Area is 2.0
// 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. ″);
}
}
继承
231
232
Java 2 实用教程
class OrderOfConstruction {
public static void main(String args[]) {
C c = new C();
}
}
Constructing A.
Constructing B.
Constructing C.
// 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
}
}
第7章
继承
// 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);
可以,因为 Y 是 X 的子类,因此 x2 可
以引用 Y
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
}
}
class TwoDShape {
private double width;
private double height;
// A default constructor.
233
234
Java 2 实用教程
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
第7章
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;
把 Triangle 引用传递给 TwoDShape 的构造
}
函数
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());
继承
235
236
Java 2 实用教程
}
}
Info for t1:
Triangle is right
Width and height are 8.0 and 12.0
Area is 48.0
Info for t2:
Triangle is right
Width and height are 8.0 and 12.0
Area is 48.0
// Construct an object from an object.
Triangle(Triangle ob) {
super(ob); // pass object to TwoDShape constructor
style = ob.style;
}
// Construct an object from an object.
TwoDShape(TwoDShape ob) {
width = ob.width;
height = ob.height;
}
第7章
继承
// 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 – this overrides show() in A
B 中的 show() 重写了 A 中定义的 show()
void show() {
System.out.println(″k: ″ + k);
}
}
class Override {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
subOb.show(); // this calls show() in B
}
}
k: 3
237
238
Java 2 实用教程
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
使用 super 调用超类 A 定义的 show()版本
}
void show() {
super. show(); // this calls A′s show()
System.out.println(″k: ″ + k);
}
}
:
i and j: 1 2
k: 3
/* Methods with differing type signatures are
overloaded and not overridden. */
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);
}
}
// Create a subclass by extending class A.
class B extends A {
第7章
int k;
B(int a, int b, int c) {
super(a, b);
k = c; }
由于签名不同,这个 show() 只是重载了超
类 A 中的 show()
// overload show()
void show(String msg) {
System.out.println(msg + k);
}
}
class Overload {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
subOb.show(″This is k: ″); // this calls show() in B
subOb.show(); // this calls show() in A
}
}
This is k: 3
i and j: 1 2
// Demonstrate dynamic method dispatch.
class Sup {
void who() {
System.out.println(″who() in Sup ″);
}
}
class Sub1 extends Sup {
void who() {
System.out.println(″who() in Sub1 ″);
}
}
继承
239
240
Java 2 实用教程
class Sub2 extends Sup {
void who() {
System.out.println(″who() in Sub2 ″);
}
}
class DynDispDemo {
public static void main(String args[]) {
Sup superOb = new Sup();
Sub1 subOb1 = new Sub1();
Sub2 subOb2 = new Sub2();
Sup supRef;
supRef = superOb;
supRef.who();
supRef = subOb1;
supRef.who();
supRef = subOb2;
supRef.who();
在每一种情况
下,调用的
who() 的 版 本
都是在运行时
通过所引用的
对象类型确定
的
}
}
who() in Sup
who() in Sub1
who() in Sub2
// Use dynamic method dispatch.
class TwoDShape {
private double width;
private double height;
private String name;
// A default constructor.
第7章
TwoDShape() {
width = height = 0.0;
name = ″null ″;
}
// Parameterized constructor.
TwoDShape(double w, double h, String n) {
width = w;
height = h;
name = n;
}
// Construct object with equal width and height.
TwoDShape(double x, String n) {
width = height = x;
name = n;
}
// Construct an object from an object.
TwoDShape(TwoDShape ob) {
width = ob.width;
height = ob.height;
name = ob.name;
}
// 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; }
String getName() { return name; }
void showDim() {
System.out.println(″Width and height are ″ +
width + ″ and ″ + height);
}
TwoDShape 定义 area()方法
double area() {
System.out.println(″area() must be overridden ″);
return 0.0;
}
}
// A subclass of TwoDShape for triangles.
class Triangle extends TwoDShape {
继承
241
242
Java 2 实用教程
private String style;
// A default constructor.
Triangle() {
super();
style = ″null ″;
}
// Constructor for Triangle.
Triangle(String s, double w, double h) {
super(w, h, ″triangle ″);
style = s;
}
// Construct an isosceles triangle.
Triangle(double x) {
super(x, ″triangle ″); // call superclass constructor
style = ″isosceles ″;
}
// Construct an object from an object.
Triangle(Triangle ob) {
super(ob); // pass object to TwoDShape constructor
style = ob.style;
}
// Override area() for Triangle.
double area() {
为 Triangle 重写 area()
return getWidth() * getHeight() / 2;
}
void showStyle() {
System.out.println(″Triangle is ″ + style);
}
}
// A subclass of TwoDShape for rectangles.
class Rectangle extends TwoDShape {
// A default constructor.
Rectangle() {
super();
}
// Constructor for Rectangle.
Rectangle(double w, double h) {
第7章
继承
243
super(w, h, ″rectangle ″); // call superclass constructor
}
// Construct a square.
Rectangle(double x) {
super(x, ″rectangle ″); // call superclass constructor
}
// Construct an object from an object.
Rectangle(Rectangle ob) {
super(ob); // pass object to TwoDShape constructor
}
boolean isSquare() {
if(getWidth() == getHeight()) return true;
return false;
}
// Override area() for Rectangle.
为 Rectangle 重写 area()
double area() {
return getWidth() * getHeight();
}
}
class DynShapes {
public static void main(String args[]) {
TwoDShape shapes[] = new TwoDShape[5];
shapes[0]
shapes[1]
shapes[2]
shapes[3]
shapes[4]
=
=
=
=
=
new
new
new
new
new
Triangle(″right ″, 8.0, 12.0);
Rectangle(10);
Rectangle(10, 4);
Triangle(7.0);
TwoDShape(10, 20, ″generic ″);
为每一种形状调用正确的
area()版本
for(int i=0; i < shapes.length; i++) {
System.out.println(″object is ″ + shapes[i].getName());
System.out.println(″Area is ″ + shapes[i].area());
System.out.println();
}
}
}
244
Java 2 实用教程
object is triangle
Area is 48.0
object is rectangle
Area is 100.0
object is rectangle
Area is 40.0
object is triangle
Area is 24.5
object is generic
area() must be overridden
Area is 0.0
abstract type name(parameter-list);
// Create an abstract class.
abstract class TwoDShape {
private double width;
private double height;
private String name;
TwoDShape 现在是抽象类
// A default constructor.
TwoDShape() {
width = height = 0.0;
name = ″null ″;
}
// Parameterized constructor.
TwoDShape(double w, double h, String n) {
width = w;
height = h;
name = n;
第7章
}
// Construct object with equal width and height.
TwoDShape(double x, String n) {
width = height = x;
name = n;
}
// Construct an object from an object.
TwoDShape(TwoDShape ob) {
width = ob.width;
height = ob.height;
name = ob.name;
}
// 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; }
String getName() { return name; }
void showDim() {
System.out.println(″Width and height are ″ +
width + ″ and ″ + height);
}
// Now, area() is abstract.
abstract double area();
使 area() 成为抽象方法
}
// 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, ″triangle ″);
继承
245
246
Java 2 实用教程
style = s;
}
// Construct an isosceles triangle.
Triangle(double x) {
super(x, ″triangle ″); // 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);
}
}
// A subclass of TwoDShape for rectangles.
class Rectangle extends TwoDShape {
// A default constructor.
Rectangle() {
super();
}
// Constructor for Rectangle.
Rectangle(double w, double h) {
super(w, h, ″rectangle ″); // call superclass constructor
}
// Construct a square.
Rectangle(double x) {
super(x, ″rectangle ″); // call superclass constructor
}
// Construct an object from an object.
第7章
Rectangle(Rectangle ob) {
super(ob); // pass object to TwoDShape constructor
}
boolean isSquare() {
if(getWidth() == getHeight()) return true;
return false;
}
double area() {
return getWidth() * getHeight();
}
}
class AbsShape {
public static void main(String args[]) {
TwoDShape shapes[] = new TwoDShape[4];
shapes[0]
shapes[1]
shapes[2]
shapes[3]
=
=
=
=
new
new
new
new
Triangle(″right ″, 8.0, 12.0);
Rectangle(10);
Rectangle(10, 4);
Triangle(7.0);
for(int i=0; i < shapes.length; i++) {
System.out.println(″object is ″ +
shapes[i].getName());
System.out.println(″Area is ″ + shapes[i].area());
System.out.println();
}
}
}
class A {
final void meth() {
System.out.println(″This is a final method. ″);
}
}
继承
247
248
Java 2 实用教程
class B extends A {
void meth() { // ERROR! Can′t override.
System.out.println(″Illegal! ″);
}
}
final class A {
// ...
}
// The following class is illegal.
class B extends A { // ERROR! Can′t subclass A
// ...
}
// Return a String object.
class ErrorMsg {
// Error codes.
final int OUTERR
= 0;
final int INERR
= 1;
final int DISKERR = 2;
final int INDEXERR = 3;
声明 final 常量
String msgs[] = {
″Output Error ″,
″Input Error ″,
″Disk Full ″,
″Index Out-Of-Bounds ″
};
// Return the error message.
String getErrorMsg(int i) {
if(i >=0 & i < msgs.length)
return msgs[i];
else
return ″Invalid Error Code ″;
}
}
第7章
class FinalD {
public static void main(String args[]) {
ErrorMsg err = new ErrorMsg();
System.out.println(err.getErrorMsg(err.OUTERR));
System.out.println(err.getErrorMsg(err.DISKERR));
}
}
class Alpha { ...
class Beta extends Alpha { ...
Class Gamma extends Beta { ...
继承
249
Related documents