Ukážeme si pár príkladov:
Príklad č.1 :
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Console.ReadKey(true);
}
}
interface IProjectik
{
}
Output :
Hello World!
Príklad č.2 :
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Console.ReadKey(true);
}
}
interface IProjectik
{
int x1;
}
Output :
Interfaces cannot contain fields (CS0525) - Program.cs:23,8
Komplilátor vyhlásil chybu. Interface nemôže obsahovať premenne.
Príklad č.3 :
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Console.ReadKey(true);
}
}
interface IProjectik
{
void MyName()
{
Console.WriteLine("Interface 1");
}
}
Output :
'TEST.Program.IProjectik.MyName()': interface members cannot have a definition (CS0531) - Program.cs:23,11
Komplilátor vyhlásil chybu. Metódy definované v interface nesmú obsahovať telo.
Príklad č.4 :
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Console.ReadKey(true);
}
}
interface IProjectik
{
void MyName();
}
Output :
Hello World!
Program sa nám podarilo úspešne skompilovať. A teraz si ukážeme, ako prepojíme našu triedu z interface.
Príklad č.5 :
class Program : IProjectik
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Console.ReadKey(true);
}
}
interface IProjectik
{
void MyName();
}
Output :
The type or namespace name 'IProjectik' could not be found (are you missing a using directive or an assembly reference?) (CS0246) - Program.cs:13,18
Komplilátor vyhlásil chybu. Trieda Program neobsahuje všetký metódy, ktoré sú deklarované v interface IProjectik.
Príklad č.6 :
class Program : IProjectik
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Program myProgram = new Program();
myProgram.MyName();
Console.ReadKey(true);
}
public void MyName()
{
Console.WriteLine("class Program");
}
}
interface IProjectik
{
void MyName();
}
Output :
Hello World! class Program
Program sa nám podarilo úspešne skompilovať.
Príklad č.7 :
class Program : IProjectik,IStudent
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
IProjectik myProjectik = new Program();
myProjectik.MyName();
IStudent myStudent = new Program();
myStudent.MyName();
Console.ReadKey(true);
}
public void MyName()
{
Console.WriteLine("class Program");
}
}
interface IProjectik
{
void MyName();
}
interface IStudent
{
void MyName();
}
Output :
Hello World! class Program class Program
Program sa nám podarilo úspešne skompilovať. A na záver si dáme ešte 2 príklady použita interface.
Príklad č.8 :
class Program
{
public static void Main(string[] args)
{
IProjectik myProjectik;
myProjectik = new Computer();
myProjectik.MyName();
myProjectik = new Human();
myProjectik.MyName();
Console.ReadKey(true);
}
}
class Human : IProjectik
{
public void MyName()
{
Console.WriteLine("Janko Mrkvicka");
}
}
class Computer : IProjectik
{
public void MyName()
{
Console.WriteLine("Pentium 4");
}
}
interface IProjectik
{
void MyName();
}
Output :
Janko Mrkvicka Pentium 4
Program sa nám podarilo úspešne skompilovať.
Príklad č.9 :
class Program :IHuman,IComputer
{
public static void Main(string[] args)
{
Program myProgram = new Program();
IHuman myHuman = myProgram;
myHuman.MyName();
IComputer myComputer = myProgram;
myComputer.MyName();
Console.ReadKey(true);
}
void IHuman.MyName()
{
Console.WriteLine("Janko Mrkvicka");
}
void IComputer.MyName()
{
Console.WriteLine("Pemtium 4");
}
}
interface IHuman
{
void MyName();
}
interface IComputer
{
void MyName();
}
Output :
Janko Mrkvicka Pentium 4
Program sa nám podarilo úspešne skompilovať.







