C#入门学习

1.数组

1.1一维数组

输出数组中的数字:

        int[] arr=new int[5];           //数组定义,并初始化为0;
namespace 学习c_shap
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = { 1, 2, 3, 4, 5 };        //定义数组,并初始化;
            for (int i=4;i>=0;i--)
            {
                Console.WriteLine(arr[i])}
            Console.ReadLine();
        }
    }
}

1.1二维数组:

数组的声明:

  type[,] arryName;

数组初始化:

int[,] arr=new int[2,2]{{1,2},{2,3}};

冒泡法对数组进行排序:

        static void Main(string[] args)
        { 
            int t;
            int[] arr= new int[5];                   
            for (int i=0;i<arr.Length ;i++)        //循环输入数组数值;
            {
                Console.Write("请输入地{0}个值:",i+1);
               arr[i]=int.Parse( Console.ReadLine()); //由于Console.Read()函数只能输入字符串,因此在想要输入数字的时候应该转换数据类型;
            }

           //显示数组元素;
            for(int i=0;i<arr.Length ;i++)
            {
                Console.Write(arr[i]+"  ");
                
            }
            Console.WriteLine();

/*
冒泡发排序原理:大循环每循环一次会将数组中的最大的值排到数组末尾,因此第二次循环是不必再处理最后一个值,因此内循环的循环结束条件为 j < arr.Length-i-1,而内循环没循环一次是将相邻两个数字中大的排在后面,小的书排在前面。
*/
            for (int i=0;i<arr.Length -1;i++)  //大循环将最大的数排到末尾
            {
                for (int j = 0; j < arr.Length-i-1; j++) //arr.Length,获取数组长度。
                {
                    if (arr[j] > arr[j + 1])
                    {
                        t = arr[j];
                        arr[j] = arr[j + 1];
                        arr[j + 1] = t;
                    }
                }
            }
            
            foreach (int number in arr)
            {
                Console.Write(number + " ");  //黑框显示数组元素;
            }
            Console.ReadLine();
        }

1.3ArrayList类

ArrayList类相当于一个高级的动态数组。
与数组的不同:

  • 数组的容量是固定的,而ArrayList的容量是可以扩充的;
  • ArrayList只能是一维形式,而数组可以是多维德;
  • ArrayList提供添加、删除和插入某一范围元素的方法,但在数组中,只能一次获取或者设置一个元素的值。

ArrayList的定义和初始化:

   ArrayList LIST= new ArrayList();
   for(int i=0;i<10;i++)            //为ArrayList添加10个int元素。
   {
          LIST.Add(i);
   }

    //直接用数组赋值;
        int[] a1 = new int[] { 1,2,3,4,5,6};
        ArrayList a2 = new ArrayList(a1);

ArrayList元素的添加:
1.add方法:

            int[] a1 = new int[] { 1,2,3,4,5,6};
            ArrayList a2 = new ArrayList(a1);
            a2.Add(7);

2.insert方法:

            int[] a1 = new int[] { 1,2,3,4,5,6};
            ArrayList a2 = new ArrayList(a1);
            a2.Insert(3, 7);       //在第三个位置处插入值7;

ArrayList元素的删除:

         a2.Clear();             //清除ArrayList 中的所有元素;
         a2.Remove(3)       //移除第一个与3匹配的元素;(也就a2中第一个等于3的元素)
        a2.RemoveAt(3)     //移除索引为3的元素;
        a2.Contion(2)         //a2中是否有2这个元素。有的话返回true;

1.4 Hashtable(哈希表)

哈希表,表示键/值对的集合,这些键/值对根据键的哈希代码进行组织。他的每一个元素都是一个存储在DictionaryEntry对象中的键/值对。键不能为空引用但值可以。

            Hashtable hashtable = new Hashtable();
            hashtable.Add("id", "BH0001");
            hashtable.Add("name", "TN");
            hashtable.Add("sex", "男");
            hashtable.Clear();  //移除哈希表中的元素;

2.方法

2.1方法的分类:

方法可以分为静态方法和非静态方法,以是否含有static修饰符来区分;静态方法不对特定的实例进行操作,在静态方法中引用this会出现错误。

2.2方法的重载:

        public int ADD(int a,int b)
        {
            return a + b;
        }

        public static double ADD(int a, double b)
        {
            return a + b;
        }

        public int ADD(int a, int b,int c)
        {
            return a + b + c;
        }

异常处理:

try
{

}
catch{

}

3. 类与对象:

一些概念:

  • 类: 也就是定义的类类型,在.NET Framework类库中定义了很多类,如string类,Object类,Math类等;
  • 对象: 也称为类的实例。对象必须被实例化才能正常使用。

3.1 属性和字段:

3.1.1 字段:

    class CAuto
    {
        public string myname;           //myname就是CAout的一个字段;
    }

3.1.2 属性:

属性是一种用于访问对象或类的特性的成员。属性可以包括字符串的长度、字体的大小,窗体的标题和客户的命称等。属性是字段和方法的交集,看起来像字段,用起来像方法。
属性有访问器,访问器指定在他们的值被读取或写入时需要执行的语句。

     public  class Data
    {
        private  int  Day = 7;
        public int day           //星期属性,该属性为可读可写;
        {
              get {return Day; }
              set { 
                      if((value>0)&&(value<8)
                      {
                       Day=value;
                      }
                  }
        }
    }

get访问器与方法类似,他必须返回属性类型的值,而set访问器类似于返回类型为void的方法,他使用称为value的隐式参数,此参数的类型是属性的类型。

3.2.2 属性的使用:

对象名.属性名

  • 如果要在其他类中调用自定义属性,必须将自定义属性的级别设置为public。
  • 如果属性为只读属性,不能再调用时为其赋值,否则产生异常。
    class MyClass
    {
        private string id;
        private string name = "";
        public string ID
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
            }
        }
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
    }

 class Program
    {
        static void Main(string[] args)
        {
            MyClass myclass = new MyClass();       // 实例化MyClass类对象;
            myclass.ID = "BH001";                  //为用户编号属性赋值;
            myclass.Name = "TM1";                  //为用户姓名属性赋值;

            Console.WriteLine(myclass.ID+ " "+myclass.Name);
            
        } 
    }

只读属性:只读属性就是只能读取数据的属性,值需要get代码块就可以了。

public class Cperson
{
          //Name 属性 ,只读
          private string myname;
          public strring Name
            {
               get {return myname};
            }
}

在使用只读属性时应注意,由于不能设置属性的数据,所以只能通过其他方式来设定它的数据,例如:

  • 通过类的构造函数带入数据,并使用一个内部变量保存它。
  • 通过其他数据的计算得到。‘

只写属性:只写属性是指只能写入数据的属性,这种属性并不常用,定义只写属性只需要set语句块。

3.2.3 字段与属性

字段与属性在调用形式上是相同的,那么对于简单的属性,为什么不选择直接使用字段呢?
那是因为,接口只能定义属性、方法和索引器成员,不允许出现字段。所以在软件开发中,对于类的公共数据成员会更多的使用属性,而字段多用于对象内部数据的处理。

3.2构造函数

    class MyClass
    {
        public string Name { get; set; }
        public MyClass(string sN)
        {
            Name = sN;
        }
    }

            MyClass tom = new MyClass("TOM");
            Console.WriteLine(tom.Name);

3.3 类的继承:

任何类都可以从另外一个类继承,也就是说,这个类拥有它继承的类的所有成员,在面向对象编程中,被继承的类称为父类或基类。C#中支持单继承,不支持多重继承。即一次只允许继承一个类,不允许继承多个类。

继承一个类时,子类不能访问基类的私有成员。

protected,只有子类才能访问protected成员,基类和外部代码都不能访问protected成员。

除了成员的保护级别外,还可以为成员定义其继承行为。基类的成员可以是虚拟的,成员可以由继承他的类重写。

此外,基类还可以定义为抽象类,抽象类不能直接实例化,要使用抽象类就必须继承这个类然后在实例化。

    public class MyClass
    {
        private int x = 0;
        private int y = 0;
        public int X
        {
            get
            {
                return x;
            }
            set
            {
                x = value;
            }
        }
        public int Y
        {
            get
            {
                return y;
            }
            set
            {
                y = value;
            }
        }

        public int add()
        {
            return X + Y;
        }
    }

    class MyClass2:MyClass
    {
        private int z=0;
        public int Z
        {
            get{
                return z;
            } 
            set
            {
                z=value;
            }
        }

        public int ADD2()
        {
            return X+Y+Z;
        }
    }

        static void Main(string[] args)
        {
            MyClass myclass1 = new MyClass();
            MyClass2 myclass2 = new MyClass2();
            myclass1.X = 3;
            myclass1.Y = 4;
            myclass2.X = 3;
            myclass2.Y = 5;
            myclass2.Z = 7;
            Console.WriteLine(myclass1.add());
            Console.WriteLine(myclass2.add());
            Console.WriteLine(myclass2.ADD2());
            
        } 

类的多态:

多态使子类的实例可以直接赋予基类的变量。

namespace 多边形类
{
    class Polygon                             //创建基类
    {
        private int length;
        private int sides;                    
        public int Length
        {
            get { return length; }
            set { length = value; }
        }
        public int Sides
        {
            get { return sides; }
            set { sides = value; }
        }
        public Polygon()
        {
            Length = 1;
        }
        public virtual string GetPeri()         //定义了一个virtual类虚方法,以便在子类中重写该方法。
        {
            return "This is a virtual mothod";
        }

    }
    class Square:Polygon
    {
        public Square()           //子类构造函数,直接初始化Sides=4;
        {
            Sides = 4;
        }
        public override string GetPeri()
        {
            int x = Length * Sides;
            return "the perimeter of the Polygon is " + x;
        }
    }
    class Pentagon:Polygon
    {
        public Pentagon()
        {
            Sides = 5;
        }
        public override string GetPeri()
        {
            int x = Length * Sides;
            return "the perimeter of the Polygon is " + x;
        }
    }
}
    class Program
    {
        static void Main(string[] args)
        {
            Square s = new Square();
            s.Length = 2;
            Console.WriteLine(s.GetPeri());
            Pentagon p = new Pentagon();
            p.Length = 5;
            Console.WriteLine(p.GetPeri());
        }
    }

抽象类与抽象方法

如果一个类不与具体的事物相联系,而只是表达一种抽象概念,仅仅是作为其派生类的一个基类,这样的类就是抽象类。

  • 抽象类不能直接实例化。
  • 抽象类中可以包含抽象成员,非抽象类中不行。
  • 抽象类不能被密封。
public abstract class myClass                       //抽象类的的定义。
{
    public abstract void method();                   //抽象方法
}
namespace 动物类
{
    class Animal
    {
        private bool sex;
        private string sound;
        public bool Sex
        {
            get { return sex; }
            set
            {
                sex = value;
            }
        }
        public string Sound
        {
            get{return sound;}
            set { sound = value; }
        }
        public Animal()
        {
            Sex = false;
        }
        public abstract string Road();  //抽象方法Road(),用于模拟动物发出的叫声。


    }
    class Cat:Animal
    {
        public Cat()
        {
            Sound ="Miaow";
        }
        public override string Road()    //在子类中事项Road()方法;
        {
            return "Cat"+this.Sound ;
        }
    }
    class Cow : Animal
    {
        public Cow()
        {
            Sound = "Moo";
        }
        public override string Road() //在子类中事项Road()方法;
        {
            return "Cow" + this.Sound;
        }
    }

        static void Main(string[] args)
        {
            Cat ca = new Cat();
            Console.WriteLine(ca.Road());
            Cat co = new Cat();
            Console.WriteLine(co.Road());
        }

接口:

在生活中,接口非常常见,如电源接口和电脑中的各种接口,使用这些接口可以根据需要,连接不同功能的设备。软件开发中,也有类似的接口。

1.创建接口:

public interface IAuto
{
  string Model {get;set;}
  int MaxSpeed{get;set;}
  void Drive();
  void Retrun();
}

在定义CAutoBase类时,使用冒号,表示CAutoBase类实现了IAuto接口。当一个类实现某个接口时,就必须实现这个接口中的所有成员。

public class CAutoBase : IAuto
{
    //构造函数:
    public  CAutoBase(string sModel="", int iMaxSpeed= 180)
    {
       Model=sModel;
       MaxSpeed=iMaxSpeed;
    }
     //实现接口成员:
     public string Model {get; set;}
     public int MaxSpeed {get; set;}
     public void Drive()
     {
           console.WriteLine("{0}型汽车正在前进",Model1);
     }
     public void Return()
     {
           console.WriteLine("{0}型汽车正在倒退",Model1);
     }
}

上述接口代码中分别定义了4个成员,Model属性、MasSpeed属性、Driver()方法和Return()方法。

在定义接口成员时,并不需要指定成员的访问级别,因为这些接口并不会做任何的实际操作,而是完全由需要实现接口的类完成具体的工作。

如果我们从设计的角度来看.一个项目中用若干个类需要去编写,由于这些类比较复杂,工作量比较大,这样每个类就需要占用一个工作人员进行编写.比如A程序员去定Dog类,B程序员去写Cat类.这两个类本来没什么联系的,可是由于用户需要他们都实现一个关于"叫"的方法.这就要对他们进行一种约束.让他们都继承于IBark接口,目的是方便统一管理.另一个是方便调用.当然了,不使用接口一样可以达到目的.只不过这样的话,这种约束就不那么明显,如果这样类还有Duck类等等,比较多的时候难免有人会漏掉这样方法.所以说还是通过接口更可靠一些,约束力更强一些。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值