建立一个C#定义宝马品牌(BMW)类,应当:
BMW有几个私有属性:型号(type)名称(Name),价格(price)
对属性进行封装
然后为构造函数进行重载,提供为1、2、3个属性赋值,一个都不赋值
定义方法show(),该方法负责输出宝马所有基本信息;
在Main中测试,要求每个构造函数的情况都要测试到
C#,怎么进行封装,没学明白
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- 一梭键盘任平生 2022-08-26 11:25关注
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; public class BMWClass { private string type; /// <summary> /// 封装type /// </summary> public string Type { get { return type; } set { type = value; } } private string name; /// <summary> /// 封装name /// </summary> public string Name { get { return name; } set { name = value; } } private string price; /// <summary> /// 封装price /// </summary> public string Price { get { return price; } set { price = value; } } /// <summary> /// 一个都不赋值 /// </summary> public BMWClass() { } /// <summary> /// 赋值一个 /// </summary> /// <param name="_type"></param> public BMWClass(string _type) { type = _type; } /// <summary> /// 赋值两个 /// </summary> /// <param name="_type"></param> /// <param name="_name"></param> public BMWClass(string _type, string _name) { type = _type; name = _name; } /// <summary> /// 赋值三个 /// </summary> /// <param name="_type"></param> /// <param name="_name"></param> /// <param name="_price"></param> public BMWClass(string _type, string _name, string _price) { type = _type; name = _name; price = _price; } /// <summary> /// 输出所有信息 /// </summary> public void show() { Console.WriteLine("type = " + type + " name = " + name + " price =" + price); } }
static void Main(string[] args) { BMWClass bMW = new BMWClass(); bMW.Type = "1"; bMW.Name = "X1"; bMW.Price = "10万"; bMW.show(); BMWClass bMW1 = new BMWClass("2"); bMW1.Name = "X2"; bMW1.Price = "20万"; bMW1.show(); BMWClass bMW2 = new BMWClass("3", "X3"); bMW2.Price = "30万"; bMW2.show(); BMWClass bMW3 = new BMWClass("4", "X4","40万"); bMW3.show(); }
如果我没理解错的话 应该是这个意思
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
- ¥15 有了解d3和topogram.js库的吗?有偿请教
- ¥100 任意维数的K均值聚类
- ¥15 stamps做sbas-insar,时序沉降图怎么画
- ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
- ¥15 关于#Java#的问题,如何解决?
- ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
- ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
- ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
- ¥500 火焰左右视图、视差(基于双目相机)