程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> 幾個C# PROGRAMS

幾個C# PROGRAMS

編輯:.NET實例教程


 

using System;

namespace Wrox.ProCSharp.OOProg     //The first program
{
   class MainEntryPoint
   {
      static void Main()
      {
         Authenticator myAccess = new Authenticator();
         bool done;
         done = myAccess.ChangePassword("", "MyNewPassWord");
         if (done == true) 
            Console.WriteLine("PassWord for myAccess changed");
         else
            Console.WriteLine("Failed to change passWord for myAccess");
         done = myAccess.ChangePassword("", "AnotherPassWord");
         if (done == true) 
            Console.WriteLine("PassWord for myAccess changed");
         else
            Console.WriteLine("Failed to change passWord for myAccess");

         if (myAccess.IsPasswordCorrect("WhatPassWord"))
            Console.WriteLine("VerifIEd myAccess\' passWord");
         else
            Console.WriteLine("Failed to verify myAccess\' passWord");
      }

   }

   public class Authenticator
   {
      // implementation as shown earlIEr

      private string passWord = "";
   
      public bool IsPasswordCorrect(string tryPassWord)
      {
    &nbs

$False$

p;    return (tryPassword == passWord) ? true : false;
      }

      public bool ChangePassword(string oldPassword, string newPassWord)
      {
         if (oldPassword == passWord)
         {
            password = newPassWord;
            return true;
         }
         else
            return false;
      }
   }
}

using System;

namespace Wrox.ProCSharp.OOProg    //The Second program
{
   class MainEntryPoint
   {
      static void Main()
      {
         Authenticator myAccess = new Authenticator();
         bool done;
         done = myAccess.ChangePassword("", "MyNewPassWord");
         if (done == true) 
            Console.WriteLine("PassWord for myAccess changed");
         else
            Console.WriteLine("Failed to change passWord for myAccess");
         done = myAccess.ChangePassword("", "AnotherPassWord");
         if (done == true) 
            Console.WriteLine("PassWord for myAccess changed");
         else
            Console.WriteLine("Failed to change passWord for myAccess");

         if (myAccess.IsPasswo

rdCorrect("WhatPassWord"))
            Console.WriteLine("VerifIEd myAccess\' passWord");
         else
            Console.WriteLine("Failed to verify myAccess\' passWord");
      }

   }

   public class Authenticator
   {
      // implementation as shown earlIEr

      private string passWord = "";
   
      public bool IsPasswordCorrect(string tryPassWord)
      {
         return (tryPassword == passWord) ? true : false;
      }

      public bool ChangePassword(string oldPassword, string newPassWord)
      {
         if (oldPasswor
d == passWord)
         {
            password = newPassWord;
            return true;
         }
         else
            return false;
      }
   }
}

namespace Wrox.ProCSharp.OOProg
{
   using System;
   public enum TypeOfCall 
   {
      CallToCellPhone, CallToLandline
   }

   public class Customer
   {
      private string name;
      private decimal balance;
      public string Name
      {
         get
         {
            return name;
         }
         set
         {
            name = value;
         }
      }

      public decimal Balance
      {
         get
         {
            return balance;
         }
      }
      public void RecordPayment(decimal amountPaid)
      {
         balance -= amountPaid;
      }
      public void RecordCall(TypeOfCall callType, uint nMinutes)
      {
         switch (callType)
         {
            case TypeOfCall.CallToLandline:
               balance += (0.02M * nMinutes);
               break;
            case TypeOfCall.CallToCellPhone:
               balance += (0.30M * nMinutes);
               break;
            default:
               break;
         }
      }
   }
   public class MainEntryPoint
   {
      public static void Main()
      {
         Customer arabel = new Customer();
         arabel.Name = "Arabel Jones";
         Customer mrJones = new Customer();
         mrJones.Name = "Ben Jones";
         arabel.RecordCall(TypeOfCall.CallToLandline, 20);
         arabel.RecordCall(TypeOfCall.CallToCellPhone, 5);
         mrJones.RecordCall(TypeOfCall.CallToLandline, 10);
         Console.WriteLine("{0,-20} owes ${1:F2}", arabel.Name, arabel.Balance);
         Console.WriteLine("{0,-20} owes ${1:F2}", mrJones.Name, mrJones.Balance);
      }
   }
}

namespace Wrox.ProCSharp.OOProg
{
   using System;
   public enum TypeOfCall 
   {
      CallToCellPhone, CallToLandline
   }

   public class Customer
   {
      private string name;
      protected decimal balance;
      public string GetFunnyString()
      {
         return "Plain ordinary customer. Kaark!";
      }
      public string Name
      {
         get
         {
            return name;
         }
         set
         {
            name = value;
         }
   &
nbsp;  }

      public decimal Balance
      {
      get
         {
            return balance;
         }
      }
      public void RecordPayment(decimal amountPaid)
      {
         balance -= amountPaid;
      }
      public virtual void RecordCall(TypeOfCall callType, uint nMinutes)
      {
         switch (callType)
         {
            case TypeOfCall.CallToLandline:
               balance += (0.02M * nMinutes);
               break;
            case TypeOfCall.CallToCellPhone:
               balance += (0.30M * nMinutes);
               break;
            default:
               break;
         }
      }
   }
   public class Nevermore60Customer : Customer
   {
      private uint highCostMinutesUsed;
      public new string GetFunnyString()
      {
         return "Nevermore60. Nevermore!";
      }
      public override void RecordCall(TypeOfCall callType, uint nMinutes)
      {
         switch (callType)
       

>            case TypeOfCall.CallToLandline:
               balance += (0.02M * nMinutes);
               break;
            case TypeOfCall.CallToCellPhone:
         uint highCostMinutes, lowCostMinutes;
         uint highCostMinutesToGo = 
            (highCostMinutesUsed < 60) ? 60 - highCostMinutesUsed : 0;
         if (nMinutes > highCostMinutesToGo)
         {
            highCostMinutes = highCostMinutesToGo;
            lowCostMinutes = nMinutes - highCostMinutes;
         }
         else
         {
            highCostMinutes = nMinutes;
            lowCostMinutes = 0;
         }
         highCostMinutesUsed += highCostMinutes;
         balance += (0.50M * highCostMinutes + 0.20M * 
            lowCostMinutes);
         break;
            default:
               break;
         }
      }
   }
   public class MainEntryPoint
   {
      public static void Main()
      {
    &nbsp;    Customer cust1;
         Nevermore60Customer cust2;
         cust1 = new Customer();
         Console.WriteLine("Customer referencing Customer: " 
            + cust1.GetFunnyString());
         cust1 = new Nevermore60Customer();
         Console.WriteLine("Customer referencing Nevermore60Customer: " 
            + cust1.GetFunnyString());
         cust2 = n
ew Nevermore60Customer();
         Console.WriteLine("Nevermore60Customer referencing: " 
            + cust2.GetFunnyString());   
      }
   }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved