您的位置:首页 > 职场人生

黑马程序员--多态练习(手机工厂)

2013-12-11 13:05 281 查看
---------------------
ASP.Net+Android+IOS开发.Net培训期待与您交流! ----------------------

interface IAppable
{
void applicatiable();
}
interface Icall
{
void call();
}
abstract class Phone:Icall
{
public abstract void call();
}
abstract class Nokia:Phone
{
public string Mark = "诺基亚";
public abstract override void call();
}
class Nokia型号8987:Nokia
{
public override void call()
{
Console.Write("我是功能机,我可以打电话,我的价格很便宜哟几百块钱哟.");
}
}
class Nokia型号appable899:Nokia,IAppable
{

public override void call()
{
Console.Write("我可以打电话.我价格几千不等.");
}

public void applicatiable()
{
Console.WriteLine("我可以装软件,我很贵,我是智能机.");
}
}
class Program
{
static void Main(string[] args)
{
//Nokia手机公司,批量生产Nokia
Nokia[] ns ={
new Nokia型号8987(),
new Nokia型号appable899()
};
for (int i = 0; i < ns.Length;i++ )
{
ns[i].call();
Console.WriteLine("品牌是"+ns[i].Mark+"\n");
}
//一个生产智能机的大公司
IAppable[] IAphones ={
new Nokia型号appable899(),
new Nokia型号appable899()
};
for (int i = 0; i < IAphones.Length;i++ )
{
IAphones[i].applicatiable();
}
Console.ReadKey();
}
}


 ■接口的实现类似于继承

  ♢补充方法体

■接口是"多继承"

  ♢由于多继承,无法保证重名的问题

  ♢可以显式实现接口成员

  ♢<接口名>.方法名(){/*方法体*/}

  ♢显式实现接口成员必须由接口对象来调用

 

interface I1
{
void Func();
}
interface I2
{
void Func();
}
class Myclass:I1,I2
{
void I1.Func()//显式实现接口成员
{
Console.WriteLine("我是I1");
}

void I2.Func()//显式实现接口成员
{
Console.WriteLine("我是I2");
}
}
class Program
{
static void Main(string[] args)
{//显式实现的接口的方法,只能使用接口对象来调用
Myclass c = new Myclass();//访问不到成员Func(),无论是I1或I2的Func()
((I1)c).Func();
((I2)c).Func();
Console.ReadKey();
}
}


 ■自动实现属性和接口属性

interface IProperity
{
//接口的属性,只写一个"get;";或"set;";或都写;表示指定的属性类型
int num1
{
set;
get;
}
int num2
{
set;
}
int num3
{
get;
}
}
class Myclass1: IProperity
{
public int num1
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}

public int num2
{
set { throw new NotImplementedException(); }
}

public int num3
{
get { throw new NotImplementedException(); }
}
}
class Myclass2
{
//自动实现属性
int num//无后备字段
{
set;//不实现方法体
get;//不实现方法体
}
//自动实现属性,必须同时具备set;和get;
}
class Program
{
static void Main(string[] args)
{
}
}


 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: