第十三节 静态方法和成员变量(实例方法)、方法重载

View Code
案例1:静态方法和成员变量(实例方法)

class MySwap
    {
        public static void Swap(ref int num1, ref int num2)
        {
            int temp;
            temp = num1;
            num1 = num2;
            num2 = temp;       
        }
    }

————————————————————————————————————————————————————————————

案例1.1

 class Program
    {
        static void Main(string[] args)
        {
            int num1 = 5;
            int num2 = 10;
            Console.WriteLine("交换前:num1={0},num2={1}",num1,num2);
            /*一个类中的静态方法是属于类的,而不是属于对象。
             * 调用静态方法通过类名.方法名来调用
             * 
             */

            /*实例方法属于对象,需要创建对象的方式来调用
             * 例如 A a=new A();创建了一个a对象,通过:a.方法名来调用
             * 
             */

            /*要理解静态方法和实例方法和简单,举个例子。人的重量是与实际的对象也就是
             * 张三、李四这些实际的对象有关,所以我们会把计算人的重量的操作写成实例方法
             * 比如说人类的总人数就是和类有关的,所以我们通常会将他封装成静态方法
             */
            MySwap.Swap(ref num1, ref num2);
            Console.WriteLine("交换后:num1={0},num2={1}", num1, num2);
            Console.ReadKey();
        }
    }

——————————————————————————————————————————————————————

——————————————————————————————————————————————————————

案例2 静态的成员只能调用静态的成员,不能调用实例的成员


 class Program
    {
        static void Main(string[] args)
        {
            //静态的成员只能调用静态的成员,不能调用实例的成员
            //如果需要调用必须在静态成员中实例化后调用

            Sub(5, 8);
            Program p = new Program();
            p.Add(5,10);

            Console.ReadKey();
        }


        public int Add(int num1, int num2)
        {
            //实例的成员可以调用静态成员
            //就像张三这个对象想获取人类的总人数一样
            num1 = Sub(10, 4);
            
            return num1 + num2;
        }

        public static int Sub(int num1, int num2)
        {

            return num1 - num2;
        
        }
    }

————————————————————————————————————————————————————————

————————————————————————————————————————————————————————————

案例3:创建一个人类

 class Human
    {
        private string name;
        public string Name
        {
            set{ name = value; }
            get{return name;}
        }


        private int age;


        public int Age
        {
            set { age = value; }
            get { return age; }
        
        }

 


        private bool CheckAge(int age)
        {
            if (age >= 18 && age <= 120)
            {
                return true;
            }
            return false;

        }

       public void Introduction()
        {
           //在一个类中的方法调用一个方法只需通过:方法名(参数...)调用即可
            bool flag = CheckAge(Age);
            if (flag == false)
                age = 18;
            Console.WriteLine("姓名:{0}\t年龄:{1}", Name, Age);
        }
    }

——————————————————————————————————————————————————————

案例3.1

 class Program
    {
        static void Main(string[] args)
        {
            Human a = new Human();
           
            Console.Write("请输入姓名:");
            string y = Console.ReadLine();
            Console.Write("请输入年龄:");
            int x = int.Parse(Console.ReadLine());

            a.Age = x;
            a.Name = y;

 

            a.Introduction();
            Console.ReadKey();

        }
    }
View Code
案例4 创建一个雇员类

 class Emoloyee //Emoloyee 雇员
    {
        private string xm;
        //属性叫聪明字段
        public string XM
        {
            set
            {
                while (true)
                {
                    if (value.Length >= 2)
                    {
                        xm = value; break;
                    }
                    else
                    {
                        Console.WriteLine("姓名必须大于或等于2个字符");
                        Console.Write("请重新输入你的姓名:");
                        value = Console.ReadLine();
                    }
                }
            }
            get { return xm; }
        }

        private int nl;
        public int NL
        {
            set
            {
                while (true)
                {
                    if (value >= 18 && value <= 60)
                    {
                        nl = value; break;
                    }
                    else
                    {
                        Console.WriteLine("年龄必须在18-60之间");
                        Console.Write("请重新输入你的年龄:");
                        value = int.Parse(Console.ReadLine());
                    }

                }

            }
            get { return nl; }
        }


        private decimal xs;
        public decimal XS
        {
            set
            {
                while (true)
                {
                    if (value >= 0)
                    {
                        xs = value; break;
                    }
                    else
                    {
                        Console.WriteLine("薪水必须是有效的数值");
                        Console.Write("请重新输入你的薪水:");
                        value = decimal.Parse(Console.ReadLine());
                    }
                }

            }
            get { return xs; }
        }

        private string jj;
        public string JJ
        {
            set { jj = value; }
            get { return jj; }
        }

        private int gznx;
        public int GZNX
        {
            set
            {
                while (true)
                {
                    if (value >= 0 && value <= 70)
                    {
                        gznx = value; break;
                    }
                    else
                    {
                        Console.WriteLine("工作年限必须是有效的数值,并且是有效的");
                        Console.Write("请重新输入你的工作年限:");
                        value = int.Parse(Console.ReadLine());
                    }

                }

            }

            get { return gznx; }
        }
        private string zw;
        public string ZW
        {
            set
            {
                if (value.Equals("部门经理"))
                {
                    jj = "有奖金";
                    zw = "部门经理";
                }
                else
                {
                    zw = value;
                    jj = "无奖金";
                }
            }
            get { return zw; }
        }

        private string xwei;
        public string Xwei
        {
            set { xwei = value; }
            get { return xwei; }

        }

 

 

 

        public void dddP()
        {
            Console.WriteLine();

            Console.WriteLine("姓名:{0}", xm);
            Console.WriteLine("年龄:{0}", nl);
            Console.WriteLine("薪水:{0}", xs);
            Console.WriteLine("奖金:{0}", jj);
            Console.WriteLine("工作年限:{0}", gznx);
            Console.WriteLine("职位:{0}", zw);
            Console.WriteLine("行为:{0}", xwei);
        }

    }

————————————————————————————————————————————————————

案例4.1  

class Program
    {
        static void Main(string[] args)
        {
            /*创建一个雇员类 (Emoloyee类)
             *状态:姓名,年龄,薪水,奖金,工作年限,职位 
             *属性: 
             *姓名必须大于或等于2个字符 
             *年龄必须在18-60之间 
             *薪水必须是有效的数值 
             *奖金要求判断是否是部门经理,是则有奖金否则无 
             *工作年限必须是有效的数值,并且是有效的 
             *职位.......... 
             *行为:自我介绍  
             * */

            Emoloyee aa = new Emoloyee();
            Console.Write("请输入你的姓名:");
            string w1 = Console.ReadLine();
            aa.XM = w1;

            Console.Write("请输入你的年龄:");
            int w2 = int.Parse(Console.ReadLine());
            aa.NL = w2;

            Console.Write("请输入你的薪水:");
            decimal w3 = decimal.Parse(Console.ReadLine());
            aa.XS = w3;

            Console.Write("你的工作年限是:");
            int w5 = int.Parse(Console.ReadLine());
            aa.GZNX = w5;

            Console.Write("你的职位是:");
            string w6 = Console.ReadLine();
            aa.ZW=w6;

           Console.Write("你的行为:");
            string w7 = Console.ReadLine();

            aa.Xwei = w7;

            aa.dddP();
            Console.ReadKey();

        }
    }

——————————————————————————————————————————————————————————————————————————————————————————————————————————————————

案例5 重载方法

class Calcuator
    {
        public int Add(int num1, int num2)
        {
            return num1 + num2;
        }

        public double Add(int num1, double num2)//
        {
            return num1 + num2;
        }

        public int Add(int num1, int num2,int num3)//多个
        {
            return num1 + num2+num3;
        }

        public String Add(string str1, string str2)
        {
            return str1 + str2;
        }
        public double Add(double d1, double d2)
        {
            return d1 + d2;
        }

        //如果仅仅是方法返回值不同是不能构成重载的
        //public decimal add(double d1, double d2)
        //{
        //    return d1 + d2;
        //}

    }

————————————————————————————————————————————————————

案例5.1

 class Program
    {
        static void Main(string[] args)
        {
            //方法重载:在一个类中,方法名相同,参数类型不同,或参数的个数不同构成方法重载
            //方法重载的好处就是不需要判断你所传递的参数数据类型是什么。直接传递参数,
            //编译器会根据你所传的参数匹配的方法来执行!
            Calcuator c1 = new Calcuator();
            int num1 = 5;
            int num2 = 10;
            c1.Add(num1, num2);
            Console.WriteLine("int的Add方法:{0}", c1.Add(num1, num2));

            double d1 = 1.2;
            double d2 = 1.7;
            c1.Add(d1, d2);
            Console.WriteLine("double的Add方法:{0}", c1.Add(d1, d2));

            string str1 = "hello";
            string str2 = "word";
            c1.Add(str1, str2);
            Console.WriteLine("string的Add方法:{0}", c1.Add(str1, str2));
            Console.ReadKey();
           
        }
    }

——————————————————————————————————————————————————————————————————————————————————————————————————————————————————

案例6  编写程序  用于计算员工的工资

 class ComputeSalary
    {
       
        public string Xm(string xm)
        {
            return xm;
           
        }

        public decimal qq(decimal gz)
        {
            return gz;
        }

        public decimal qq(decimal gz,decimal zfjt)
        {
            return gz+zfjt;
        }

        public decimal qq(decimal gz, decimal zfjt,decimal jj)
        {
            return  gz + zfjt + jj;
        }
                  
    }

————————————————————————————————————————————————————————

案例6.1

class Program
    {
        static void Main(string[] args)
        {
            ComputeSalary aa=new ComputeSalary();

                 string f2;
              
               do
                {
                  
                    Console.Write("请选中职位:(1.普通员工、2.部门经理、3.总经理)");
                    int f1 = int.Parse(Console.ReadLine());
                    if (f1 == 1)
                    {
                        Console.Write("请输入员工的姓名:");
                        string xm = Console.ReadLine();
                        aa.Xm(xm);
                        Console.Write("请输入你的工资:");
                        decimal w1 = decimal.Parse(Console.ReadLine());
                        aa.qq(w1);
                        Console.WriteLine("工资为:{0}", w1);
                    }

                    else if (f1 == 2)
                    {
                        Console.Write("请输入员工的姓名:");
                        string xm = Console.ReadLine();
                        aa.Xm(xm);
                        Console.Write("请输入你的工资:");
                        decimal w1 = decimal.Parse(Console.ReadLine());
                        Console.Write("请输入你的住房津贴:");
                        decimal w2 = decimal.Parse(Console.ReadLine());
                        aa.qq(w1, w2);
                        Console.WriteLine("工资为:{0},住房津贴:{1},合计:{2}", w1, w2, aa.qq(w1, w2));
                    }

                    else if (f1 == 3)
                    {
                        Console.Write("请输入员工的姓名:");
                        string xm = Console.ReadLine();
                        aa.Xm(xm);
                        Console.Write("请输入你的工资:");
                        decimal w1 = decimal.Parse(Console.ReadLine());
                        Console.Write("请输入你的住房津贴:");
                        decimal w2 = decimal.Parse(Console.ReadLine());
                        Console.Write("请输入你的奖金:");
                        decimal w3 = decimal.Parse(Console.ReadLine());
                        aa.qq(w1, w2, w3);
                        Console.WriteLine("工资为:{0},住房津贴:{1},奖金:{2}合计:{3}", w1, w2, w3, aa.qq(w1, w2, w3));
                    }
                    else
                    {
                        Console.WriteLine("输入有误,请重新输入");
                    }

                    Console.Write("是否继续:<Y/N>:");
                    f2 = Console.ReadLine().ToLower();
                } while (f2.Equals("y"));
           
            

            Console.ReadKey();

        }


    }

 

posted @ 2012-06-20 20:54  ComBat  阅读(236)  评论(0编辑  收藏  举报