16、接口
一、接口简介
二、接口的规则
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public interface Ibook { string B_name { get; set; } void InsertToData(); } public interface IZbook:Ibook { double B_Price { get; set; } } public class book : IZbook { public string B_name { get; set; } public double B_Price { get; set; } public void InsertToData() { string info = "书名:" + B_name + "价格:" + B_Price.ToString(); MessageBox.Show(info); } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string name = textBox1.Text; double price = Convert.ToDouble(textBox2.Text); IZbook b = new book(); b.B_name = name; b.B_Price = price; b.InsertToData(); } } }
三、接口和抽象类对比
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步