📄 chap8.lst
字号:
listing 1
// Public vs private access.
using System;
class MyClass {
private int alpha; // private access explicitly specified
int beta; // private access by default
public int gamma; // public access
/* Methods to access alpha and beta. It is OK for a
member of a class to access a private member
of the same class.
*/
public void setAlpha(int a) {
alpha = a;
}
public int getAlpha() {
return alpha;
}
public void setBeta(int a) {
beta = a;
}
public int getBeta() {
return beta;
}
}
class AccessDemo {
public static void Main() {
MyClass ob = new MyClass();
/* Access to alpha and beta is allowed only
through methods. */
ob.setAlpha(-99);
ob.setBeta(19);
Console.WriteLine("ob.alpha is " + ob.getAlpha());
Console.WriteLine("ob.beta is " + ob.getBeta());
// You cannot access alpha or beta like this:
// ob.alpha = 10; // Wrong! alpha is private!
// ob.beta = 9; // Wrong! beta is private!
// It is OK to directly access gamma because it is public.
ob.gamma = 99;
}
}
listing 2
// A stack class for characters.
using System;
class Stack {
// these members are private
char[] stck; // holds the stack
int tos; // index of the top of the stack
// Construct an empty Stack given its size.
public Stack(int size) {
stck = new char[size]; // allocate memory for stack
tos = 0;
}
// Push characters onto the stack.
public void push(char ch) {
if(tos==stck.Length) {
Console.WriteLine(" -- Stack is full.");
return;
}
stck[tos] = ch;
tos++;
}
// Pop a character from the stack.
public char pop() {
if(tos==0) {
Console.WriteLine(" -- Stack is empty.");
return (char) 0;
}
tos--;
return stck[tos];
}
// Return true if the stack is full.
public bool full() {
return tos==stck.Length;
}
// Return true if the stack is empty.
public bool empty() {
return tos==0;
}
// Return total capacity of the stack.
public int capacity() {
return stck.Length;
}
// Return number of objects currently on the stack.
public int getNum() {
return tos;
}
}
listing 3
// Demonstrate the Stack class.
using System;
class StackDemo {
public static void Main() {
Stack stk1 = new Stack(10);
Stack stk2 = new Stack(10);
Stack stk3 = new Stack(10);
char ch;
int i;
// Put some characters into stk1.
Console.WriteLine("Push A through J onto stk1.");
for(i=0; !stk1.full(); i++)
stk1.push((char) ('A' + i));
if(stk1.full()) Console.WriteLine("stk1 is full.");
// Display the contents of stk1.
Console.Write("Contents of stk1: ");
while( !stk1.empty() ) {
ch = stk1.pop();
Console.Write(ch);
}
Console.WriteLine();
if(stk1.empty()) Console.WriteLine("stk1 is empty.\n");
// put more characters into stk1
Console.WriteLine("Again push A through J onto stk1.");
for(i=0; !stk1.full(); i++)
stk1.push((char) ('A' + i));
/* Now, pop from stk1 and push the element in stk2.
This causes stk2 to hold the elements in
reverse order. */
Console.WriteLine("Now, pop chars from stk1 and push " +
"them onto stk2.");
while( !stk1.empty() ) {
ch = stk1.pop();
stk2.push(ch);
}
Console.Write("Contents of stk2: ");
while( !stk2.empty() ) {
ch = stk2.pop();
Console.Write(ch);
}
Console.WriteLine("\n");
// put 5 characters into stack
Console.WriteLine("Put 5 characters on stk3.");
for(i=0; i < 5; i++)
stk3.push((char) ('A' + i));
Console.WriteLine("Capacity of stk3: " + stk3.capacity());
Console.WriteLine("Number of objects in stk3: " +
stk3.getNum());
}
}
listing 4
// References can be passed to methods.
using System;
class MyClass {
int alpha, beta;
public MyClass(int i, int j) {
alpha = i;
beta = j;
}
/* Return true if ob contains the same values
as the invoking object. */
public bool sameAs(MyClass ob) {
if((ob.alpha == alpha) & (ob.beta == beta))
return true;
else return false;
}
// Make a copy of ob.
public void copy(MyClass ob) {
alpha = ob.alpha;
beta = ob.beta;
}
public void show() {
Console.WriteLine("alpha: {0}, beta: {1}",
alpha, beta);
}
}
class PassOb {
public static void Main() {
MyClass ob1 = new MyClass(4, 5);
MyClass ob2 = new MyClass(6, 7);
Console.Write("ob1: ");
ob1.show();
Console.Write("ob2: ");
ob2.show();
if(ob1.sameAs(ob2))
Console.WriteLine("ob1 and ob2 have the same values.");
else
Console.WriteLine("ob1 and ob2 have different values.");
Console.WriteLine();
// now, make ob1 a copy of ob2
ob1.copy(ob2);
Console.Write("ob1 after copy: ");
ob1.show();
if(ob1.sameAs(ob2))
Console.WriteLine("ob1 and ob2 have the same values.");
else
Console.WriteLine("ob1 and ob2 have different values.");
}
}
listing 5
// Value types are passed by value.
using System;
class Test {
/* This method causes no change to the arguments
used in the call. */
public void noChange(int i, int j) {
i = i + j;
j = -j;
}
}
class CallByValue {
public static void Main() {
Test ob = new Test();
int a = 15, b = 20;
Console.WriteLine("a and b before call: " +
a + " " + b);
ob.noChange(a, b);
Console.WriteLine("a and b after call: " +
a + " " + b);
}
}
listing 6
// Objects are passed by reference.
using System;
class Test {
public int a, b;
public 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. */
public void change(Test ob) {
ob.a = ob.a + ob.b;
ob.b = -ob.b;
}
}
class CallByRef {
public static void Main() {
Test ob = new Test(15, 20);
Console.WriteLine("ob.a and ob.b before call: " +
ob.a + " " + ob.b);
ob.change(ob);
Console.WriteLine("ob.a and ob.b after call: " +
ob.a + " " + ob.b);
}
}
listing 7
// Use ref to pass a value type by reference.
using System;
class RefTest {
/* This method changes its argument.
Notice the use of ref. */
public void sqr(ref int i) {
i = i * i;
}
}
class RefDemo {
public static void Main() {
RefTest ob = new RefTest();
int a = 10;
Console.WriteLine("a before call: " + a);
ob.sqr(ref a); // notice the use of ref
Console.WriteLine("a after call: " + a);
}
}
listing 8
// Swap two values.
using System;
class Swap {
// This method now changes its arguments.
public void swap(ref int a, ref int b) {
int t;
t = a;
a = b;
b = t;
}
}
class SwapDemo {
public static void Main() {
Swap ob = new Swap();
int x = 10, y = 20;
Console.WriteLine("x and y before call: " + x + " " + y);
ob.swap(ref x, ref y);
Console.WriteLine("x and y after call: " + x + " " + y);
}
}
listing 9
// Use out.
using System;
class Decompose {
/* Decompose a floating-point value into its
integer and fractional parts. */
public int parts(double n, out double frac) {
int whole;
whole = (int) n;
frac = n - whole; // pass fractional part back through frac
return whole; // return integer portion
}
}
class UseOut {
public static void Main() {
Decompose ob = new Decompose();
int i;
double f;
i = ob.parts(10.125, out f);
Console.WriteLine("Integer portion is " + i);
Console.WriteLine("Fractional part is " + f);
}
}
listing 10
// Use two out parameters.
using System;
class Num {
/* Determine if x and v have a common divisor.
If so, return least and greatest common factors in
the out parameters. */
public bool hasComFactor(int x, int y,
out int least, out int greatest) {
int i;
int max = x < y ? x : y;
bool first = true;
least = 1;
greatest = 1;
// find least and greatest common factors
for(i=2; i <= max/2 + 1; i++) {
if( ((y%i)==0) & ((x%i)==0) ) {
if(first) {
least = i;
first = false;
}
greatest = i;
}
}
if(least != 1) return true;
else return false;
}
}
class DemoOut {
public static void Main() {
Num ob = new Num();
int lcf, gcf;
if(ob.hasComFactor(231, 105, out lcf, out gcf)) {
Console.WriteLine("Lcf of 231 and 105 is " + lcf);
Console.WriteLine("Gcf of 231 and 105 is " + gcf);
}
else
Console.WriteLine("No common factor for 35 and 49.");
if(ob.hasComFactor(35, 51, out lcf, out gcf)) {
Console.WriteLine("Lcf of 35 and 51 " + lcf);
Console.WriteLine("Gcf of 35 and 51 is " + gcf);
}
else
Console.WriteLine("No common factor for 35 and 51.");
}
}
listing 11
// Swap two references.
using System;
class RefSwap {
int a, b;
public RefSwap(int i, int j) {
a = i;
b = j;
}
public void show() {
Console.WriteLine("a: {0}, b: {1}", a, b);
}
// This method changes its arguments.
public void swap(ref RefSwap ob1, ref RefSwap ob2) {
RefSwap t;
t = ob1;
ob1 = ob2;
ob2 = t;
}
}
class RefSwapDemo {
public static void Main() {
RefSwap x = new RefSwap(1, 2);
RefSwap y = new RefSwap(3, 4);
Console.Write("x before call: ");
x.show();
Console.Write("y before call: ");
y.show();
Console.WriteLine();
// exchange the objects to which x and y refer
x.swap(ref x, ref y);
Console.Write("x after call: ");
x.show();
Console.Write("y after call: ");
y.show();
}
}
listing 12
// Demonstrate params.
using System;
class Min {
public int minVal(params int[] nums) {
int m;
if(nums.Length == 0) {
Console.WriteLine("Error: no arguments.");
return 0;
}
m = nums[0];
for(int i=1; i < nums.Length; i++)
if(nums[i] < m) m = nums[i];
return m;
}
}
class ParamsDemo {
public static void Main() {
Min ob = new Min();
int min;
int a = 10, b = 20;
// call with two values
min = ob.minVal(a, b);
Console.WriteLine("Minimum is " + min);
// call with 3 values
min = ob.minVal(a, b, -1);
Console.WriteLine("Minimum is " + min);
// call with 5 values
min = ob.minVal(18, 23, 3, 14, 25);
Console.WriteLine("Minimum is " + min);
// can call with an int array, too
int[] args = { 45, 67, 34, 9, 112, 8 };
min = ob.minVal(args);
Console.WriteLine("Minimum is " + min);
}
}
listing 13
// Use regular parameter with a params parameter.
using System;
class MyClass {
public void showArgs(string msg, params int[] nums) {
Console.Write(msg + ": ");
foreach(int i in nums)
Console.Write(i + " ");
Console.WriteLine();
}
}
class ParamsDemo2 {
public static void Main() {
MyClass ob = new MyClass();
ob.showArgs("Here are some integers",
1, 2, 3, 4, 5);
ob.showArgs("Here are two more",
17, 20);
}
}
listing 14
// Return an object.
using System;
class Rect {
int width;
int height;
public Rect(int w, int h) {
width = w;
height = h;
}
public int area() {
return width * height;
}
public void show() {
Console.WriteLine(width + " " + height);
}
/* Return a rectangle that is a specified
factor larger than the invoking rectangle. */
public Rect enlarge(int factor) {
return new Rect(width * factor, height * factor);
}
}
class RetObj {
public static void Main() {
Rect r1 = new Rect(4, 5);
Console.Write("Dimensions of r1: ");
r1.show();
Console.WriteLine("Area of r1: " + r1.area());
Console.WriteLine();
// create a rectangle that is twice as big as r1
Rect r2 = r1.enlarge(2);
Console.Write("Dimensions of r2: ");
r2.show();
Console.WriteLine("Area of r2 " + r2.area());
}
}
listing 15
// Use a class factory.
using System;
class MyClass {
int a, b; // private
// Create a class factory for MyClass.
public MyClass factory(int i, int j) {
MyClass t = new MyClass();
t.a = i;
t.b = j;
return t; // return an object
}
public void show() {
Console.WriteLine("a and b: " + a + " " + b);
}
}
class MakeObjects {
public static void Main() {
MyClass ob = new MyClass();
int i, j;
// generate objects using the factory
for(i=0, j=10; i < 10; i++, j--) {
MyClass anotherOb = ob.factory(i, j); // make an object
anotherOb.show();
}
Console.WriteLine();
}
}
listing 16
// Return an array.
using System;
class Factor {
/* Return an array containing the factors of num.
On return, numfactors will contain the number of
factors found. */
public int[] findfactors(int num, out int numfactors) {
int[] facts = new int[80]; // size of 80 is arbitrary
int i, j;
// find factors and put them in the facts array
for(i=2, j=0; i < num/2 + 1; i++)
if( (num%i)==0 ) {
facts[j] = i;
j++;
}
numfactors = j;
return facts;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -