深入类的方法

1.构造函数

  通过将类进行实例化,创建类实例的方法被称为构造函数。

  特点:

              1.方法名与类型相同

              2.没有返回值类型

              3.主要完成对象的初始化工作

2.无参构造函数:没有方法体

   语法:

访问修饰符 类名(){
    //方法体
}

  示例:

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

namespace MyOffice
{
    
    /// <summary>
    /// 员工类
    /// </summary>
    public class SE
    {
        /// <summary>
        /// 无参构造函数:设置属性初始值

        /// </summary>
        public SE() 
        {
            this.ID = "000";
            this.Age = 20;
            this.Name = "无名氏";
            this.Gender = Gender.male;
            this.Popularity = 0;
        }

        public string ID { get; set; }
        /// <summary>
        /// 年龄
        /// </summary>
        public int Age { get; set; }

        /// <summary>
        /// 姓名
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 性别
        /// </summary>
        public Gender Gender { get; set; }

        /// <summary>
        /// 人气值

        /// </summary>
        private int _popularity;
        public int Popularity
        {
            get { return _popularity; }
            set { _popularity = value; }
        }

        public string SayHi() 
        {
            string message = string.Format("大家好,我是 {0}, 今年 {1}岁,工号是 {2},我的人气值高达 {3}!",this.Name,this.Age,this.ID,this.Popularity);
            return message;
        }
    }
}

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

namespace MyOffice
{
    /// <summary>
    /// 性别枚举
    /// </summary>
    public enum Gender
    {
        male,female
    }
}

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

namespace MyOffice
{
    class Program
    {
        static void Main(string[] args)
        {
            SE engineer = new SE();                    
            Console.WriteLine(engineer.SayHi());
            Console.ReadLine();
        }
    }
}


1

3.带参构造函数:

  语法:

访问修饰符 类名(参数列表)
{
     //方法体
}

  示例:

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

namespace MyOffice
{
    
    /// <summary>
    /// 员工类
    /// </summary>
    public class SE
    {
        /// <summary>
        /// 带参构造函数

        /// </summary>
        /// <param name="id">工号</param>
        /// <param name="name">姓名</param>
        /// <param name="age">年龄</param>
        /// <param name="gender">性别</param>
        /// <param name="popularity">人气值</param>
        public SE(string id, string name, int age, Gender gender,int popularity) 
        {
            this.ID = id;
            this.Name = name;
            this.Age = age;
            this.Gender = gender;
            this.Popularity = popularity;
        }

        public string ID { get; set; }
        /// <summary>
        /// 年龄
        /// </summary>
        public int Age { get; set; }

        /// <summary>
        /// 姓名
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 性别
        /// </summary>
        public Gender Gender { get; set; }

        /// <summary>
        /// 人气值

        /// </summary>
        private int _popularity;
        public int Popularity
        {
            get { return _popularity; }
            set { _popularity = value; }
        }

        public string SayHi() 
        {
            string message = string.Format("大家好,我是 {0}, 今年 {1}岁,工号是 {2},我的人气值高达 {3}!",this.Name,this.Age,this.ID,this.Popularity);
            return message;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyOffice
{
    /// <summary>
    /// 性别枚举
    /// </summary>
    public enum Gender
    {
        male,female
    }
}

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

namespace MyOffice
{
    class Program
    {
        static void Main(string[] args)
        {
            //实例化一个员工(程序员)对象
            SE engineer = new SE("112", "艾边成", 25, Gender.male, 100);

            Console.WriteLine(engineer.SayHi());

            Console.ReadLine();
        }
    }
}

2

4.方法重载

 构造函数的重载:

在同一个类中,如果多个方法,方法名称相同,但是参数列表(个数顺序,属性类型)不同的多个方法,可以称为方法重载

示例:

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

namespace MyOffice
{
    /// <summary>
    /// 薪水计算类,用重载的方法实现项目经理和程序员的薪水计算
    /// </summary>
    class CompSalary
    {
        /// <summary>
        /// 项目经理的薪水计算
        /// </summary>
        /// <param name="pm">项目经理对象</param>
        public static void Pay(PM pm)
        {
            float money = pm.BasePay + pm.MgrPrize + pm.Bonus;
            Console.WriteLine("项目经理的薪水 :" + money);
        }

        /// <summary>
        /// 程序员的薪水计算

        /// </summary>
        /// <param name="pm">程序员对象</param>
        public static void Pay(SE se)
        {
            float money = se.BasePay + se.MeritPay;
            Console.WriteLine("程序员的薪水:" + money);
        }

    }
}

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

namespace MyOffice
{
    /// <summary>
    /// 项目经理类
    /// </summary>
    class PM
    {
        public PM(string id, string name,int age, Gender gender, int yearOfExperience)
        {
            this.ID = id;
            this.Name = name;
            this.Age = age;
            this.Gender = gender;
            this.YearOfExperience = yearOfExperience;
        }

        public PM() { }

        /// <summary>
        /// ID
        /// </summary>
        private string _id;
        public string ID
        {
            set { _id = value; }
            get { return _id; }
        }
        /// <summary>
        /// 年龄
        /// </summary>
        private int _age;
        public int Age   
        {
            get { return _age; }
            set 
            {
                if (value >= 30 && value <= 100)
                {
                    _age = value;
                }
                else 
                {
                    _age = 30;
                }
            }
        }

        /// <summary>
        /// 姓名
        /// </summary>
        private string _name; 
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        /// <summary>
        /// 性别
        /// </summary>
        private Gender _gender; 
        public Gender Gender
        {
            get { return _gender; }
            set { _gender = value; }
        }

        /// <summary>
        /// 资历
        /// </summary>
        private int _yearOfExperience; 
        public int YearOfExperience
        {
            get { return _yearOfExperience; }
            set { _yearOfExperience = value; }
        }

        /// <summary>
        /// 基础工资
        /// </summary>
        public float BasePay { get; set; }

        /// <summary>
        /// 项目奖金
        /// </summary>
        public float MgrPrize { get; set; }
       

        /// <summary>
        /// 分红
        /// </summary>
        public float Bonus { get; set; }


        /// <summary>
        /// 问好
        /// </summary>
        /// <returns>问好的内容</returns>
        public string SayHi() 
        {
            string message;
            message = string.Format(
                "大家好,我是 {0} ,今年 {1} 岁,项目管理经验 {2}年。",
                this._name, this._age, this._yearOfExperience
            );
            return message;
        }
    }
}


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

namespace MyOffice
{
    
    /// <summary>
    /// 员工类
    /// </summary>
    public class SE
    {
        /// <summary>
        /// 带参构造函数

        /// </summary>
        /// <param name="id">工号</param>
        /// <param name="name">姓名</param>
        /// <param name="age">年龄</param>
        /// <param name="gender">性别</param>
        /// <param name="popularity">人气值</param>
        public SE(string id, string name, int age, Gender gender,int popularity) 
        {
            this.ID = id;
            this.Name = name;
            this.Age = age;
            this.Gender = gender;
            this.Popularity = popularity;
        }

        public string ID { get; set; }
        /// <summary>
        /// 年龄
        /// </summary>
        public int Age { get; set; }

        /// <summary>
        /// 姓名
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 性别
        /// </summary>
        public Gender Gender { get; set; }

        /// <summary>
        /// 人气值

        /// </summary>
        private int _popularity;
        public int Popularity
        {
            get { return _popularity; }
            set { _popularity = value; }
        }

        /// <summary>
        /// 基本工资
        /// </summary>
        public float BasePay { get; set; }


        /// <summary>
        /// 考核工资
        /// </summary>
        public float MeritPay { get; set; }

        public string SayHi() 
        {
            string message = string.Format("大家好,我是 {0}, 今年 {1}岁,工号是 {2},我的人气值高达 {3}!",this.Name,this.Age,this.ID,this.Popularity);
            return message;
        }
    }
}





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

namespace MyOffice
{
    /// <summary>
    /// 性别枚举
    /// </summary>
    public enum Gender
    {
        male,female
    }
}

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

namespace MyOffice
{
    class Program
    {
        static void Main(string[] args)
        {
            //实例化一个员工(程序员)对象
            SE engineer = new SE("112", "艾边成", 25, Gender.male, 100);
            engineer.BasePay = 4000;
            engineer.MeritPay = 3000;


            //实例化一个PM
            PM joe = new PM("890", "乔布斯",50,Gender.male, 10);
            joe.BasePay = 8000;
            joe.MgrPrize = 4000;
            joe.Bonus = 2000;

            //计算工资
            CompSalary.Pay(engineer);
            CompSalary.Pay(joe);

            Console.ReadLine();
        }
    }
}

3

 

 5.对象交互:

   每个类都有自己的特性和功能,我们把它们封装为属性和方法。对象之间通过属性和方法

   进行传递,可以认为方法的参数及方  法的返回值都是对象间相互传递的信息

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值