C#中的接口
接口定义属性,方法和事件,它们是接口的成员。接口仅包含成员的声明。定义成员是派生类的责任。它通常有助于提供派生类将遵循的标准结构。
让我们看看如何使用接口成员在C#中声明接口-
public interface ITransactions {
//成员
void showTransaction();
double getAmount();
}以下是显示如何在C#中使用Interface的示例-
示例
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
namespace Demo {
public interface ITransactions {
//成员
void showTransaction();
}
public class Transaction : ITransactions {
private string tCode;
private string date;
public Transaction() {
tCode = " ";
date = " ";
}
public Transaction(string c, string d) {
tCode = c;
date = d;
}
public void showTransaction() {
Console.WriteLine("Transaction ID: {0}", tCode);
Console.WriteLine("Date: {0}", date);
}
}
class Tester {
static void Main(string[] args) {
Transaction t1 = new Transaction("8877", "6/25/2018");
Transaction t2 = new Transaction("5656", "7/25/2018");
t1.showTransaction();
t2.showTransaction();
Console.ReadKey();
}
}
}输出结果
Transaction ID: 8877 Date: 6/25/2018 Transaction ID: 5656 Date: 7/25/2018
热门推荐
10 学生宴祝福语简短的
11 牛年女神节祝福语简短
12 教练生日祝福语 简短独特
13 修祖屋祝福语简短
14 生日牌祝福语英语简短
15 朋友孩子考试祝福语简短
16 职场离职文案祝福语简短
17 学生节祝福语简短大全
18 宝子生日祝福语简短