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

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

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

A class with method and member variables

using System;
class Employee
{
    // constructor
    public Employee(string name, float billingRate)
    {
        this.name = name;
        this.billingRate = billingRate;
    }
    // figure out the charge based on Employee"s rate
    public float CalculateCharge(float hours)
    {
        return(hours * billingRate);
    }
    // return the name of this type
    public string TypeName()
    {
        return("Employee");
    }
    
    private string name;
    protected float billingRate;
}
class MainClass
{
    public static void Main()
    {
        Employee Employee = new Employee("A", 21.20F);
        Console.WriteLine("Name is: {0}", Employee.TypeName());
    }
}
Name is: Employee

Add a method to access the field variables

using System;  
  
class Building {   
  public int area;     
  public int occupants;
 
  
  public void areaPerPerson() {  
    Console.WriteLine("Display the area per person.");
    Console.WriteLine("  " + area / occupants + 
                      " area per person"); 
  }  
}   
 
class BuildingDemo {   
  public static void Main() {   
    Building house = new Building();   
    Building office = new Building(); 
 
    house.occupants = 4;  
    house.area = 2500;  
 
    // assign values to fields in office 
    office.occupants = 25;  
    office.area = 4200;  
   
    Console.WriteLine("house has:\n  " + 
                      house.occupants + " occupants\n  " + 
                      house.area + " total area"); 
    house.areaPerPerson(); 
 
    Console.WriteLine(); 
 
    Console.WriteLine("office has:\n  " + 
                      office.occupants + " occupants\n  " + 
                      office.area + " total area"); 
    office.areaPerPerson(); 
  }   
}
house has:
  4 occupants
  2500 total area
Display the area per person.
  625 area per person
office has:
  25 occupants
  4200 total area
Display the area per person.
  168 area per person

Call base constructor to init member variables

public class A
{
   public A( int x )
   {
      this.x = x;
   }
   public A() : this( 0 )
   {
   }
   internal int x;
}
public class B : A
{
   public B() : base( 1 )
   {
   }
}
public class MainClass
{
   static void Main()
   {
      B b = new B();
      System.Console.WriteLine( "A.x = {0}", b.x );
   }
}
A.x = 1

field initialization

class FieldInitExample
    {
        int x = 5;
        int y;
        public FieldInitExample() : this(5)
        {
        }
        public FieldInitExample(int y)
        {
            this.y = y;
        }
    }

fields

class FieldExample
{
    private static int idCounter;
    protected int id;
    public string name;
    public int x;
    public int y;
    private System.DateTime createDate;
}

How to use a "has a" relationship

public class Engine
{
  public int cylinders;
  public int horsepower;
  public void Start()
  {
    System.Console.WriteLine("Engine started");
  }
}
public class Car
{
  public string make;
  public Engine engine;  // Car has an Engine
  public void Start()
  {
    engine.Start();
  }
}
class MainClass
{
  public static void Main()
  {
    System.Console.WriteLine("Creating a Car object");
    Car myCar = new Car();
    myCar.make = "Toyota";
    System.Console.WriteLine("Creating an Engine object");
    myCar.engine = new Engine();
    myCar.engine.cylinders = 4;
    myCar.engine.horsepower = 180;
    System.Console.WriteLine("myCar.make = " + myCar.make);
    System.Console.WriteLine("myCar.engine.cylinders = " + myCar.engine.cylinders);
    System.Console.WriteLine("myCar.engine.horsepower = " + myCar.engine.horsepower);
    myCar.Start();
  }
}
Creating a Car object
Creating an Engine object
myCar.make = Toyota
myCar.engine.cylinders = 4
myCar.engine.horsepower = 180
Engine started

Illustrates how to assign default values to fields using initializers

class PC
{
  public string make = "AAA";
  public string model = "T";
  public string color;
  public int yearBuilt = 1910;
  public void Start()
  {
    System.Console.WriteLine(yearBuilt + " yearBuilt");
  }
}
class MainClass
{
  public static void Main()
  {
    PC myPC = new PC();
    System.Console.WriteLine("myPC.make = " + myPC.make);
    System.Console.WriteLine("myPC.model = " + myPC.model);
    if (myPC.color == null)
    {
      System.Console.WriteLine("myPC.color is null");
    }
    System.Console.WriteLine("myPC.yearBuilt = " + myPC.yearBuilt);
  }
}
myPC.make = AAA
myPC.model = T
myPC.color is null
myPC.yearBuilt = 1910

Use this and base together to init a class

using System;
class Base
{
   public Base( int x )
   {
      Console.WriteLine( "Base.Base(int)" );
      this.x = x;
   }
   
   public int x = 0;
}
class Derived : Base
{
   public Derived( int a ):base( a )
   {
      Console.WriteLine( "Derived.Derived(int)" );
      this.a = a;
   }
   public Derived( int a, int b ):this( a )
   {
      Console.WriteLine( "Derived.Derived(int, int)" );
      this.a = a;
      this.b = b;
   }
   public int a = 0;
   public int b = 0;
}
public class MainClass
{
   static void Main()
   {
      Derived b = new Derived( 1, 2 );
   }
}
Base.Base(int)
Derived.Derived(int)
Derived.Derived(int, int)