課程
/后端開發(fā)
/C#
/C#面向?qū)ο缶幊?/a>
dog轉(zhuǎn)換成cat之后,cat沒有繼承dog的名字(構(gòu)造方法),名字變成了空
2018-11-04
源自:C#面向?qū)ο缶幊?nbsp; 4-2
正在回答
public static“?explicit” operator Cat(Dog dog)
改成隱士轉(zhuǎn)換
烏薪王 提問者
輸出:我叫hommy
????????? wow
????????? 我叫
????????? meow
using System;
namespace 裝箱與拆箱{?? abstract public class pet??? {??????? public string name;? ? ? ????????? public pet(string name)??????? {??????????? Console.WriteLine("我叫" + name);??????? }??????? abstract public void speak();??????? ??? }??? public class Dog : pet??? {??????? public Dog(string name):base(name)??????? {?????????? ??????? }??????? ??????? public override void speak()??????? {??????????? Console.WriteLine("wow");??????? }??????? public static explicit operator Cat(Dog dog)??????? {??????????? return new Cat(dog.name);??????? }??? }??? public class Cat : pet??? {??????? public Cat(string name) : base(name)??????? {
??????? }??????? public override void speak()??????? {??????????? Console.WriteLine("meow");??????? }??????? ??? }??? ??? class Program??? {??????? static void Main(string[] args)??????? {? ? ? ? ? ? Dog dog = new Dog("hommy");??????????? dog.speak();??????????? Cat cat = (Cat)dog;??????????? cat.speak();? ? ? ? ? ????????? }??? }}
舉報
本系列教程主要是,帶你學(xué)習(xí)C#面向?qū)ο缶幊痰木幊趟枷?、編程技?/p> 進入課程
2 回答為class Dog和class Cat的命名的構(gòu)造方法,為什么不放到父類的Pat中去實現(xiàn)呢?
3 回答構(gòu)造函數(shù)的數(shù)量
5 回答構(gòu)造函數(shù)的使用
1 回答為什么以前的方法好多不能調(diào)用了
4 回答隱式調(diào)用基類的構(gòu)造函數(shù)
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2018-11-04
public static“?explicit” operator Cat(Dog dog)
2018-11-04
改成隱士轉(zhuǎn)換
2018-11-04
輸出:我叫hommy
????????? wow
????????? 我叫
????????? meow
2018-11-04
using System;
namespace 裝箱與拆箱
{?? abstract public class pet
??? {
??????? public string name;
? ? ? ??
??????? public pet(string name)
??????? {
??????????? Console.WriteLine("我叫" + name);
??????? }
??????? abstract public void speak();
???????
??? }
??? public class Dog : pet
??? {
??????? public Dog(string name):base(name)
??????? {
??????????
??????? }
???????
??????? public override void speak()
??????? {
??????????? Console.WriteLine("wow");
??????? }
??????? public static explicit operator Cat(Dog dog)
??????? {
??????????? return new Cat(dog.name);
??????? }
??? }
??? public class Cat : pet
??? {
??????? public Cat(string name) : base(name)
??????? {
??????? }
??????? public override void speak()
??????? {
??????????? Console.WriteLine("meow");
??????? }
???????
??? }
???
??? class Program
??? {
??????? static void Main(string[] args)
??????? {
? ? ? ? ? ? Dog dog = new Dog("hommy");
??????????? dog.speak();
??????????? Cat cat = (Cat)dog;
??????????? cat.speak();
? ? ? ? ? ??
??????? }
??? }
}