📄 chap8.lst
字号:
}
class FindFactors {
public static void Main() {
Factor f = new Factor();
int numfactors;
int[] factors;
factors = f.findfactors(1000, out numfactors);
Console.WriteLine("Factors for 1000 are: ");
for(int i=0; i < numfactors; i++)
Console.Write(factors[i] + " ");
Console.WriteLine();
}
}
listing 17
// Demonstrate method overloading.
using System;
class Overload {
public void ovlDemo() {
Console.WriteLine("No parameters");
}
// Overload ovlDemo for one integer parameter.
public void ovlDemo(int a) {
Console.WriteLine("One parameter: " + a);
}
// Overload ovlDemo for two integer parameters.
public int ovlDemo(int a, int b) {
Console.WriteLine("Two parameters: " + a + " " + b);
return a + b;
}
// Overload ovlDemo for two double parameters.
public double ovlDemo(double a, double b) {
Console.WriteLine("Two double parameters: " +
a + " "+ b);
return a + b;
}
}
class OverloadDemo {
public static void Main() {
Overload ob = new Overload();
int resI;
double resD;
// call all versions of ovlDemo()
ob.ovlDemo();
Console.WriteLine();
ob.ovlDemo(2);
Console.WriteLine();
resI = ob.ovlDemo(4, 6);
Console.WriteLine("Result of ob.ovlDemo(4, 6): " +
resI);
Console.WriteLine();
resD = ob.ovlDemo(1.1, 2.32);
Console.WriteLine("Result of ob.ovlDemo(1.1, 2.32): " +
resD);
}
}
listing 18
/* Automatic type conversions can affect
overloaded method resolution. */
using System;
class Overload2 {
public void f(int x) {
Console.WriteLine("Inside f(int): " + x);
}
public void f(double x) {
Console.WriteLine("Inside f(double): " + x);
}
}
class TypeConv {
public static void Main() {
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 19
// Add f(byte).
using System;
class Overload2 {
public void f(byte x) {
Console.WriteLine("Inside f(byte): " + x);
}
public void f(int x) {
Console.WriteLine("Inside f(int): " + x);
}
public void f(double x) {
Console.WriteLine("Inside f(double): " + x);
}
}
class TypeConv {
public static void Main() {
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 20
// Demonstrate an overloaded constructor.
using System;
class MyClass {
public int x;
public MyClass() {
Console.WriteLine("Inside MyClass().");
x = 0;
}
public MyClass(int i) {
Console.WriteLine("Inside MyClass(int).");
x = i;
}
public MyClass(double d) {
Console.WriteLine("Inside MyClass(double).");
x = (int) d;
}
public MyClass(int i, int j) {
Console.WriteLine("Inside MyClass(int, int).");
x = i * j;
}
}
class OverloadConsDemo {
public static void Main() {
MyClass t1 = new MyClass();
MyClass t2 = new MyClass(88);
MyClass t3 = new MyClass(17.23);
MyClass t4 = new MyClass(2, 4);
Console.WriteLine("t1.x: " + t1.x);
Console.WriteLine("t2.x: " + t2.x);
Console.WriteLine("t3.x: " + t3.x);
Console.WriteLine("t4.x: " + t4.x);
}
}
listing 21
// 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;
}
// Construct a Stack from a stack.
public Stack(Stack ob) {
// allocate memory for stack
stck = new char[ob.stck.Length];
// copy elements to new stack
for(int i=0; i < ob.tos; i++)
stck[i] = ob.stck[i];
// set tos for new stack
tos = ob.tos;
}
// 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;
}
}
// Demonstrate the Stack class.
class StackDemo {
public static void Main() {
Stack stk1 = 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));
// Create a copy of stck1
Stack stk2 = new Stack(stk1);
// Display the contents of stk1.
Console.Write("Contents of stk1: ");
while( !stk1.empty() ) {
ch = stk1.pop();
Console.Write(ch);
}
Console.WriteLine();
Console.Write("Contents of stk2: ");
while ( !stk2.empty() ) {
ch = stk2.pop();
Console.Write(ch);
}
Console.WriteLine("\n");
}
}
listing 22
// Demonstrate invoking a constructor through this.
using System;
class XYCoord {
public int x, y;
public XYCoord() : this(0, 0) {
Console.WriteLine("Inside XYCoord()");
}
public XYCoord(XYCoord obj) : this(obj.x, obj.y) {
Console.WriteLine("Inside XYCoord(obj)");
}
public XYCoord(int i, int j) {
Console.WriteLine("Inside XYCoord(int, int)");
x = i;
y = j;
}
}
class OverloadConsDemo {
public static void Main() {
XYCoord t1 = new XYCoord();
XYCoord t2 = new XYCoord(8, 9);
XYCoord t3 = new XYCoord(t2);
Console.WriteLine("t1.x, t1.y: " + t1.x + ", " + t1.y);
Console.WriteLine("t2.x, t2.y: " + t2.x + ", " + t2.y);
Console.WriteLine("t3.x, t3.y: " + t3.x + ", " + t3.y);
}
}
listing 23
// Display all command-line information.
using System;
class CLDemo {
public static void Main(string[] args) {
Console.WriteLine("There are " + args.Length +
" command-line arguments.");
Console.WriteLine("They are: ");
for(int i=0; i < args.Length; i++)
Console.WriteLine(args[i]);
}
}
listing 24
// Encode or decode a message.
using System;
class Cipher {
public static int Main(string[] args) {
// see if arguments are present
if(args.Length < 2) {
Console.WriteLine("Usage: encode/decode word1 [word2...wordN]");
return 1; // return failure code
}
// if args present, first arg must be encode or decode
if(args[0] != "encode" & args[0] != "decode") {
Console.WriteLine("First arg must be encode or decode.");
return 1; // return failure code
}
// encode or decode message
for(int n=1; n < args.Length; n++) {
for(int i=0; i < args[n].Length; i++) {
if(args[0] == "encode")
Console.Write((char) (args[n][i] + 1) );
else
Console.Write((char) (args[n][i] - 1) );
}
Console.Write(" ");
}
Console.WriteLine();
return 0;
}
}
listing 25
// A simple example of recursion.
using System;
class Factorial {
// This is a recursive function.
public int factR(int n) {
int result;
if(n==1) return 1;
result = factR(n-1) * n;
return result;
}
// This is an iterative equivalent.
public int factI(int n) {
int t, result;
result = 1;
for(t=1; t <= n; t++) result *= t;
return result;
}
}
class Recursion {
public static void Main() {
Factorial f = new Factorial();
Console.WriteLine("Factorials using recursive method.");
Console.WriteLine("Factorial of 3 is " + f.factR(3));
Console.WriteLine("Factorial of 4 is " + f.factR(4));
Console.WriteLine("Factorial of 5 is " + f.factR(5));
Console.WriteLine();
Console.WriteLine("Factorials using iterative method.");
Console.WriteLine("Factorial of 3 is " + f.factI(3));
Console.WriteLine("Factorial of 4 is " + f.factI(4));
Console.WriteLine("Factorial of 5 is " + f.factI(5));
}
}
listing 26
// Display a string in reverse by using recursion.
using System;
class RevStr {
// Display a string backwards.
public void displayRev(string str) {
if(str.Length > 0)
displayRev(str.Substring(1, str.Length-1));
else
return;
Console.Write(str[0]);
}
}
class RevStrDemo {
public static void Main() {
string s = "this is a test";
RevStr rsOb = new RevStr();
Console.WriteLine("Original string: " + s);
Console.Write("Reversed string: ");
rsOb.displayRev(s);
Console.WriteLine();
}
}
listing 27
// Use static.
using System;
class StaticDemo {
// a static variable
public static int val = 100;
// a static method
public static int valDiv2() {
return val/2;
}
}
class SDemo {
public static void Main() {
Console.WriteLine("Initial value of StaticDemo.val is "
+ StaticDemo.val);
StaticDemo.val = 8;
Console.WriteLine("StaticDemo.val is " + StaticDemo.val);
Console.WriteLine("StaticDemo.valDiv2(): " +
StaticDemo.valDiv2());
}
}
listing 28
class StaticError {
int denom = 3; // a normal instance variable
static int val = 1024; // a static variable
/* Error! Can't directly access a non-static variable
from within a static method. */
static int valDivDenom() {
return val/denom; // won't compile!
}
}
listing 29
using System;
class AnotherStaticError {
// non-static method.
void nonStaticMeth() {
Console.WriteLine("Inside nonStaticMeth().");
}
/* Error! Can't directly call a non-static method
from within a static method. */
static void staticMeth() {
nonStaticMeth(); // won't compile
}
}
listing 30
class MyClass {
// non-static method.
void nonStaticMeth() {
Console.WriteLine("Inside nonStaticMeth().");
}
/* Can call a non-static method through an
object reference from within a static method. */
public static void staticMeth(MyClass ob) {
ob.nonStaticMeth(); // this is OK
}
}
listing 31
// Use a static field to count instances.
using System;
class CountInst {
static int count = 0;
// increment count when object is created
public CountInst() {
count++;
}
// decrement count when object is destroyed
~CountInst() {
count--;
}
public static int getcount() {
return count;
}
}
class CountDemo {
public static void Main() {
CountInst ob;
for(int i=0; i < 10; i++) {
ob = new CountInst();
Console.WriteLine("Current count: " +
CountInst.getcount());
}
}
}
listing 32
// Use a static class factory.
using System;
class MyClass {
int a, b;
// Create a class factory for MyClass.
static 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() {
int i, j;
// generate objects using the factory
for(i=0, j=10; i < 10; i++, j--) {
MyClass ob = MyClass.factory(i, j); // get an object
ob.show();
}
Console.WriteLine();
}
}
listing 33
// Use a static constructor.
using System;
class Cons {
public static int alpha;
public int beta;
// static constructor
static Cons() {
alpha = 99;
Console.WriteLine("Inside static constructor.");
}
// instance constructor
public Cons() {
beta = 100;
Console.WriteLine("Inside instance constructor.");
}
}
class ConsDemo {
public static void Main() {
Cons ob = new Cons();
Console.WriteLine("Cons.alpha: " + Cons.alpha);
Console.WriteLine("ob.beta: " + ob.beta);
}
}
listing 34
// Demonstrate a static class.
using System;
static class NumericFn {
// Return the reciprocal of a value.
static public double reciprocal(double num) {
return 1/num;
}
// Return the fractional part of a value.
static public double fracPart(double num) {
return num - (int) num;
}
// Return true if num is even.
static public bool isEven(double num) {
return (num % 2) == 0 ? true : false;
}
// Return true of num is odd.
static public bool isOdd(double num) {
return !isEven(num);
}
}
class StaticClassDemo {
public static void Main() {
Console.WriteLine("Reciprocal of 5 is " +
NumericFn.reciprocal(5.0));
Console.WriteLine("Fractional part of 4.234 is " +
NumericFn.fracPart(4.234));
if(NumericFn.isEven(10))
Console.WriteLine("10 is even.");
if(NumericFn.isOdd(5))
Console.WriteLine("5 is odd.");
// The following attempt to create an instance of
// NumericFn will cause an error.
// NumericFn ob = new NumericFn(); // Wrong!
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -