Csharp/CSharp Tutorial/Class/Object Reference

Материал из .Net Framework эксперт
Версия от 12:16, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Assign values to the House object"s fields using object renerence

class House
{
  public string make;
  public string model;
  public string color;
  public int yearBuilt;
  public void Start()
  {
    System.Console.WriteLine(model + " started");
  }
  public void Stop()
  {
    System.Console.WriteLine(model + " stopped");
  }
}
class MainClass
{
  public static void Main()
  {
    
    House myHouse;
    myHouse = new House();
    myHouse.make = "ABC";
    myHouse.model = "Apartment";
    myHouse.color = "black";
    myHouse.yearBuilt = 1995;
  }
}

Change the object referenced by the myHouse object reference to the object referenced by yourHouse

class House
{
  public string make;
  public string model;
  public string color;
  public int yearBuilt;
  public void Start()
  {
    System.Console.WriteLine(model + " started");
  }
  public void Stop()
  {
    System.Console.WriteLine(model + " stopped");
  }
}
class MainClass
{
  public static void Main()
  {
    
    House myHouse;
    myHouse = new House();
    myHouse.Start();
    myHouse.Stop();
    
    House yourHouse = new House();
    yourHouse.make = "AAA";
    yourHouse.model = "VVV";
    yourHouse.color = "red";
    yourHouse.yearBuilt = 2000;
    System.Console.WriteLine("yourHouse is a " + yourHouse.model);
    
    System.Console.WriteLine("Assigning yourHouse to myHouse");
    myHouse = yourHouse;
    System.Console.WriteLine("myHouse details:");
    System.Console.WriteLine("myHouse.make = " + myHouse.make);
    System.Console.WriteLine("myHouse.model = " + myHouse.model);
    System.Console.WriteLine("myHouse.color = " + myHouse.color);
    System.Console.WriteLine("myHouse.yearBuilt = " + myHouse.yearBuilt);
  }
}
started
 stopped
yourHouse is a VVV
Assigning yourHouse to myHouse
myHouse details:
myHouse.make = AAA
myHouse.model = VVV
myHouse.color = red
myHouse.yearBuilt = 2000

Class comparison

using System;
  class ComparingRelations
  {
    static void Main(string[] args)
    {   
            int a = 12;
            int b = 12;
            Console.WriteLine( a == b );
            Console.WriteLine( (object)a == (object)b );

            string c = "hello";
            string d = "hello";
            Console.WriteLine( (object) c==(object) d );
            ClassCompare x = new ClassCompare();
            ClassCompare y;
            x.val = 1;
            y = x;
            Console.WriteLine( x == y );
            x.val = 2;
            Console.WriteLine( y.val.ToString() );
    }
  }
    class ClassCompare
    {
        public int val = 0;
    }

Creating a House object and assigning its memory location to myHouse

public class House
{
  public string make;
  public string model;
  public string color;
  public int yearBuilt;
  public void Start()
  {
    System.Console.WriteLine(model + " started");
  }
  public void Stop()
  {
    System.Console.WriteLine(model + " stopped");
  }
}
class MainClass
{
  public static void Main()
  {
    
    House myHouse;
    myHouse = new House();
  }
}

Declare a House object reference named myHouse

public class House
{
  public string make;
  public string model;
  public string color;
  public int yearBuilt;
  public void Start()
  {
    System.Console.WriteLine(model + " started");
  }
  public void Stop()
  {
    System.Console.WriteLine(model + " stopped");
  }
}
class MainClass
{
  public static void Main()
  {
    
    House myHouse;
    System.Console.WriteLine("Creating a House object and assigning its memory location to myHouse");
    myHouse = new House();
  }
}
Creating a House object and assigning its memory location to myHouse

Declare another House object reference and create another House object

class House
{
  public string make;
  public string model;
  public string color;
  public int yearBuilt;
  public void Start()
  {
    System.Console.WriteLine(model + " started");
  }
  public void Stop()
  {
    System.Console.WriteLine(model + " stopped");
  }
}
class MainClass
{
  public static void Main()
  {
    
    House myHouse;
    myHouse = new House();
    myHouse.Start();
    myHouse.Stop();
    
    House yourHouse = new House();
    yourHouse.make = "AQW";
    yourHouse.model = "SSS";
    yourHouse.color = "red";
    yourHouse.yearBuilt = 2000;
    System.Console.WriteLine("yourHouse is a " + yourHouse.model);
  }
}
started
 stopped
yourHouse is a SSS

Declare class fields and methods

  1. The new operator dynamically allocates memory for an object and returns a reference to it.
  2. This reference is, more or less, the address in memory of the object allocated by new.
  3. This reference is then stored in a variable.


public class House
{
  public string make;
  public string model;
  public string color;
  public int yearBuilt;
  public void Start()
  {
    System.Console.WriteLine(model + " started");
  }
  public void Stop()
  {
    System.Console.WriteLine(model + " stopped");
  }
}
class MainClass
{
  public static void Main()
  {
    
    House myHouse;
    System.Console.WriteLine("Creating a House object and assigning its memory location to myHouse");
    myHouse = new House();
  }
}
Creating a House object and assigning its memory location to myHouse

Display the field values using object reference

class House
{
  public string make;
  public string model;
  public string color;
  public int yearBuilt;
  public void Start()
  {
    System.Console.WriteLine(model + " started");
  }
  public void Stop()
  {
    System.Console.WriteLine(model + " stopped");
  }
}
class MainClass
{
  public static void Main()
  {
    
    House myHouse;
    myHouse = new House();
    myHouse.make = "ABC";
    myHouse.model = "Apartment";
    myHouse.color = "black";
    myHouse.yearBuilt = 1995;
    System.Console.WriteLine("myHouse details:");
    System.Console.WriteLine("myHouse.make = " + myHouse.make);
    System.Console.WriteLine("myHouse.model = " + myHouse.model);
    System.Console.WriteLine("myHouse.color = " + myHouse.color);
    System.Console.WriteLine("myHouse.yearBuilt = " + myHouse.yearBuilt);
  }
}
myHouse details:
myHouse.make = ABC
myHouse.model = Apartment
myHouse.color = black
myHouse.yearBuilt = 1995

Pass reference type variable without "out" and "ref"

using System;
class MyClass
{
   public int Val = 20;
}
class MainClass
{
   static void MyMethod(MyClass myObject, int intValue)
   {
      myObject.Val = myObject.Val + 5;
      intValue = intValue + 5;        
   }
   static void Main()
   {
      MyClass myObject = new MyClass();
      int intValue = 10;
      Console.WriteLine("Before -- myObject.Val: {0}, intValue: {1}", myObject.Val, intValue);
      MyMethod(myObject, intValue);
      Console.WriteLine("After  -- myObject.Val: {0}, intValue: {1}", myObject.Val, intValue);
   }
}
Before -- myObject.Val: 20, intValue: 10
After  -- myObject.Val: 25, intValue: 10

Reference an object by interface and class

  1. You can create an interface reference variable.
  2. Such a variable can refer to any object that implements its interface.
  3. When you call a method on an object through an interface reference, the method is the version of the method implemented by the object.


public interface Player
{
 void PlayMusic();
}
public class Student : Player
{
 public void PlayMusic(){}
 public void DoALittleDance(){}
}
public class MainClass
{
   static void Main()
   {
      Student st = new Student();
      Player musician = st;
      musician.PlayMusic();
      st.PlayMusic();
      st.DoALittleDance();
   }
}

Reference a static member function without using the class name

public class A
{
   public static void SomeFunction()
   {
      System.Console.WriteLine( "SomeFunction() called" );
   }
   static void Main()
   {
      A.SomeFunction();
      SomeFunction();
   }
}
SomeFunction() called
SomeFunction() called

Reference equals

public class MyClass
{
   public MyClass(  )
   {
   }
   
}
public class MainClass
{
   static void Main()
   {
      MyClass referenceA = new MyClass(  );
      MyClass referenceB = new MyClass(  );
      System.Console.WriteLine( "Result of Equality is {0}", referenceA == referenceB );
   }
}
Result of Equality is False

Reference one object by multiple interfaces

using System;
interface Interface1 {
      void PrintOut(string s);
}
interface Interface2 {
      void PrintOut(string s);
}
class MyClass : Interface1, Interface2
{
   public void PrintOut(string s)
   {
      Console.WriteLine(s);
   }
}
class MainClass
{
   static void Main()
   {
      MyClass mc = new MyClass();
      Interface1 ifc1 = (Interface1)mc;
      Interface2 ifc2 = (Interface2)mc;
      mc.PrintOut("object.");
      ifc1.PrintOut("interface 1.");
      ifc2.PrintOut("interface 2.");
   }
}
object.
interface 1.
interface 2.

Reference type equals: complex number

public class ComplexNumber
{
   public ComplexNumber( int real, int imaginary )
   {
      this.real = real;
      this.imaginary = imaginary;
   }
   public override bool Equals( object obj )
   {
      ComplexNumber other = obj as ComplexNumber;
      if( other == null )
      {
         return false;
      }
      return (this.real == other.real) && (this.imaginary == other.imaginary);
   }
   public override int GetHashCode()
   {
      return (int) real ^ (int) imaginary;
   }
   public static bool operator==( ComplexNumber me, ComplexNumber other )
   {
      return Equals( me, other );
   }
   public static bool operator!=( ComplexNumber me, ComplexNumber other )
   {
      return Equals( me, other );
   }
   
   private double real;
   private double imaginary;
}
public class MainClass
{
   static void Main()
   {
      ComplexNumber referenceA = new ComplexNumber( 1, 2 );
      ComplexNumber referenceB = new ComplexNumber( 1, 2 );
      System.Console.WriteLine( "Result of Equality is {0}",referenceA == referenceB );
      System.Console.WriteLine( "Identity of references is {0}",(object) referenceA == (object) referenceB );
      System.Console.WriteLine( "Identity of references is {0}",ReferenceEquals(referenceA, referenceB) );
   }
}
Result of Equality is True
Identity of references is False
Identity of references is False

Use interface as reference

using System;
interface Interface1
{
   void PrintOut(string s);
}
class MyClass : Interface1
{
   public void PrintOut(string s)
   {
      Console.WriteLine("Calling through: {0}", s);
   }
}
class MainClass
{
   static void Main()
   {
      MyClass mc = new MyClass();         
      mc.PrintOut("object.");             
      Interface1 ifc = (Interface1)mc;    
      ifc.PrintOut("interface.");         
   }
}
Calling through: object.
Calling through: interface.