接口实现方式(c#)

如果类实现两个接口,并且这两个接口包含具有相同签名的成员,那么在类中实现该成员将导致两个接口都使用该成员作为它们的实现。例如:

//接口
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{

    interface IControl
    {
        void Paint();
    }
    interface ISurface
    {
        void Paint();
    }

}


//接口实现类(隐式实现)
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Class2:IControl,ISurface
    {

        #region ISurface 成员

        public void Paint()
        {
            Console.WriteLine("Hello,World!");
        }

        #endregion
    }
}

//效果测试类
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Class2 c2 = new Class2();
            c2.Paint();

            Console.ReadLine();
        }
    }
}

然而,如果两个接口成员执行不同的函数,那么这可能会导致其中一个接口的实现不正确或两个接口的实现都不正确。可以显式地实现接口成员 -- 即创建一个仅通过该接口调用并且特定于该接口的类成员。这是使用接口名称和一个句点命名该类成员来实现的。例如:

//接口
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    interface ITest
    {
        bool GetResult(string userName);

        int P { get;}
    }

    interface ITest1
    {
        int P(int a,int b);

        bool GetResult(int age);
    }

}

//接口实现类(显示实现)
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Class1:ITest,ITest1
    {
        private int _a;

        public Class1(int a)
        {
            this._a = a;
        }

        #region ITest 成员

        bool ITest.GetResult(string userName)
        {
            bool check = false;
            if (userName == "xiaoming")
            {
                check = true;
            }
            return check;
        }

        public int P
        {
            get { return _a; }
        }

        #endregion

        #region ITest1 成员

        int ITest1.P(int a,int b)
        {
            return a + b;
        }

        bool ITest1.GetResult(int age)
        {
            bool check = false;
            if (age >= 18)
            {
                check = true;
            }
            return check;
        }

        #endregion
    }
}


//测试效果类
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Class1 c = new Class1(100);
            int num = c.P;
            ITest t1 = (ITest)c;
            bool check = t1.GetResult("zhangsan");

            ITest1 t = (ITest1)c;
            int sum = t.P(4, 5);
            bool check1 = t.GetResult(22);

            Console.WriteLine("ITest:");
            Console.WriteLine("FunctioName:GetResult-->" + check.ToString());
            Console.WriteLine("AttributeName:P-->" + num.ToString());
            Console.WriteLine("");
            Console.WriteLine("ITest1:");
            Console.WriteLine("FunctionName:P-->" + sum.ToString());
            Console.WriteLine("FunctionName:GetResult-->" + check1.ToString());

            Console.ReadLine();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个使用C#实现MySQL语句的示例代码: ```csharp using System; using MySql.Data.MySqlClient; public interface IDatabaseService { bool Connect(string connectionString); bool Disconnect(); bool ExecuteNonQuery(string query); MySqlDataReader ExecuteReader(string query); } public class MySqlDatabaseService : IDatabaseService { private MySqlConnection connection; public bool Connect(string connectionString) { connection = new MySqlConnection(connectionString); try { connection.Open(); return true; } catch (MySqlException ex) { Console.WriteLine("Error connecting to MySQL: " + ex.Message); return false; } } public bool Disconnect() { try { connection.Close(); return true; } catch (MySqlException ex) { Console.WriteLine("Error disconnecting from MySQL: " + ex.Message); return false; } } public bool ExecuteNonQuery(string query) { MySqlCommand command = new MySqlCommand(query, connection); try { command.ExecuteNonQuery(); return true; } catch (MySqlException ex) { Console.WriteLine("Error executing non-query: " + ex.Message); return false; } } public MySqlDataReader ExecuteReader(string query) { MySqlCommand command = new MySqlCommand(query, connection); try { return command.ExecuteReader(); } catch (MySqlException ex) { Console.WriteLine("Error executing reader: " + ex.Message); return null; } } } public class Program { public static void Main() { string connectionString = "your_connection_string_here"; IDatabaseService databaseService = new MySqlDatabaseService(); if(databaseService.Connect(connectionString)) { // 执行查询语句 string query = "SELECT * FROM table_name"; MySqlDataReader reader = databaseService.ExecuteReader(query); if(reader != null) { while(reader.Read()) { // 处理查询结果 string column1 = reader.GetString(0); int column2 = reader.GetInt32(1); Console.WriteLine("Column1: " + column1 + ", Column2: " + column2); } reader.Close(); } // 执行非查询语句 string insertQuery = "INSERT INTO table_name (column1, column2) VALUES ('value1', 2)"; bool success = databaseService.ExecuteNonQuery(insertQuery); if(success) { Console.WriteLine("Insert query executed successfully."); } databaseService.Disconnect(); } Console.ReadLine(); } } ``` 在上述代码中,我们定义了一个`IDatabaseService`接口,其中包含连接数据库、断开连接、执行非查询语句和执行查询语句的方法。然后,我们实现了一个`MySqlDatabaseService`类,该类通过`MySqlConnection`对象连接到MySQL数据库并实现了`IDatabaseService`接口方法。 在`Main`方法中,我们创建了一个`MySqlDatabaseService`对象,并调用了`Connect`方法来连接到数据库。如果连接成功,我们可以执行查询语句和非查询语句。在执行查询语句时,我们使用`ExecuteReader`方法返回一个`MySqlDataReader`对象,并通过循环读取查询结果。在执行非查询语句时,我们使用`ExecuteNonQuery`方法执行插入语句,并检查返回值来判断是否执行成功。最后,我们调用`Disconnect`方法断开与数据库的连接。 请注意,你需要将`your_connection_string_here`替换为你自己的MySQL连接字符串。这个连接字符串应包含正确的数据库服务器地址、用户名、密码和数据库名称。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值