接口中只能包含方法、属性、索引器、事件的声明。
接口中只能包含方法、属性、索引器、事件的声明。
接口不能实例化,不能有构造函数,不可以进行运算符重载。
接口成员不能带有访问修饰符。
使用接口时,要引用接口所在命名空间。
(forreach要实现system.collections.IEnumerable接口)
using
System;
using System.Collections.Generic;
using System.Text;
// using InterfaceTest; // 1.运行报警告信息“......没有引用命名空间",能运行。
// 2.引用后没有警告信息。
// 3.保存,关闭,再打开去掉引用,运行不报警告信息。
namespace InterfaceTest
... {
interface IBankAccount
...{
void PayIn(decimal amount);
bool Withdraw(decimal amount);
decimal Balance
...{
get;
}
}
public class SaverAccount:IBankAccount
...{
private decimal balance;
public void PayIn(decimal amount)
...{
balance = balance + amount;
}
public bool Withdraw(decimal amount)
...{
if (balance >= amount)
...{
balance=balance-amount;
return true;
}
Console.WriteLine("Withdraw attempt failed!");
return false;
}
public decimal Balance
...{
get ...{ return balance; }
}
public override string ToString()
...{
return String.Format("Venus Bank Saver:Balance={0,6:C}", balance);
}
//public SaverAccount()
//{
//}
}
class Program
...{
static void Main(string[] args)
...{
IBankAccount venusAccount = new SaverAccount(); //接口引用
venusAccount.PayIn(200);
venusAccount.Withdraw(100);
Console.WriteLine(venusAccount.ToString());
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Text;
// using InterfaceTest; // 1.运行报警告信息“......没有引用命名空间",能运行。
// 2.引用后没有警告信息。
// 3.保存,关闭,再打开去掉引用,运行不报警告信息。
namespace InterfaceTest
... {
interface IBankAccount
...{
void PayIn(decimal amount);
bool Withdraw(decimal amount);
decimal Balance
...{
get;
}
}
public class SaverAccount:IBankAccount
...{
private decimal balance;
public void PayIn(decimal amount)
...{
balance = balance + amount;
}
public bool Withdraw(decimal amount)
...{
if (balance >= amount)
...{
balance=balance-amount;
return true;
}
Console.WriteLine("Withdraw attempt failed!");
return false;
}
public decimal Balance
...{
get ...{ return balance; }
}
public override string ToString()
...{
return String.Format("Venus Bank Saver:Balance={0,6:C}", balance);
}
//public SaverAccount()
//{
//}
}
class Program
...{
static void Main(string[] args)
...{
IBankAccount venusAccount = new SaverAccount(); //接口引用
venusAccount.PayIn(200);
venusAccount.Withdraw(100);
Console.WriteLine(venusAccount.ToString());
Console.ReadLine();
}
}
}