C#(2):类与命名空间

  • 类(class)是最基础的c#类型,是构成程序的主体
  • namespace以树形结构组织类和其他类型

    类似c++的import,引用类库。
    命名类的时候要精确命名
  • 类库的引用:使用命名空间的物理基础
    尽量使用弱依赖性的类库

  • 黑盒:直接引用网上类库
    白盒:引用自己编写的类库或者下载的类库

  • 单引号只能括字符



    练习

    引用自己创建的类库,


    引用的库中的类
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace SuperCalculator
    {
        public class Caculator
        {
            public static double Add(double a,double b)
            {
                return a + b;    
            }
            public static double Sub(double a, double b)
            {
                return a - b;
            }
            public static double Mul(double a, double b)
            {
                return a * b;
            }
            public static double Add(double a, double b)
            {
                if (b == 0)
                {
                    return a / b;
                }
                else {
                    return a / b;
                } 
            }
        }
    }

     

编写主程序,先在引用中选择

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SuperCalculator;//引用库
namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("helloworld");
            System.Console.WriteLine("systemhelloworld");
            double result = Caculator.Mul(3, 4);
            Console.WriteLine(result);
        }
    }
}

 

posted @ 2023-09-08 15:31  HY10-SALTEST-FISH  阅读(15)  评论(0编辑  收藏  举报