ex-08-06
来自「Programming Csharp Source Code(代码) Prog」· 代码 · 共 73 行
TXT
73 行
//Example 08-06: References on value types
using System;
// declare a simple interface
interface IStorable
{
void Read();
int Status { get;set;}
}
// Implement through a struct
public struct myStruct : IStorable
{
public void Read()
{
Console.WriteLine(
"Implementing IStorable.Read");
}
public int Status
{
get
{
return status;
}
set
{
status = value;
}
}
private int status;
}
public class Tester
{
static void Main()
{
// create a myStruct object
myStruct theStruct = new myStruct();
theStruct.Status = -1; // initialize
Console.WriteLine(
"theStruct.Status: {0}", theStruct.Status);
// Change the value
theStruct.Status = 2;
Console.WriteLine("Changed object.");
Console.WriteLine(
"theStruct.Status: {0}", theStruct.Status);
// cast to an IStorable
// implicit box to a reference type
IStorable isTemp = (IStorable) theStruct;
// set the value through the interface reference
isTemp.Status = 4;
Console.WriteLine("Changed interface.");
Console.WriteLine("theStruct.Status: {0}, isTemp: {1}",
theStruct.Status, isTemp.Status);
// Change the value again
theStruct.Status = 6;
Console.WriteLine("Changed object.");
Console.WriteLine("theStruct.Status: {0}, isTemp: {1}",
theStruct.Status, isTemp.Status);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?