Csharp/CSharp Tutorial/Class/static Class — различия между версиями

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

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

Demonstrate a static class.

using System;  
  
static class MathFunction {  
  // 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 MainClass {  
  public static void Main() {    
    Console.WriteLine("Reciprocal of 5 is " + 
                      MathFunction.reciprocal(5.0)); 
 
    Console.WriteLine("Fractional part of 4.234 is " + 
                      MathFunction.fracPart(4.234)); 
 
    if(MathFunction.isEven(10)) 
      Console.WriteLine("10 is even."); 
 
    if(MathFunction.isOdd(5)) 
      Console.WriteLine("5 is odd."); 
 
    // The following attempt to create an instance of  
    // MathFunction will cause an error. 
//  MathFunction ob = new MathFunction(); // Wrong! 
  }  
}
Reciprocal of 5 is 0.2
Fractional part of 4.234 is 0.234
10 is even.
5 is odd.

Static class and its inner class

using System;
public static class StaticClass
{
    public static void DoWork() {
        ++CallCount;
        Console.WriteLine( "StaticClass.DoWork()" );
    }
    public class NestedClass {
        public NestedClass() {
            Console.WriteLine( "NestedClass.NestedClass()" );
        }
    }
    public static long CallCount = 0;
}
public static class MainClass
{
    static void Main() {
        StaticClass.DoWork();
        StaticClass.NestedClass nested = new StaticClass.NestedClass();
        Console.WriteLine( "CallCount = {0}", StaticClass.CallCount );
    }
}
StaticClass.DoWork()
NestedClass.NestedClass()
CallCount = 1

Static inner class constructor

using System;
using System.ruponentModel;
        class Outer<T>
        {
            public class Inner<U, V>
            {
                static Inner()
                {
                    Console.WriteLine("Outer<{0}>.Inner<{1},{2}>",
                                      typeof(T).Name,
                                      typeof(U).Name,
                                      typeof(V).Name);
                }
                public static void DummyMethod()
                {
                }
            }
            static void Main()
            {
                Outer<int>.Inner<string, DateTime>.DummyMethod();
            }
        }