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();
}
}
}

  

C#基础篇八构造函数和面向对象思想的更多相关文章

  1. Python(三)基础篇之「模块&面向对象编程」

    [笔记]Python(三)基础篇之「模块&面向对象编程」 2016-12-07 ZOE    编程之魅  Python Notes: ★ 如果你是第一次阅读,推荐先浏览:[重要公告]文章更新. ...

  2. 【重走Android之路】【Java面向对象基础(三)】面向对象思想

    [重走Android之路][基础篇(三)][Java面向对象基础]面向对象思想   1 面向对象的WWH   1.1 What--什么是面向对象         首先,要理解“对象”.在Thinkin ...

  3. c# 扩展方法奇思妙用基础篇八:Distinct 扩展

    刚看了篇文章 <Linq的Distinct太不给力了>,文中给出了一个解决办法,略显复杂. 试想如果能写成下面的样子,是不是更简单优雅 var p1 = products.Distinct ...

  4. c# 扩展方法奇思妙用基础篇八:Distinct 扩展(转载)

    转载地址:http://www.cnblogs.com/ldp615/archive/2011/08/01/distinct-entension.html 刚看了篇文章 <Linq的Distin ...

  5. Lua 学习之基础篇八<Lua 元表(Metatabble)&&继承>

    讲到元表,先看一段table的合并动作. t1 = {1,2} t2 = {3,4} t3 = t1 + t2 attempt to perform arithmetic on a table val ...

  6. Python基础篇(八)

    key words:私有变量,类静态变量,生成器,导入Python模块,r查看模块可以使用的函数,查看帮助信息,启动外部程序,集合,堆,时间模块,random模块,shelve模块,文件读取等 > ...

  7. Linux基础篇八:VIM

    新知识: 普通模式光标跳转: G     ##光标跳转到末端  (shift +g) gg   ##光标跳转到开端 Ngg 15gg  ##光标跳转到当前文本中的15行 $     ##光标移动到当前 ...

  8. 基础篇八:log配置

    第一:首选查看有哪些日志文件 cd /etc/nginx/ cat nginx.conf cd /var/log/nginx/

  9. C#基础知识-面向对象思想之继承(八)

    上一篇的标题编程思想我觉得不是很符合主题,因为编程思想的范围太大了,不仅仅是封装 继承 多态,所以比较符合主题的应该是面向对象思想.这一篇中将继续叙述面向对象思想中的继承. 从字面来看继承表达的意思已 ...

随机推荐

  1. Gym-101102-K-Topological Sort

    K. Topological Sort 题面 Consider a directed graph G of N nodes and all edges (u→v) such that u < v ...

  2. 何时使用Delegate或接口

    在以下情况下使用Delegates很有用: 调用一个单一方法: 一个类要进行方法规范(method specification)的多种执行: 使用一个静态方法来执行规范: 想获得类似事件设计的模式: ...

  3. 大文件断点上传 js+php

    /* * js */ function PostFile(file, i, t) {    console.log(1);  var name = file.name, //文件名 size = fi ...

  4. 开源HIS之C/S选型

    客户端/服务的形式是我中爱的,我认可只有这样软件跑起来不会失控.因为你不知道每一个程序员是否足够清醒.但一开始我说过要从基本的应急的门诊收费开始,所以我并不打算一启动就写一个服务,并为之选型:TCP/ ...

  5. centos网络配置(手动设置,自动获取)的2种方法3

    不知道为什么最近一段时间网络特别的慢,还老是断,断的时候,局域网都连不上,当我手动设置一下ip后就可以了,搞得我很无语.下面是2种设置网络连接的方法,在说怎么设置前,一定要做好备份工作,特别是对于新手 ...

  6. java实现下载excel功能

    1,获取服务器现有excel文件 public List<Object[]> getObject(String filePath){ log.info("**文件路径为:**&q ...

  7. 第一次Java实验

      模仿JavaAppArguments.java实例,编写一个程序,此程序从命令行接受多个数字,求和之后输出. 1.设计思路:命令行参数都是字符串,必须将其转化成数字才能相加,定义一个数组接收字符串 ...

  8. utf8.php

    <?php /** * */ class Utf8 { function __construct() { global $CFG; if( preg_match('/./u', '') === ...

  9. pm2

    使用PM2将Node.js的集群变得更加容易(http://www.cnblogs.com/jaxu/p/5193643.html) nodejs pm2配置使用教程(http://blog.csdn ...

  10. bootstrap1.2

      <html>   <head>   <meta charset="UTF-8">   <title></title> ...