using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices;
namespace Example03 { class Program { //注意DllImport是一个Attribute Property,在System.Runtime.InteropServices命名空间中定义 //extern与DllImport一起使用时必须再加上一个static修饰符 [DllImport("User32.dll")] public static extern int MessageBox(int Handle, string Message, string Caption, int Type);
static int Main() { string myString; Console.Write("Enter your message: "); myString = Console.ReadLine(); return MessageBox(0, myString, "My Message Box", 0); } } } 结果:
using System; using System.Collections.Generic; using System.Text;
namespace Example06 { class Program { class A { public virtual void F() { Console.WriteLine("A.F"); } public virtual void G() { Console.WriteLine("A.G"); } } class B : A { public sealed override void F() { Console.WriteLine("B.F"); } public override void G() { Console.WriteLine("B.G"); } } class C : B { public override void G() { Console.WriteLine("C.G"); } } static void Main(string[] args) { new A().F(); new A().G(); new B().F(); new B().G(); new C().F(); new C().G();
Console.ReadLine(); } } } 结果: 类 B 在继承类 A 时可以重写两个虚函数,如图所示:
using System; using System.Collections.Generic; using System.Text;
namespace Example08 { public class Point { private double x, y; public Point(double X, double Y) { x = X; y = Y; } //重写ToString方法方便输出 public override string ToString() { return String.Format("X: {0} , Y: {1}", x, y); } } public class Points { Point[] points; public Points(Point[] Points) { points = Points; } public int PointNumber { get { return points.Length; } } //实现索引访问器 public Point this[int Index] { get { return points[Index]; } } }
//感谢watson hua(http://huazhihao.cnblogs.com/)的指点 //索引指示器的实质是含参属性,参数并不只限于int class WeatherOfWeek { public string this[int Index] { get { //注意case段使用return直接返回所以不需要break switch (Index) { case 0: { return "Today is cloudy!"; } case 5: { return "Today is thundershower!"; } default: { return "Today is fine!"; } } } } public string this[string Day] { get { string TodayWeather = null; //switch的标准写法 switch (Day) { case "Sunday": { TodayWeather = "Today is cloudy!"; break; } case "Friday": { TodayWeather = "Today is thundershower!"; break; } default: { TodayWeather = "Today is fine!"; break; } } return TodayWeather; } } } class Program { static void Main(string[] args) { Point[] tmpPoints = new Point[10]; for (int i = 0; i < tmpPoints.Length; i++) { tmpPoints[i] = new Point(i, Math.Sin(i)); }
Points tmpObj = new Points(tmpPoints); for (int i = 0; i < tmpObj.PointNumber; i++) { Console.WriteLine(tmpObj[i]); }
string[] Week = new string[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Staurday"}; WeatherOfWeek tmpWeatherOfWeek = new WeatherOfWeek(); for (int i = 0; i < 6; i++) { Console.WriteLine(tmpWeatherOfWeek[i]); } foreach (string tmpDay in Week) { Console.WriteLine(tmpWeatherOfWeek[tmpDay]); }
Console.ReadLine(); } } } 结果: X: 0 , Y: 0 X: 1 , Y: 0.841470984807897 X: 2 , Y: 0.909297426825682 X: 3 , Y: 0.141120008059867 X: 4 , Y: -0.756802495307928 X: 5 , Y: -0.958924274663138 X: 6 , Y: -0.279415498198926 X: 7 , Y: 0.656986598718789 X: 8 , Y: 0.989358246623382 X: 9 , Y: 0.412118485241757 Today is cloudy! Today is fine! Today is fine! Today is fine! Today is fine! Today is thundershower! Today is cloudy! Today is fine! Today is fine! Today is fine! Today is fine! Today is thundershower! Today is fine!
using System; using System.Collections.Generic; using System.Text;
namespace Example09 { class BaseClass { //基类设计者声明了一个PI的公共变量,方便进行运算 public static double PI = 3.1415; } class DervieClass : BaseClass { //继承类发现该变量的值不能满足运算精度,于是可以通过new修饰符显示隐藏基类中的声明 public new static double PI = 3.1415926; } class Program { static void Main(string[] args) { Console.WriteLine(BaseClass.PI); Console.WriteLine(DervieClass.PI);
class BaseClass { public virtual void F() { Console.WriteLine("BaseClass.F"); } } abstract class DeriveClass : BaseClass { public new abstract void F(); }
12.密封类可以有虚函数吗?
答:
可以,基类中的虚函数将隐式的转化为非虚函数,但密封类本身不能再增加新的虚函数
示例:
class BaseClass { public virtual void F() { Console.WriteLine("BaseClass.F"); } } sealed class DeriveClass : BaseClass { //基类中的虚函数F被隐式的转化为非虚函数
using System; using System.Collections.Generic; using System.Text;
namespace Example16 { interface IPoint { double X { get; set; } double Y { get; set; } double Z { get; set; } } //结构也可以从接口继承 struct Point: IPoint { private double x, y, z; //结构也可以增加构造函数 public Point(double X, double Y, double Z) { this.x = X; this.y = Y; this.z = Z; } public double X { get { return x; } set { x = value; } } public double Y { get { return x; } set { x = value; } } public double Z { get { return x; } set { x = value; } } } //在此简化了点状Geometry的设计,实际产品中还包含Project(坐标变换)等复杂操作 class PointGeometry { private Point value;
public PointGeometry(double X, double Y, double Z) { value = new Point(X, Y, Z); } public PointGeometry(Point value) { //结构的赋值将分配新的内存 this.value = value; } public double X { get { return value.X; } set { this.value.X = value; } } public double Y { get { return value.Y; } set { this.value.Y = value; } } public double Z { get { return value.Z; } set { this.value.Z = value; } } public static PointGeometry operator +(PointGeometry Left, PointGeometry Rigth) { return new PointGeometry(Left.X + Rigth.X, Left.Y + Rigth.Y, Left.Z + Rigth.Z); } public override string ToString() { return string.Format("X: {0}, Y: {1}, Z: {2}", value.X, value.Y, value.Z); } } class Program { static void Main(string[] args) { Point tmpPoint = new Point(1, 2, 3);
PointGeometry tmpPG1 = new PointGeometry(tmpPoint); PointGeometry tmpPG2 = new PointGeometry(tmpPoint); tmpPG2.X = 4; tmpPG2.Y = 5; tmpPG2.Z = 6;
using System; using System.Collections.Generic; using System.Text;
namespace com.nblogs.reonlyrun.CSharp26QExample.Example19.Lib01 { class Class1 { public override string ToString() { return "com.nblogs.reonlyrun.CSharp26QExample.Example19.Lib01's Class1"; } } } Class2.cs
using System; using System.Collections.Generic; using System.Text;
namespace com.nblogs.reonlyrun.CSharp26QExample.Example19.Lib02 { class Class1 { public override string ToString() { return "com.nblogs.reonlyrun.CSharp26QExample.Example19.Lib02's Class1"; } } } 主单元(Program.cs):
using System; using System.Collections.Generic; using System.Text;
//使用别名指示符解决同名类型的冲突 using Lib01Class1 = com.nblogs.reonlyrun.CSharp26QExample.Example19.Lib01.Class1; using Lib02Class2 = com.nblogs.reonlyrun.CSharp26QExample.Example19.Lib02.Class1;
namespace Example19 { class Program { static void Main(string[] args) { Lib01Class1 tmpObj1 = new Lib01Class1(); Lib02Class2 tmpObj2 = new Lib02Class2();