Csharp/CSharp Tutorial/Class/Object Reference
Содержание
- 1 Assign values to the House object"s fields using object renerence
- 2 Change the object referenced by the myHouse object reference to the object referenced by yourHouse
- 3 Class comparison
- 4 Creating a House object and assigning its memory location to myHouse
- 5 Declare a House object reference named myHouse
- 6 Declare another House object reference and create another House object
- 7 Declare class fields and methods
- 8 Display the field values using object reference
- 9 Pass reference type variable without "out" and "ref"
- 10 Reference an object by interface and class
- 11 Reference a static member function without using the class name
- 12 Reference equals
- 13 Reference one object by multiple interfaces
- 14 Reference type equals: complex number
- 15 Use interface as reference
Assign values to the House object"s fields using object renerence
<source lang="csharp">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; }
}</source>
Change the object referenced by the myHouse object reference to the object referenced by yourHouse
<source lang="csharp">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); }
}</source>
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
<source lang="csharp">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; }</source>
Creating a House object and assigning its memory location to myHouse
<source lang="csharp">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(); }
}</source>
Declare a House object reference named myHouse
<source lang="csharp">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(); }
}</source>
Creating a House object and assigning its memory location to myHouse
Declare another House object reference and create another House object
<source lang="csharp">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); }
}</source>
started stopped yourHouse is a SSS
Declare class fields and methods
- The new operator dynamically allocates memory for an object and returns a reference to it.
- This reference is, more or less, the address in memory of the object allocated by new.
- This reference is then stored in a variable.
<source lang="csharp">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(); }
}</source>
Creating a House object and assigning its memory location to myHouse
Display the field values using object reference
<source lang="csharp">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); }
}</source>
myHouse details: myHouse.make = ABC myHouse.model = Apartment myHouse.color = black myHouse.yearBuilt = 1995
Pass reference type variable without "out" and "ref"
<source lang="csharp">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); }
}</source>
Before -- myObject.Val: 20, intValue: 10 After -- myObject.Val: 25, intValue: 10
Reference an object by interface and class
- You can create an interface reference variable.
- Such a variable can refer to any object that implements its interface.
- When you call a method on an object through an interface reference, the method is the version of the method implemented by the object.
<source lang="csharp">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(); }
}</source>
Reference a static member function without using the class name
<source lang="csharp">public class A {
public static void SomeFunction() { System.Console.WriteLine( "SomeFunction() called" ); } static void Main() { A.SomeFunction(); SomeFunction(); }
}</source>
SomeFunction() called SomeFunction() called
Reference equals
<source lang="csharp">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 ); }
}</source>
Result of Equality is False
Reference one object by multiple interfaces
<source lang="csharp">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."); }
}</source>
object. interface 1. interface 2.
Reference type equals: complex number
<source lang="csharp">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) ); }
}</source>
Result of Equality is True Identity of references is False Identity of references is False
Use interface as reference
<source lang="csharp">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."); }
}</source>
Calling through: object. Calling through: interface.