Interface we have already mentioned earlier. The names of interfaces are traditionally preceded by uppercase I.
We assume a situation where a system has many users, and we can query if a user exists and can modify the user's password. But one day our database will be upgraded from MySQL to SQL Server. In this case, let's look at one of the following examples.
Using System;
Namespace gosoa.com.cn
{
public interface Iuseroperation
{
BOOL Userexites (string username);
BOOL Updateuserpwd (string newpwd,string oldpwd);
}
public class Sqluseroperation:iuseroperation
{
Here, if uname oldpwd is found in the database through SQL. Specific inquiries, here will not say.
String uname= "Pan";
public string oldpwd= "gosoa.com.cn";
public bool Userexites (string username)
{
if (username==uname)
{
return true;
}else
{
return false;
}
}
public bool Updateuserpwd (string newpwd,string oldpwd)
{
if (NEWPWD==OLDPWD)
{
return true;
} else
{
return false;
}
}
}
public class MainClass
{
static void Main (string [] args)
{
String Newpwd =console.readline ();
String username =console.readline ();
Sqluseroperation one=new sqluseroperation ();
Iuseroperation tow=new sqluseroperation ();
if (Tow.userexites (username))
{
Console.WriteLine ("User Presence");
}else
{
Console.WriteLine ("User does not exist");
}
if (Tow.updateuserpwd (NEWPWD,ONE.OLDPWD))
{
Console.WriteLine ("Password modified successfully");
}else
{
Console.WriteLine ("Password modification failed");
}
}
}
/*
We may need to use the MySQL database one day. This time the concrete implementation is different.
public class Mysqluseroperation:iuseroperation
{
}
*/
}
Note that the class that implements the interface must implement all the members of the class. Otherwise there will be an error.
Let's take a look at this sentence iuseroperation tow=new sqluseroperation (); The clause declares the reference variable as iuseroperation, which means that they can point to an instance of any class that implements the interface.
Also, interfaces can inherit from each other, but note that the class that implements the interface must implement all the methods of the interface and the parent interface of the interface.