C#基础篇八构造函数和面向对象思想

3.关于 对象创建的几个关键词
Dog d1 = new Dog();

Dog d1 叫做 声明变量
new Dog() 叫做 实例化(创建)对象

4.关于对象、方法和 this 的关系
Dog d1 = new Dog();//在new关键字 开辟堆空间,创建完对象,开始调用构造函数的时候,会把对象的地址 传给 构造函数里的 this
d1.ShowLove();//在调用方法时,会先将 d1里保存的 对象地址 传给 方法里的 this,然后再执行方法体;

5.静态方法 只能 访问 静态成员,不能访问 实例成员(变量和方法)!
实例成员 可以 访问静态成员,也可以访问 实例成员!
实例成员(就是堆空间里某个对象的成员)
*重要区别:【静态方法】的访问 是通过 类名访问,方法里的没有this!
【实例方法】的方法 是通过 对象访问-》会将此对象 传给 方法里的this

7.构造函数(构造方法):
语法: 访问修饰符 类名(参数列表) {方法体代码}
构造函数也有 重载(方法名一样,但是参数的个数或类型不一样)~~!
构造函数调用实际:new 关键字 在实例化对象的时候调用!不能在其它方法中直接调用!

构造函数之间 可以相互调用:通过 :this(参数列表) 来调用!

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

namespace P01构造函数
{
    //类只有两个访问修饰符: public(公共的) internal(程序集共享 - 默认)
    /// <summary>
    /// 男孩类
    /// </summary>
    public class Boy
    {
        /// <summary>
        /// 姓名
        /// </summary>
        public string name = "aa";

        /// <summary>
        /// 年龄
        /// </summary>
        public int age;

        #region 各种 构造函数
        public Boy(string name)
        {
            this.name = name;//将形参 赋给 对象里的 name
        }

        public Boy(int age)
        {
            //如果 年龄 不符合 自然规则,则默认设置给对象 年龄为 18 岁
            if (age < 0 || age > 100)
            {
                this.age = 18;
            }
            else
            {
                this.age = age;//将形参 赋给 对象里的 age
            }
        }

        public Boy(string name, int age1)
            : this(age1)//调用当前类的 另外一个构造函数!
        {
            this.name = name;

            //访问静态成员:类名.静态成员名
            //Boy.boyNum++;
            // 如果 访问代码 和静态成员 在同一个类中,可以简写成:
            boyNum++;
        } 
        #endregion

        #region 1.0 向指定的 MM 表白 void ShowLove(string theGirlName)
        /// <summary>
        /// 向指定的 MM 表白
        /// </summary>
        /// <param name="theGirlName">要表白的女孩名字</param>
        public void ShowLove(string theGirlName)
        {
            Console.WriteLine("Hi~~~{0},你在等我吗?我叫{1},今年{2}岁了,我不帅,但就TM有钱!", theGirlName, this.name, this.age);
        } 
        #endregion

        /// <summary>
        /// 男孩的总个数
        /// </summary>
        public static int boyNum = 0;

        #region 2.0 显示老师的总个数 void ShowBoyNum()
        /// <summary>
        /// 显示老师的总个数
        /// </summary>
        public static void ShowBoyNum()
        {
            //*静态成员中 不能访问 this !
            //Console.WriteLine("老师的名字:" + this.name);
            Console.WriteLine("老师总个数:" + Boy.boyNum);
        } 
        #endregion
    }
}

  

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

namespace P01构造函数
{
    class Program
    {
        static void Main(string[] args)
        {
            Boy boy1 = new Boy("大林老师",1);
            boy1.ShowLove("小林老师");

            Boy boy2 = new Boy("波波老师", 2);
            boy2.ShowLove("苍老师");

            Boy.ShowBoyNum();

            Console.ReadLine();
        }
    }

}

  

8.练习题:
商店系统V1.0
1.在商店中 可以 找 售货员 询问 商品列表(商品种类 剩余数量 单价)
2.可以选择 要购买的商品 的 种类、数量
3.售货员会将商品 计算价格 然后 “交给” 客户

分析:
1.商店类(Shop)
成员变量:
商品列表类 ProductList list;
售货员 Saler saler;
成员方法:
开始营业 void Start()
初始化商品数据 void InitProduct()

2.售货员类(Saler)
成员变量:名字 string name,性别 bool sex,年龄 int age
成员方法:
接待客户 void Welcome()
显示商品列表(商品种类 剩余数量 单价)void ShowProduct()
销售货物 void Selling()
显示商品类表 调用商店的
接收用户的输入 GetUserInput()
计算价格 Compute()

3.商品类(Product)
成员变量:商品种类 string type 剩余数量 int count 单价 decimal price

4.商品列表类-相当于仓库(ProductList)
成员变量:商品(数组)Product[] list
成员方法:
添加商品
删除商品
查询所有商品


类的编写顺序:商品->商品列表(仓库)->销售员->商店->Program

商店系统V1.0
1.在商店中 可以 找 售货员 询问 商品列表(商品种类 剩余数量 单价)
2.可以选择 要购买的商品 的 种类、数量
3.售货员会将商品 计算价格 然后 “交给” 客户

分析:
1.商店类(Shop)
成员变量:
商品列表类 ProductList list;
售货员 Saler saler;
成员方法:
开始营业 void Start()
初始化商品数据 void InitProduct()
显示商品列表(商品种类 剩余数量 单价)void ShowProduct()

2.售货员类(Saler)
成员变量:名字 string name,性别 bool sex,年龄 int age
成员方法:
接待客户 void Welcome()
销售货物 void Selling()
显示商品类表 调用商店的 ShowProduct()
接收用户的输入 GetUserInput()
计算价格 Compute()

3.商品类(Product)
成员变量:商品种类 string type 剩余数量 int count 单价 decimal price

4.商品列表类(ProductList)
成员变量:商品(数组)Product[] list
成员方法:
添加商品
删除商品
查询所有商品

工具帮助类Helper

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

namespace P02ShopV1
{
    /// <summary>
    /// 工具类
    /// </summary>
    public class Helper
    {
        #region 1.0 获取用户输入的一个 整型数值 +int GetAUserNum()
        /// <summary>
        /// 1.0 获取用户输入的一个 整型数值
        /// </summary>
        /// <returns></returns>
        public static int GetAUserNum(string strMsg)
        {
            int num=-1;
            while (true)
            {
                Console.Write(strMsg);
                string strNum = Console.ReadLine();
                if (int.TryParse(strNum, out num))
                {
                    break;
                }
                else
                {
                    Console.WriteLine("请输入数值!");
                }
            }
            return num;
        } 
        #endregion
    }
}

  

商品类Product

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

namespace P02ShopV1
{
    /// <summary>
    /// 商品类
    /// </summary>
    public class Product
    {
        /// <summary>
        /// 静态变量:保存 整个程序中 的 商品编号种子
        /// </summary>
        static int idSeed = 1;

        /// <summary>
        /// 商品编号
        /// </summary>
        public int id;

        /// <summary>
        /// 商品类型名称
        /// </summary>
        public string type;

        /// <summary>
        /// 商品价格
        /// </summary>
        public decimal price;

        /// <summary>
        /// 商品剩余数量
        /// </summary>
        public int count;

        #region 构造函数 +Product(string type, decimal price, int count)
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="type">商品类型名称</param>
        /// <param name="price">商品价格</param>
        /// <param name="count">商品剩余数量</param>
        public Product(string type, decimal price, int count)
        {
            this.id = idSeed++;//注意,是后置的++:会先赋值,在++
            this.type = type;
            this.price = price;
            this.count = count;
        } 
        #endregion
    }
}

  

商品列表类-相当于仓库(ProductList)

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

namespace P02ShopV1
{
    /// <summary>
    /// 商品类表类(维护一个商店里的所有商品,有点像【仓库】)
    /// </summary>
    public class ProductList
    {
        #region 全局变量 list,count
        /// <summary>
        /// 0.1 商品数组
        /// </summary>
        Product[] list;

        /// <summary>
        /// 0.2 商品类型的数量
        /// </summary>
        int typeCount = 0; 
        #endregion

        #region 0.0 默认构造函数 ProductList()
        /// <summary>
        /// 默认构造函数
        /// </summary>
        public ProductList()
        {
            //1.初始化 商品数组
            list = new Product[10];
        } 
        #endregion

        #region 1.0 添加商品 +void AddProduct(Product pro)
        /// <summary>
        /// 1.0 添加商品
        /// </summary>
        /// <param name="pro"></param>
        public void AddProduct(Product pro)
        {
            //1. 如果 商品 已经存满 数组了,则扩容
            if (typeCount >= list.Length)
            {
                //1.1 创建一个为原来数组2倍容量的新数组
                Product[] temp = new Product[list.Length * 2];
                //1.2 将原来 数组内容 复制到 新数组中
                list.CopyTo(temp, 0);
                //1.3 将新数组 赋值给 原来的数组变量
                list = temp;
            }
            //2.尝试从仓库中 获取 同类型产品
            Product existPro = GetAProduct(pro.type);
            //2.1如果 商品在仓库中存在,则直接 在数量上 增加
            if (existPro != null)
            {
                existPro.count += pro.count;
            }
            else//2.2如果 商品在仓库中不存在,则直接 追缴到数组中
            {
                //2.将新增的商品加入到数组中
                list[typeCount] = pro;
                //3.商品类型的 数量自增
                typeCount++;
            }
        } 
        #endregion

        #region 1.1 添加商品 +void AddProduct(string type, int count, decimal price)
        /// <summary>
        /// 1.1 添加商品
        /// </summary>
        /// <param name="type">商品类型名称</param>
        /// <param name="price">价格</param>
        /// <param name="count">商品数量</param>
        public void AddProduct(string type, decimal price, int count)
        {
            Product p = new Product(type, price, count);
            AddProduct(p);
        } 
        #endregion

        #region 2.0 按照 商品类型名称 减去一个商品 +Product RemoveAProduct(string strProType)
        /// <summary>
        /// 2.0 按照 商品类型名称 减去一个商品
        /// </summary>
        /// <param name="strProType">商品类型名称</param>
        /// <returns>如果有库存,则返回true;如果没有,则返回false</returns>
        public bool RemoveAProduct(string strProType)
        {
            //1.0 循环 商品数组,但注意,按照商品数量来循环(因为数组中后面的空间可能是空的)
            for (int i = 0; i < typeCount; i++)
            { 
                //a.取出商品对象
                Product proTemp = list[i];
                //b.判断商品类型名称 是否 和 要获取的相同
                if (proTemp.type == strProType)
                {
                    //c.判断找到的商品 的数量 是否有剩余
                    if (proTemp.count > 0)
                    {
                        //c1.如果有,则自减,并返回true
                        proTemp.count--;
                        return true;
                    }
                    else //c2.如果没有,则直接返回false
                    {
                        //c3.移除 产品数组 里的 count = 0 的 产品

                        return false;
                    }
                }
            }
            return false;
        } 
        #endregion

        #region 3.0 返回 包含 所有商品的数组 + Product[] GetAllProducts()
        /// <summary>
        /// 3.0 返回 包含 所有商品的数组
        /// </summary>
        /// <returns></returns>
        public Product[] GetAllProducts()
        {
            Product[] temp=new Product[typeCount];
            //list.CopyTo(temp, 0);
            //Buffer.BlockCopy(list, 0, temp, 0, typeCount);
            for (int i = 0; i < typeCount; i++)
            {
                temp[i] = list[i];
            }

            return temp;
        } 
        #endregion

        #region 3.1 返回 要查找的 商品 - Product GetAProduct(string strProType)
        /// <summary>
        /// 3.1 返回 要查找的 商品
        /// </summary>
        /// <param name="strProType"></param>
        /// <returns></returns>
        private Product GetAProduct(string strProType)
        {
            //1.0 循环 商品数组,但注意,按照商品数量来循环(因为数组中后面的空间可能是空的)
            for (int i = 0; i < typeCount; i++)
            {
                //a.取出商品对象
                Product proTemp = list[i];
                //b.判断商品类型名称 是否 和 要获取的相同
                if (proTemp.type == strProType)
                {
                    //c.判断找到的商品 的数量 是否有剩余
                    if (proTemp.count > 0)
                    {
                        //c1.如果有剩余,则 返回此商品对象
                        return proTemp;
                    }
                }
            }
            return null;
        } 
        #endregion

        #region 4. 根据 id 查询 产品对象 +Product GetProductById(int proId)
        /// <summary>
        /// 4. 根据 id 查询 产品对象
        /// </summary>
        /// <param name="proId">产品id</param>
        /// <returns>如果找到了,则返回产品对象;如果没找到,返回null</returns>
        public Product GetProductById(int proId)
        {
            //1.0 循环 商品数组,但注意,按照商品数量来循环(因为数组中后面的空间可能是空的)
            for (int i = 0; i < typeCount; i++)
            {
                //a.取出商品对象
                Product proTemp = list[i];
                //b.判断商品id 是否 和 要获取的相同
                if (proTemp.id == proId)
                {
                    //c.判断找到的商品 的数量 是否有剩余
                    if (proTemp.count > 0)
                    {
                        //c1.如果有剩余,则 返回此商品对象
                        return proTemp;
                    }
                }
            }
            return null;
        } 
        #endregion
    }
}

  

售货员Saler

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

namespace P02ShopV1
{
    /// <summary>
    /// 售货员
    /// </summary>
    public class Saler
    {
        #region 全局变量 name,sex,age
        /// <summary>
        /// 名字
        /// </summary>
        private string name;
        /// <summary>
        /// 性别
        /// </summary>
        private bool sex;
        /// <summary>
        /// 年龄
        /// </summary>
        private int age; 
        #endregion

        public Saler(string name, bool sex, int age)
        {
            this.name = name;
            this.age = age;
            this.sex = sex;
        }

        #region 1.0 欢迎 +void Welcome()
        /// <summary>
        /// 1.0 欢迎
        /// </summary>
        public void Welcome()
        {
            Console.WriteLine("您好~我是 售货员【{0}】,年龄{1},性别{2}", name, age, sex ? "男" : "女");
        } 
        #endregion

        #region 2.0 销售货物 +void Selling()
        /// <summary>
        /// 2.0 销售货物
        /// </summary>
        public void Selling(ProductList list)
        {
            do
            {
                //1.显示 商品列表
                ShowProduct(list);
                //2.接收用户输入,并显示价格
                GetUserInput(list);
                //3.询问是否要继续卖商品
                Console.WriteLine("您是否要继续选购商品呢?(y/n)");
            } while (Console.ReadLine() == "y");
        } 
        #endregion

        #region 2.1 显示商品列表 +void ShowProduct(ProductList list)
        /// <summary>
        /// 2.1 显示商品列表
        /// </summary>
        /// <param name="list"></param>
        public void ShowProduct(ProductList list)
        {
            //1.获取 仓库 里的 商品列表数据
            Product[] listPro = list.GetAllProducts();
            //2.循环 商品列表
            for (int i = 0; i < listPro.Length;i++ )
            {
                Product pro = listPro[i];
                Console.WriteLine("编号【{0}】:【{1}】 单价:{2} 剩余数量{3}", pro.id, pro.type, pro.price, pro.count);
            }
        } 
        #endregion

        #region 2.2  接收用户输入 -void GetUserInput(ProductList list)
        /// <summary>
        /// 2.2  接收用户输入
        /// </summary>
        private void GetUserInput(ProductList list)
        {
            //1.接收用户 要购买的类型编号
            int id = -1;
            Product pro = null;
            while (true)
            {
                id = Helper.GetAUserNum("请问你要购买什么产品呢(输入产品编号):");
                //1.1判断用户输入的产品编号 是否存在
                pro = list.GetProductById(id);
                if (pro == null)
                {
                    Console.WriteLine("您的产品编号输入有误,请重新输入!");
                }
                else
                {
                    break;
                }
            }

            //2.接收用户的 数量
            int num = -1;
            while (true)
            {
                num = Helper.GetAUserNum("请输入你要购买的数量:");
                //2.1判断用户 要购买的 数量是否足够
                if (pro.count < num)
                {
                    Console.WriteLine("对不起,你要购买的商品数量不够哦~~~,请重新输入数量吧!");
                }
                else
                {
                    break;
                }
            }

            //3.移除商品列表中 指定数量和id的产品
            for (int i = 0; i < num; i++)
            {
                list.RemoveAProduct(pro.type);
            }
            //4.显示购买成功消息
            Console.WriteLine("亲爱的客户,您成功购买了{0}件【{1}】商品,总价格为:{2},谢谢光临~~", num, pro.type, num * pro.price);
        } 
        #endregion
    }
}

  

 商店类Shop

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

namespace P02ShopV1
{
    /// <summary>
    /// 商店类
    /// </summary>
    public class Shop
    {
        #region 全局变量 proList,saler,shopName
        /// <summary>
        /// 商店仓库
        /// </summary>
        private ProductList proList;

        /// <summary>
        /// 售货员
        /// </summary>
        Saler saler;

        /// <summary>
        /// 商店名字
        /// </summary>
        string shopName="连锁商店";
        #endregion

        #region 0.1 构造函数,初始化商店数据 + Shop(Saler saler)
        /// <summary>
        /// 构造函数,初始化商店数据
        /// </summary>
        /// <param name="saler">售货员对象</param>
        public Shop(Saler saler)
        {
            this.saler = saler;//将传入的 售货员对象 设置给 当前商店对象的 saler
            InitProduct();//初始化商店商品数据
        }
        #endregion

        #region 0.2 构造函数,初始化商店数据 +Shop(Saler saler, string shopName)
        /// <summary>
        /// 构造函数,初始化商店数据
        /// </summary>
        /// <param name="saler">售货员对象</param>
        /// <param name="shopName">商店名字</param>
        public Shop(Saler saler, string shopName)
            : this(saler)
        {
            this.shopName = shopName;
        } 
        #endregion

        #region 0.0 初始化商店商品数据 -void InitProduct()
        /// <summary>
        /// 初始化商店商品数据
        /// </summary>
        private void InitProduct()
        {
            //实例化 商店的商品列表(仓库)
            proList = new ProductList();
            //向仓库中 添加 数据
            proList.AddProduct("玩具狗狗", 20, 10);
            proList.AddProduct("玩具猫猫", 40, 10);
            proList.AddProduct("玩具袅袅", 120, 10);
            proList.AddProduct("玩具老虎", 200, 10);
        } 
        #endregion

        #region 1.0 开始营业 +void Start()
        /// <summary>
        /// 开始营业
        /// </summary>
        public void Start()
        {
            Console.WriteLine("欢迎光临{0}", this.shopName);
            //1.售货员打招呼
            saler.Welcome();
            //2.售货员开始售货
            saler.Selling(proList);
        } 
        #endregion

    }
}

  

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

namespace P02ShopV1
{
    class Program
    {
        static void Main(string[] args)
        {
            Saler saler = new Saler("小白", false, 18);
            Shop shop = new Shop(saler, "小白连锁超市");
            shop.Start();
            Console.ReadLine();
        }
    }
}

  

原文地址:https://www.cnblogs.com/fenglingyi/p/4230888.html