C # Advanced (vi) interface, interface inheritance

Source: Internet
Author: User
Tags bool inheritance mysql readline mysql database

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.