Upload
tamas-lakatos
View
3
Download
0
Embed Size (px)
DESCRIPTION
Java Kurzus - 2013 Babes-Bolyai Tudomanyegyetem5. eloadas - InterfeszDarvay ZsoltPar szo arrol hogy mi is az interfesz, hogyan lehet hasznalni, s miert is jo mindez.An interface in the Java programming language is an abstract type that is used to specify an interface (in the generic sense of the term) that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations (variable declarations that are declared to be both static and final). All methods of an Interface do not contain implementation (method bodies) as of all versions below Java 8. Starting with Java 8, default and static methods may have implementation in the interface definition.Interfaces cannot be instantiated, but rather are implemented. A class that implements an interface must implement all of the methods described in the interface, or be an abstract class. Object references in Java may be specified to be of an interface type; in each case, they must either be null, or be bound to an object that implements the interface.One benefit of using interfaces is that they simulate multiple inheritance. All classes in Java must have exactly one base class, the only exception being java.lang.Object (the root class of the Java type system); multiple inheritance of classes is not allowed.A Java class may implement, and an interface may extend, any number of interfaces; however an interface may not implement an interface.
Citation preview
Interfsz a Java nyelvben
Darvay Zsolt
Java 2
Mi is az interfsz (fellet)?
Az interfsz az osztlyhoz hasonlan egy tpust hatroz meg.
Abban klnbzik az osztlytl, hogy az sszes metdusa absztrakt, s az sszes mezje konstans.
Az interfsz nem tartalmaz megvalstst, de az osztlyok implementlhatjk a fellet metdusait.
Deklarci
InterfszMdostopc interface Azonost Tpusparamterekopc sopc InterfszTrzs
InterfszMdost: public (nyilvnos interfsz)
s: extends sinterfszek listja
Java 4
Plda
interface Jarmu {
void indul();
void megall();
void megy(int km);
void all(int perc);
};
Java 5
A Bicikli osztly
class Bicikli implements Jarmu {
public void indul()
{
System.out.println("Indul a bicikli.");
}
public void megall()
{
System.out.println("Megall a bicikli.");
}
Java 6
A Bicikli osztly
public void megy(int km) {
System.out.println("Biciklizik " + km +
" kilometert.");
}
public void all(int perc) {
System.out.println("A bicikli all " + perc +
" percet.");
}
}
Java 7
Az Auto osztly
class Auto implements Jarmu {
public void indul()
{
System.out.println("Indul az auto.");
}
public void megall()
{
System.out.println("Megall az auto.");
}
Java 8
Az Auto osztly
public void megy(int km) {
System.out.println("Az auto megy " + km + " kilometert.");
}
public void all(int perc) {
System.out.println("Az auto all " + perc +
" percet.");
}
}
Java 9
A bejar_ut metdus
class Utazas {
public static void bejar(Jarmu j)
{
j.indul();
j.megy(3);
j.all(1);
j.megy(2);
j.megall();
}
Java 10
A main metdus
public static void main(String[] args)
{
Jarmu b = new Bicikli();
bejar( b );
Jarmu a = new Auto();
bejar( a );
}
}
Java 11
Kimenet
Indul a bicikli.
Biciklizik 3 kilometert.
A bicikli all 1 percet.
Biciklizik 2 kilometert.
Megall a bicikli.
Indul az auto.
Az auto megy 3 kilometert.
Az auto all 1 percet.
Az auto megy 2 kilometert.
Megall az auto.
Java 12
Konstansok hasznlata
Az interfsz trzsben megadott vltozk public static final jellegek (a mdostkat nem kell megadni).
Ha egy osztly tbb interfszt valst meg, s azokban vannak azonos nev konstansok, akkor minstett neveket kell hasznlni (az interfsz nevt s a . opertort helyezzk el a meznv el).
Ha egy interfsz tbb gon rkli ugyanazt a mezt, akkor nem kell minstett nevet megadni.
Java 13
Plda: konstansok s szrmaztats
interface Elso {
int x = 10;
}
interface Masodik {
int x = 20;
}
Java 14
Szrmaztatott felletek
interface Alap {
int x = 50;
}
interface S1 extends Alap {
}
interface S2 extends Alap {
}
interface S1S2 extends S1, S2 {
}
Java 15
Az ElsoMasodik osztly
class ElsoMasodik implements Elso, Masodik {
public void kiir()
{
//System.out.println(x); // hiba
System.out.println(Elso.x);
System.out.println(Masodik.x);
}
}
Java 16
Az S1S2Oszt osztly
class S1S2Oszt implements S1S2 {
public void kiir()
{
System.out.println(x);
}
}
Java 17
A main metdus
public class InterfeszS {
public static void main(String[] args)
{
ElsoMasodik u = new ElsoMasodik();
u.kiir();
S1S2Oszt v = new S1S2Oszt();
v.kiir();
}
}
Java 18
Kimenet
10
20
50
Java 19
Interfsz s Object
Az interfsznek az Object nem se, de ennek ellenre nem adhatunk meg olyan metdust, amelynek a szignatrja (nv + formlis paramterek listja) azonos az Object osztly egyik metdusval, de a visszatrsi rtk klnbz.