我学到的C#——基础

习惯了利用Matlab、Bash等面向过程的语言直接对数据或文件操作。由于工作需要,要用到C#语言,什么循环判断以及其它的技术细节都容易理解,但对其面向对象的机制及程序结构比较茫然。网上查了一些资料,有了一个初步的理解,写了一个简单的联系程序,记录如下。

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

namespace ConsoleApplication1
{
  /*********************************************************************************/
    public struct myColor
        //一个结构体
    {
        public int red;
        public int green;
        public int blue;
    }
    /***************************************************************************/
    public enum manOrWoman {man,woman} //一个枚举
/********************************************************************************/
    public class Program
    {
        static void Main(string[] args)
            /*Main方法是程序的入口点,C#中必须包含一个Main方法,在该方法中可以创建对象和调用其他方法,
             一个C#程序中只能有一个Main方法,并且在C#中所有的Main方法都是静态的。
             * Main方法必须是类或者结构的静态方法,并且其返回类型必须是int或void*/
            // public:说明Main方法是共用的,在类的外面可以调用整个方法。
            /*static:说明方法是一个静态方法,即这个方法属于类的本身而不是这个类的特定对象。
             * 调用静态方法不能使用类的实例化对象,必须使用类名来调用。*/
            // void:此修饰符说明方法无返回值。
        {
            myColor m; //结构的实例化不用new算符
            m.blue=100;
            m.green=100;
            m.red=100;
            Console.WriteLine("第一个结构体" + Convert.ToString(m.blue));

            manOrWoman sex = manOrWoman.man; //枚举的实例
            switch (sex)
            {
                case manOrWoman.man:
                    Console.WriteLine("第一个枚举.{0}",sex);
                    break;
                case manOrWoman.woman:
                    Console.WriteLine("第一个枚举.{0}", sex);
                    break;
            }
                                 
            people ren = new people(); //类的实例,后面有people类
            Console.WriteLine(ren.Say("I like ", "apple!")); //类的方法
            ren.name = "zhangsan"; //类的属性
            Console.WriteLine(ren.name);

            animal dog = new animal(); //类的实例,后面有animal类
            Console.WriteLine(dog.GetHashCode());//类的方法,所有的类都继承子object类,也继承了该类的方法,GetHashCode是该类的方法之一
            dog.name = "Tiger"; //animal类继承了people类的属性和方法
            dog.ID = "9527";
            dog.foot = 4;
            Console.WriteLine("The dog's name is {0}, its ID is {1}. It has {2} foot.{3}{4}",
                dog.name,dog.ID,dog.foot,dog.Say("I eat ","bones."),dog.eat("OK!"));
                       
            const double pi = 3.14;
            myfun();
            int num1 = 1, num2 = 2;
            double num3 = 2 * pi;
            int[] num = new int[3] { 4, 5, 6 };
            Console.WriteLine("Do you like fruite?"+qiuhe(num1,num2)+Convert.ToChar(num[2]));
            string str = ipt();
            if (str == "liulian")
            {
                Console.WriteLine("I like liulian too.");
            }
            else
            {
                Console.WriteLine("I lile " + str);
            }
            Console.ReadLine();
        }

        //方法一
        static void myfun()
        {
            //没有参数与返回值,当作功能方法用
            //由Main方法直接使用的方法,需要加static,表示静态方法
            //方法定义在类的里面,其他方法的外面
            int a = 111; //方法中定义的变量只在该方法中起作用,称为局部变量
            Console.WriteLine("Hello World!"+Convert.ToString(a));
        }

        //方法二
        static string ipt()
        {
            //带有返回值的方法,返回类型由void改为目标类型
            Console.WriteLine("Input:");
            string str = Console.ReadLine();
            return str; //方法结束要有return
        }

        //方法三
        static string qiuhe(int num1, int num2)
        {
            //带有参数的方法
            return Convert.ToString(num1 + num2);
        }
    }

    /***************************************************************************/
    public class people
        //构造一个类
    {
        //类的属性
        public string name;
        public string ID;

        //类的方法
        public string Say(string s1,string s2)
        {
            return s1 + s2;
        }
    }//*/

    public class animal:people
    {
        public int foot;
        public int eat(string str)
        {
            Console.WriteLine("I eat "+str);
            return 0;
        }
    }
}

posted @ 2013-01-06 22:40  秋天掰穗人  阅读(244)  评论(0编辑  收藏  举报