sobota, 13 október 2012 08:49 Written by 7791 times
Rate this item
(3 votes)

C# - Interface

Čo je to Interface ? Interface môžeme chápať ako nejaký predpis. Umožňuje definovať správanie bez implementácie .Je to vlastne trieda, ktorá má zadeklarované názvy metód, ale tieto metódy nemajú telo. Ak nejaká trieda dedí tento interface, musí obsahovať všetký metódy, ktoré sú zadeklarované v interface, a však už musia obsahovať aj telo.

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ť.

Last modified on pondelok, 19 december 2016 21:23
Ing.Jaroslav Vadel

Som zakladateľom www.projectik.eu.

Hrám sa na programátora, ktorý ovláda:

c#,cpp,java,unity3d,php,html,NI testand,NI Vision Builder,Cognex In-Sight,NI LabView

"Naprogramovať program, ktorý funguje vie každy. Ale to, že program funguje ešte neznamena, že je napísany správne "

Website: www.projectik.eu