建立一个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(); }
如果我没理解错的话 应该是这个意思
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥20 机器学习能否像多层线性模型一样处理嵌套数据
- ¥20 西门子S7-Graph,S7-300,梯形图
- ¥50 用易语言http 访问不了网页
- ¥50 safari浏览器fetch提交数据后数据丢失问题
- ¥15 matlab不知道怎么改,求解答!!
- ¥15 永磁直线电机的电流环pi调不出来
- ¥15 用stata实现聚类的代码
- ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
- ¥20 docker里部署springboot项目,访问不到扬声器
- ¥15 netty整合springboot之后自动重连失效