課程
/后端開(kāi)發(fā)
/C#
/用C#實(shí)現(xiàn)封裝
C#學(xué)習(xí)中不能順利實(shí)現(xiàn)封裝,問(wèn)題如下圖,求大神指點(diǎn)!
2016-05-11
源自:用C#實(shí)現(xiàn)封裝 2-4
正在回答
代碼全部復(fù)制出來(lái),不要截圖
慕設(shè)計(jì)7170223 提問(wèn)者
問(wèn)題就在于你的每次封裝?public 語(yǔ)句后面加了分號(hào),導(dǎo)致語(yǔ)法錯(cuò)誤
?類(lèi)函數(shù)
?class Child
? ?{
? ? ? ? ? //訪問(wèn)修飾符public公共的 private私有的 ?就是做封裝
? ? ? private string name;//隱藏字段?
? ? ? ?public string Name;//通過(guò)封裝實(shí)現(xiàn)字段屬性
? ? {
? ? ? ? get{return name;}
? ? ? ? set{name=value }
? ? }
? ??
? ? ? ? ? ? ? //封裝的快捷鍵CTRL+R+E
? ? ? ? private string sex;
? ? ? ? public string Sex;
? ? ?{
? ? ? ? ?get {return sex;}
? ? ? ? ?set {sex= value;}
? ? ?}
? ? ? ? ?private int age;
? ? ? ? public int Age;
? ? ? ? ?get {return age;}
? ? ? ? ?set {
? ? ? ? ? ? ? ? if(value>=5&&<=7)//條件結(jié)構(gòu)
? ? ? ? ? ? ? ? ?age= value;
? ? ? ? ? ? }
? ? ? }
? ? ? ? ?private int height;?
? ? ? ? public int Height;
? ? ? ? ?get {return height;}
? ? ? ? ?set {height= value;}
? ? ? ?
? ? ? ? ? ?
? ? ? ? ?public void PlayBall()//方法聲明 void為返回值類(lèi)型
? ? ? ? ?{//方法體
? ? ? ? ? ? ?Console.WriteLine("耶!我是小小C羅");//定義方法 即對(duì)象方法
? ? ? ? ?}
調(diào)用函數(shù)
?class Program
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? Child child = new Child();//實(shí)例化了Child類(lèi)的對(duì)象
? ? ? ? ? ? //child._name = "馬小明";//為字段賦值
? ? ? ? ? ? child.Name = "馬小明";//為字段賦值
? ? ? ? ? ? child.Sex = "男";
? ? ? ? ? ? child.Age = 6;
? ? ? ? ? ? child.Height = 120;
? ? ? ? ? ? Console.WriteLine("我叫" + child.Name + ",今年" + child.Age + "歲。");
? ? ? ? ? ? child.PlayBall();//調(diào)用踢球的
代碼還沒(méi)保存,先保存.
舉報(bào)
C#視頻教程教大家,用C#語(yǔ)言實(shí)現(xiàn)面向?qū)ο蟮姆庋b
4 回答c#封裝教學(xué)第四章4-5內(nèi)容求解
2 回答c#學(xué)習(xí)
1 回答C#的封裝的理解。
2 回答是不是不能封裝方法?
3 回答關(guān)于封裝問(wèn)題
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購(gòu)課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2016-05-11
代碼全部復(fù)制出來(lái),不要截圖
2016-08-21
問(wèn)題就在于你的每次封裝?public 語(yǔ)句后面加了分號(hào),導(dǎo)致語(yǔ)法錯(cuò)誤
2016-05-11
?類(lèi)函數(shù)
?class Child
? ?{
? ? ? ? ? //訪問(wèn)修飾符public公共的 private私有的 ?就是做封裝
? ? ? private string name;//隱藏字段?
? ? ? ?public string Name;//通過(guò)封裝實(shí)現(xiàn)字段屬性
? ? {
? ? ? ? get{return name;}
? ? ? ? set{name=value }
? ? }
? ??
? ? ? ? ? ? ? //封裝的快捷鍵CTRL+R+E
? ? ? ? private string sex;
? ? ? ? public string Sex;
? ? ?{
? ? ? ? ?get {return sex;}
? ? ? ? ?set {sex= value;}
? ? ?}
? ? ? ? ?private int age;
? ? ? ? public int Age;
? ? ?{
? ? ? ? ?get {return age;}
? ? ? ? ?set {
? ? ? ? ? ? ? ? if(value>=5&&<=7)//條件結(jié)構(gòu)
? ? ? ? ? ? ? ? ?age= value;
? ? ? ? ? ? }
? ? ? }
? ? ? ? ?private int height;?
? ? ? ? public int Height;
? ? ?{
? ? ? ? ?get {return height;}
? ? ? ? ?set {height= value;}
? ? ?}
? ? ? ?
? ? ? ? ? ?
? ? ? ? ?public void PlayBall()//方法聲明 void為返回值類(lèi)型
? ? ? ? ?{//方法體
? ? ? ? ? ? ?Console.WriteLine("耶!我是小小C羅");//定義方法 即對(duì)象方法
? ? ? ? ?}
調(diào)用函數(shù)
?class Program
? ? {
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? Child child = new Child();//實(shí)例化了Child類(lèi)的對(duì)象
? ? ? ? ? ? //child._name = "馬小明";//為字段賦值
? ? ? ? ? ? child.Name = "馬小明";//為字段賦值
? ? ? ? ? ? child.Sex = "男";
? ? ? ? ? ? child.Age = 6;
? ? ? ? ? ? child.Height = 120;
? ? ? ? ? ? Console.WriteLine("我叫" + child.Name + ",今年" + child.Age + "歲。");
? ? ? ? ? ? child.PlayBall();//調(diào)用踢球的
2016-05-11
代碼還沒(méi)保存,先保存.