?? module6.lst
字號:
listing 1
// Public vs private access.
class MyClass {
private int alpha; // private access
public int beta; // public access
int gamma; // default access (essentially public)
/* Methods to access alpha. It is OK for a
member of a class to access a private member
of the same class.
*/
void setAlpha(int a) {
alpha = a;
}
int getAlpha() {
return alpha;
}
}
class AccessDemo {
public static void main(String args[]) {
MyClass ob = new MyClass();
/* Access to alpha is allowed only through
its accessor methods. */
ob.setAlpha(-99);
System.out.println("ob.alpha is " + ob.getAlpha());
// You cannot access alpha like this:
// ob.alpha = 10; // Wrong! alpha is private!
// These are OK because beta and gamma are public.
ob.beta = 88;
ob.gamma = 99;
}
}
listing 2
/* This class implements a "fail-soft" array which prevents
runtime errors.
*/
class FailSoftArray {
private int a[]; // reference to array
private int errval; // value to return if get() fails
public int length; // length is public
/* Construct array given its size and the value to
return if get() fails. */
public FailSoftArray(int size, int errv) {
a = new int[size];
errval = errv;
length = size;
}
// Return value at given index.
public int get(int index) {
if(ok(index)) return a[index];
return errval;
}
// Put a value at an index. Return false on failure.
public boolean put(int index, int val) {
if(ok(index)) {
a[index] = val;
return true;
}
return false;
}
// Return true if index is within bounds.
private boolean ok(int index) {
if(index >= 0 & index < length) return true;
return false;
}
}
// Demonstrate the fail-soft array.
class FSDemo {
public static void main(String args[]) {
FailSoftArray fs = new FailSoftArray(5, -1);
int x;
// show quiet failures
System.out.println("Fail quietly.");
for(int i=0; i < (fs.length * 2); i++)
fs.put(i, i*10);
for(int i=0; i < (fs.length * 2); i++) {
x = fs.get(i);
if(x != -1) System.out.print(x + " ");
}
System.out.println("");
// now, handle failures
System.out.println("\nFail with error reports.");
for(int i=0; i < (fs.length * 2); i++)
if(!fs.put(i, i*10))
System.out.println("Index " + i + " out-of-bounds");
for(int i=0; i < (fs.length * 2); i++) {
x = fs.get(i);
if(x != -1) System.out.print(x + " ");
else
System.out.println("Index " + i + " out-of-bounds");
}
}
}
listing 3
// An improved queue class for characters.
class Queue {
// these members are now private
private char q[]; // this array holds the queue
private int putloc, getloc; // the put and get indices
Queue(int size) {
q = new char[size+1]; // allocate memory for queue
putloc = getloc = 0;
}
// Put a characer into the queue.
void put(char ch) {
if(putloc==q.length-1) {
System.out.println(" -- Queue is full.");
return;
}
putloc++;
q[putloc] = ch;
}
// Get a character from the queue.
char get() {
if(getloc == putloc) {
System.out.println(" -- Queue is empty.");
return (char) 0;
}
getloc++;
return q[getloc];
}
}
listing 4
// Objects can be passed to methods.
class Block {
int a, b, c;
int volume;
Block(int i, int j, int k) {
a = i;
b = j;
c = k;
volume = a * b * c;
}
// Return true if ob defines same block.
boolean sameBlock(Block ob) {
if((ob.a == a) & (ob.b == b) & (ob.c == c)) return true;
else return false;
}
// Return true if ob has same volume.
boolean sameVolume(Block ob) {
if(ob.volume == volume) return true;
else return false;
}
}
class PassOb {
public static void main(String args[]) {
Block ob1 = new Block(10, 2, 5);
Block ob2 = new Block(10, 2, 5);
Block ob3 = new Block(4, 5, 5);
System.out.println("ob1 same dimensions as ob2: " +
ob1.sameBlock(ob2));
System.out.println("ob1 same dimensions as ob3: " +
ob1.sameBlock(ob3));
System.out.println("ob1 same volume as ob3: " +
ob1.sameVolume(ob3));
}
}
listing 5
// Simple types are passed by value.
class Test {
/* This method causes no change to the arguments
used in the call. */
void noChange(int i, int j) {
i = i + j;
j = -j;
}
}
class CallByValue {
public static void main(String args[]) {
Test ob = new Test();
int a = 15, b = 20;
System.out.println("a and b before call: " +
a + " " + b);
ob.noChange(a, b);
System.out.println("a and b after call: " +
a + " " + b);
}
}
listing 6
// Objects are passed by reference.
class Test {
int a, b;
Test(int i, int j) {
a = i;
b = j;
}
/* Pass an object. Now, ob.a and ob.b in object
used in the call will be changed. */
void change(Test ob) {
ob.a = ob.a + ob.b;
ob.b = -ob.b;
}
}
class CallByRef {
public static void main(String args[]) {
Test ob = new Test(15, 20);
System.out.println("ob.a and ob.b before call: " +
ob.a + " " + ob.b);
ob.change(ob);
System.out.println("ob.a and ob.b after call: " +
ob.a + " " + ob.b);
}
}
listing 7
// Return a String object.
class ErrorMsg {
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";
}
}
class ErrMsg {
public static void main(String args[]) {
ErrorMsg err = new ErrorMsg();
System.out.println(err.getErrorMsg(2));
System.out.println(err.getErrorMsg(19));
}
}
listing 8
// Return a programmer-defined object.
class Err {
String msg; // error message
int severity; // code indicating severity of error
Err(String m, int s) {
msg = m;
severity = s;
}
}
class ErrorInfo {
String msgs[] = {
"Output Error",
"Input Error",
"Disk Full",
"Index Out-Of-Bounds"
};
int howbad[] = { 3, 3, 2, 4 };
Err getErrorInfo(int i) {
if(i >=0 & i < msgs.length)
return new Err(msgs[i], howbad[i]);
else
return new Err("Invalid Error Code", 0);
}
}
class ErrInfo {
public static void main(String args[]) {
ErrorInfo err = new ErrorInfo();
Err e;
e = err.getErrorInfo(2);
System.out.println(e.msg + " severity: " + e.severity);
e = err.getErrorInfo(19);
System.out.println(e.msg + " severity: " + e.severity);
}
}
listing 9
// Demonstrate method overloading.
class Overload {
void ovlDemo() {
System.out.println("No parameters");
}
// Overload ovlDemo for one integer parameter.
void ovlDemo(int a) {
System.out.println("One parameter: " + a);
}
// Overload ovlDemo for two integer parameters.
int ovlDemo(int a, int b) {
System.out.println("Two parameters: " + a + " " + b);
return a + b;
}
// Overload ovlDemo for two double parameters.
double ovlDemo(double a, double b) {
System.out.println("Two double parameters: " +
a + " "+ b);
return a + b;
}
}
class OverloadDemo {
public static void main(String args[]) {
Overload ob = new Overload();
int resI;
double resD;
// call all versions of ovlDemo()
ob.ovlDemo();
System.out.println();
ob.ovlDemo(2);
System.out.println();
resI = ob.ovlDemo(4, 6);
System.out.println("Result of ob.ovlDemo(4, 6): " +
resI);
System.out.println();
resD = ob.ovlDemo(1.1, 2.32);
System.out.println("Result of ob.ovlDemo(1.1, 2.2): " +
resD);
}
}
listing 10
/* Automatic type conversions can affect
overloaded method resolution.
*/
class Overload2 {
void f(int x) {
System.out.println("Inside f(int): " + x);
}
void f(double x) {
System.out.println("Inside f(double): " + x);
}
}
class TypeConv {
public static void main(String args[]) {
Overload2 ob = new Overload2();
int i = 10;
double d = 10.1;
byte b = 99;
short s = 10;
float f = 11.5F;
ob.f(i); // calls ob.f(int)
ob.f(d); // calls ob.f(double)
ob.f(b); // calls ob.f(int) -- type conversion
ob.f(s); // calls ob.f(int) -- type conversion
ob.f(f); // calls ob.f(double) -- type conversion
}
}
listing 11
// Add f(byte).
class Overload2 {
void f(byte x) {
System.out.println("Inside f(byte): " + x);
}
void f(int x) {
System.out.println("Inside f(int): " + x);
}
void f(double x) {
System.out.println("Inside f(double): " + x);
}
}
class TypeConv {
public static void main(String args[]) {
Overload2 ob = new Overload2();
int i = 10;
double d = 10.1;
byte b = 99;
short s = 10;
float f = 11.5F;
ob.f(i); // calls ob.f(int)
ob.f(d); // calls ob.f(double)
ob.f(b); // calls ob.f(byte) -- now, no type conversion
ob.f(s); // calls ob.f(int) -- type conversion
ob.f(f); // calls ob.f(double) -- type conversion
}
}
listing 12
// Demonstrate an overloaded constructor.
class MyClass {
int x;
MyClass() {
System.out.println("Inside MyClass().");
x = 0;
}
MyClass(int i) {
System.out.println("Inside MyClass(int).");
x = i;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -