nowhy

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

使用其他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 }

 

posted on 2019-08-30 10:20  g6z3z  阅读(568)  评论(0编辑  收藏  举报