使用其他namespace中定义的类
在工程中新建一个类文件,注意一个工程中有很多个类文件,可以在不同的文件实现不同的类。
通过namespace在不同类文件中互相调用,但是同一工程下的这些类文件只允许有一个
public static void Main()
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using typedemo;//DoStuff()方法定义在该namespace中 5 6 namespace complex 7 { 8 public class Complex 9 { 10 double a, b; 11 public Complex(double d1,double d2) 12 { 13 a = d1; 14 b = d2; 15 } 16 public static Complex operator+(Complex c1,Complex c2) 17 { 18 Complex c3 = new Complex((c1.a + c2.a), (c1.b + c2.b)); 19 return c3; 20 } 21 public override string ToString() 22 { 23 if (b < 0) 24 { 25 return a.ToString() + "-" + Math.Abs(b) + "i"; 26 } 27 else 28 { 29 return a.ToString() + "+" + b + "i"; 30 } 31 } 32 } 33 public static class OverloadDemo 34 { 35 public static void Main() 36 { 37 Complex a = new Complex(1, 1) + new Complex(1, 2); 38 Console.WriteLine(a); 39 TypeDemo.DoStuff(22); 40 TypeDemo.DoStuff("dfd"); 41 // //Console.WriteLine("Hello World!"); 42 // //Console.WriteLine("Hello World!"); 43 } 44 } 45 }
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using static typedemo.TypeDemo; 5 6 namespace complex 7 { 8 public class Complex 9 { 10 double a, b; 11 public Complex(double d1,double d2) 12 { 13 a = d1; 14 b = d2; 15 } 16 public static Complex operator+(Complex c1,Complex c2) 17 { 18 Complex c3 = new Complex((c1.a + c2.a), (c1.b + c2.b)); 19 return c3; 20 } 21 public override string ToString() 22 {//重载tostring方法 23 if (b < 0) 24 { 25 return a.ToString() + "-" + Math.Abs(b) + "i"; 26 } 27 else 28 { 29 return a.ToString() + "+" + b + "i"; 30 } 31 } 32 } 33 public static class OverloadDemo 34 { 35 public static void Main() 36 { 37 Complex a = new Complex(1, 1) + new Complex(1, 2); 38 Console.WriteLine(a); 39 DoStuff(22); 40 DoStuff("dfd"); 41 // //Console.WriteLine("Hello World!"); 42 // //Console.WriteLine("Hello World!"); 43 } 44 } 45 }