📄 chap12.lst
字号:
}
interface IMyIF_B {
int meth(int x);
}
// MyClass implements both interfaces.
class MyClass : IMyIF_A, IMyIF_B {
// explicitly implement the two meth()s
int IMyIF_A.meth(int x) {
return x + x;
}
int IMyIF_B.meth(int x) {
return x * x;
}
// call meth() through an interface reference.
public int methA(int x){
IMyIF_A a_ob;
a_ob = this;
return a_ob.meth(x); // calls IMyIF_A
}
public int methB(int x){
IMyIF_B b_ob;
b_ob = this;
return b_ob.meth(x); // calls IMyIF_B
}
}
class FQIFNames {
public static void Main() {
MyClass ob = new MyClass();
Console.Write("Calling IMyIF_A.meth(): ");
Console.WriteLine(ob.methA(3));
Console.Write("Calling IMyIF_B.meth(): ");
Console.WriteLine(ob.methB(3));
}
}
listing 14
// An encryption interface.
public interface ICipher {
string encode(string str);
string decode(string str);
}
listing 15
/* A simple implementation of ICipher that codes
a message by shifting each character 1 position
higher. Thus, A becomes B, and so on. */
class SimpleCipher : ICipher {
// Return an encoded string given plaintext.
public string encode(string str) {
string ciphertext = "";
for(int i=0; i < str.Length; i++)
ciphertext = ciphertext + (char) (str[i] + 1);
return ciphertext;
}
// Return a decoded string given ciphertext.
public string decode(string str) {
string plaintext = "";
for(int i=0; i < str.Length; i++)
plaintext = plaintext + (char) (str[i] - 1);
return plaintext;
}
}
/* This implementation of ICipher uses bit
manipulations and key. */
class BitCipher : ICipher {
ushort key;
// Specify a key when constructing BitCiphers.
public BitCipher(ushort k) {
key = k;
}
// Return an encoded string given plaintext.
public string encode(string str) {
string ciphertext = "";
for(int i=0; i < str.Length; i++)
ciphertext = ciphertext + (char) (str[i] ^ key);
return ciphertext;
}
// Return adecoded string given ciphertext.
public string decode(string str) {
string plaintext = "";
for(int i=0; i < str.Length; i++)
plaintext = plaintext + (char) (str[i] ^ key);
return plaintext;
}
}
listing 16
// Demonstrate ICipher.
using System;
class ICipherDemo {
public static void Main() {
ICipher ciphRef;
BitCipher bit = new BitCipher(27);
SimpleCipher sc = new SimpleCipher();
string plain;
string coded;
// first, ciphRef refers to the simple cipher
ciphRef = sc;
Console.WriteLine("Using simple cipher.");
plain = "testing";
coded = ciphRef.encode(plain);
Console.WriteLine("Cipher text: " + coded);
plain = ciphRef.decode(coded);
Console.WriteLine("Plain text: " + plain);
// now, let ciphRef refer to the bitwise cipher
ciphRef = bit;
Console.WriteLine("\nUsing bitwise cipher.");
plain = "testing";
coded = ciphRef.encode(plain);
Console.WriteLine("Cipher text: " + coded);
plain = ciphRef.decode(coded);
Console.WriteLine("Plain text: " + plain);
}
}
listing 17
// Use ICipher.
using System;
// A class for storing unlisted telephone numbers.
class UnlistedPhone {
string pri_name; // supports name property
string pri_number; // supports number property
ICipher crypt; // reference to encryption object
public UnlistedPhone(string name, string number, ICipher c)
{
crypt = c; // store encryption object
pri_name = crypt.encode(name);
pri_number = crypt.encode(number);
}
public string Name {
get {
return crypt.decode(pri_name);
}
set {
pri_name = crypt.encode(value);
}
}
public string Number {
get {
return crypt.decode(pri_number);
}
set {
pri_number = crypt.encode(value);
}
}
}
// Demonstrate UnlistedPhone
class UnlistedDemo {
public static void Main() {
UnlistedPhone phone1 =
new UnlistedPhone("Tom", "555-3456", new BitCipher(27));
UnlistedPhone phone2 =
new UnlistedPhone("Mary", "555-8891", new BitCipher(9));
Console.WriteLine("Unlisted number for " +
phone1.Name + " is " +
phone1.Number);
Console.WriteLine("Unlisted number for " +
phone2.Name + " is " +
phone2.Number);
}
}
listing 18
// This version uses SimpleCipher.
class UnlistedDemo {
public static void Main() {
// now, use SimpleCipher rather than BitCipher
UnlistedPhone phone1 =
new UnlistedPhone("Tom", "555-3456", new SimpleCipher());
UnlistedPhone phone2 =
new UnlistedPhone("Mary", "555-8891", new SimpleCipher());
Console.WriteLine("Unlisted number for " +
phone1.Name + " is " +
phone1.Number);
Console.WriteLine("Unlisted number for " +
phone2.Name + " is " +
phone2.Number);
}
}
listing 19
// Demonstrate a structure.
using System;
// Define a structure.
struct Book {
public string author;
public string title;
public int copyright;
public Book(string a, string t, int c) {
author = a;
title = t;
copyright = c;
}
}
// Demonstrate Book structure.
class StructDemo {
public static void Main() {
Book book1 = new Book("Herb Schildt",
"C#: The Complete Reference",
2005); // explicit constructor
Book book2 = new Book(); // default constructor
Book book3; // no constructor
Console.WriteLine(book1.title + " by " + book1.author +
", (c) " + book1.copyright);
Console.WriteLine();
if(book2.title == null)
Console.WriteLine("book2.title is null.");
// now, give book2 some info
book2.title = "Brave New World";
book2.author = "Aldous Huxley";
book2.copyright = 1932;
Console.Write("book2 now contains: ");
Console.WriteLine(book2.title + " by " + book2.author +
", (c) " + book2.copyright);
Console.WriteLine();
// Console.WriteLine(book3.title); // error, must initialize first
book3.title = "Red Storm Rising";
Console.WriteLine(book3.title); // now OK
}
}
listing 20
// Copy a struct.
using System;
// Define a structure.
struct MyStruct {
public int x;
}
// Demonstrate structure assignment.
class StructAssignment {
public static void Main() {
MyStruct a;
MyStruct b;
a.x = 10;
b.x = 20;
Console.WriteLine("a.x {0}, b.x {1}", a.x, b.x);
a = b;
b.x = 30;
Console.WriteLine("a.x {0}, b.x {1}", a.x, b.x);
}
}
listing 21
// Copy a class.
using System;
// Define a structure.
class MyClass {
public int x;
}
// Now show a class object assignment.
class ClassAssignment {
public static void Main() {
MyClass a = new MyClass();
MyClass b = new MyClass();
a.x = 10;
b.x = 20;
Console.WriteLine("a.x {0}, b.x {1}", a.x, b.x);
a = b;
b.x = 30;
Console.WriteLine("a.x {0}, b.x {1}", a.x, b.x);
}
}
listing 22
// Structures are good when grouping data.
using System;
// Define a packet structure.
struct PacketHeader {
public uint packNum; // packet number
public ushort packLen; // length of packet
}
// Use PacketHeader to create an e-commerce transaction record.
class Transaction {
static uint transacNum = 0;
PacketHeader ph; // incorporate PacketHeader into Transaction
string accountNum;
double amount;
public Transaction(string acc, double val) {
// create packet header
ph.packNum = transacNum++;
ph.packLen = 512; // arbitrary length
accountNum = acc;
amount = val;
}
// Simulate a transaction.
public void sendTransaction() {
Console.WriteLine("Packet #: " + ph.packNum +
", Length: " + ph.packLen +
",\n Account #: " + accountNum +
", Amount: {0:C}\n", amount);
}
}
// Demonstrate Packet
class PacketDemo {
public static void Main() {
Transaction t = new Transaction("31243", -100.12);
Transaction t2 = new Transaction("AB4655", 345.25);
Transaction t3 = new Transaction("8475-09", 9800.00);
t.sendTransaction();
t2.sendTransaction();
t3.sendTransaction();
}
}
listing 23
// Demonstrate an enumeration.
using System;
class EnumDemo {
enum Apple { Jonathan, GoldenDel, RedDel, Winesap,
Cortland, McIntosh };
public static void Main() {
string[] color = {
"Red",
"Yellow",
"Red",
"Red",
"Red",
"Reddish Green"
};
Apple i; // declare an enum variable
// use i to cycle through the enum
for(i = Apple.Jonathan; i <= Apple.McIntosh; i++)
Console.WriteLine(i + " has value of " + (int)i);
Console.WriteLine();
// use an enumeration to index an array
for(i = Apple.Jonathan; i <= Apple.McIntosh; i++)
Console.WriteLine("Color of " + i + " is " +
color[(int)i]);
}
}
listing 24
// Simulate a conveyor belt
using System;
class ConveyorControl {
// enumerate the conveyor commands
public enum Action { start, stop, forward, reverse };
public void conveyor(Action com) {
switch(com) {
case Action.start:
Console.WriteLine("Starting conveyor.");
break;
case Action.stop:
Console.WriteLine("Stopping conveyor.");
break;
case Action.forward:
Console.WriteLine("Moving forward.");
break;
case Action.reverse:
Console.WriteLine("Moving backward.");
break;
}
}
}
class ConveyorDemo {
public static void Main() {
ConveyorControl c = new ConveyorControl();
c.conveyor(ConveyorControl.Action.start);
c.conveyor(ConveyorControl.Action.forward);
c.conveyor(ConveyorControl.Action.reverse);
c.conveyor(ConveyorControl.Action.stop);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -