Csharp/C Sharp/Class Interface/public — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 11:38, 26 мая 2010

A public constructor initializes private instance variable balance through public property.

using System;
public class Account
{
   private decimal balance; 
   public Account( decimal initialBalance )
   {
      Balance = initialBalance; 
   } 
   public void Credit( decimal amount )
   {
      Balance = Balance + amount; 
   } 
   public decimal Balance
   {
      get
      {
         return balance;
      } 
      set
      {
         if ( value >= 0 )
            balance = value;
      }
   } 
} 
public class AccountTest
{
   public static void Main( string[] args )
   {
      Account account1 = new Account( 5.00M ); 
      Account account2 = new Account( -7.53M ); 
      Console.WriteLine( "account1 balance: {0:C}",account1.Balance ); 
      Console.WriteLine( "account2 balance: {0:C}\n",account2.Balance );
      decimal depositAmount; 
      Console.Write( "Enter deposit amount for account1: " );
      depositAmount = Convert.ToDecimal( Console.ReadLine() );
      Console.WriteLine( "adding {0:C} to account1 balance\n",depositAmount );
      account1.Credit( depositAmount ); 
      Console.WriteLine( "account1 balance: {0:C}",account1.Balance );
      Console.WriteLine( "account2 balance: {0:C}\n",account2.Balance );
      Console.Write( "Enter deposit amount for account2: " );
      depositAmount = Convert.ToDecimal( Console.ReadLine() );
      Console.WriteLine( "adding {0:C} to account2 balance\n",depositAmount );
      account2.Credit( depositAmount ); 
      Console.WriteLine( "account1 balance: {0:C}", account1.Balance );
      Console.WriteLine( "account2 balance: {0:C}", account2.Balance );
   }
}


Class declaration with a public method that has a parameter.

       
using System;
public class MyClass
{
   public void DisplayMessage( string courseName )
   {
      Console.WriteLine( "Welcome to the grade book for\n{0}!",courseName );
   }
}
public class MyClassTest
{
   public static void Main( string[] args )
   {
      MyClass myMyClass = new MyClass();
      Console.WriteLine( "Please enter the course name:" );
      string nameOfCourse = Console.ReadLine(); 
      Console.WriteLine(); 
      myMyClass.DisplayMessage( nameOfCourse );
   } 
}


Class declaration with one public method.

using System;
public class MyClass
{
   public void DisplayMessage()
   {
      Console.WriteLine( "Welcome to the Grade Book!" );
   }
} 
public class MyClassTest
{
   public static void Main( string[] args )
   {
      MyClass myMyClass = new MyClass();
      myMyClass.DisplayMessage();
   }
}


MyClass class with a public constructor to initialize the course name.

using System;
public class MyClass
{
   private string courseName;
   public MyClass( string name )
   {
      CourseName = name; 
   }
   public string CourseName
   {
      get
      {
         return courseName;
      } 
      set
      {
         courseName = value;
      }
   }
   public void DisplayMessage()
   {
      Console.WriteLine( CourseName );
   } 
} 
public class MyClassTest
{
   public static void Main( string[] args )
   {
      MyClass gradeBook1 = new MyClass("C#" );
      MyClass gradeBook2 = new MyClass("Data Structures" );
      Console.WriteLine( "gradeBook1 course name is: {0}",gradeBook1.CourseName );
      Console.WriteLine( "gradeBook2 course name is: {0}",gradeBook2.CourseName );
   }
}


User-defined public method

using System;
public class MaximumFinder
{
   public void DetermineMaximum()
   {
      Console.WriteLine( "Enter three floating-point values,\n pressing "Enter" after each one: " );
      double number1 = Convert.ToDouble( Console.ReadLine() );
      double number2 = Convert.ToDouble( Console.ReadLine() );
      double number3 = Convert.ToDouble( Console.ReadLine() );
      double result = Maximum( number1, number2, number3 );
      Console.WriteLine( "Maximum is: " + result );
   }
   public double Maximum( double x, double y, double z )
   {
      double maximumValue = x; 
      if ( y > maximumValue )
         maximumValue = y;
      if ( z > maximumValue )
         maximumValue = z;
      return maximumValue;
   } 
} 
public class MaximumFinderTest
{
   public static void Main( string[] args )
   {
      MaximumFinder maximumFinder = new MaximumFinder();
      maximumFinder.DetermineMaximum();
   }
}