Interfețe
Interfețe
1. Sintacsa interfeții.
2. Moștenirea multiplă în Java
3. Extinderea interfeții la moștenire.
4. Constantele în interfețe.
5. Inițializarea cîmpurilor în interfețe.
6. Interfețe incluse.
Sintacsa interfeții
interface Instrument {
//
La compilare devine automat static & final
int i = 5;
void play();
// automat este public
String what();
void adjust();}
class Wind implements Instrument {
public void play() { System.out.println("Wind.play()"); }
public String what() { return "Wind"; }
public void adjust() {}}
class Percussion implements Instrument {
public void play() { System.out.println("Percussion.play()"); }
public String what() { return "Percussion"; }
public void adjust() {}}
class Stringed implements Instrument {
public void play() { System.out.println("Stringed.play()"); }
public String what() { return "Stringed"; }
public void adjust() {}}
class Brass extends Wind {
public void play() { System.out.println("Brass.play()"); }
public void adjust() { System.out.println("Brass.adjust()");
}}
continuare
class Woodwind extends Wind {
public void play() { System.out.println("Woodwind.play()");}
public String what() { return "Woodwind"; }}
public class Music5 {
static void tune(Instrument i) { // ...
i.play(); }
static void tuneAll(Instrument[] e) {
for(int i = 0; i < e.length; i++)
tune(e[i]); }
public static void main(String[] args)
{ Instrument[] orchestra = new Instrument[5];
int i = 0;
// Aducerea la tipul de bază la complectarea tabloului:
orchestra[i++] = new Wind();
orchestra[i++] = new Percussion();
orchestra[i++] = new Stringed();
orchestra[i++] = new Brass();
orchestra[i++] = new Woodwind();
tuneAll(orchestra);
}}
Moștenirea multiplă în Java
interface CanFight {
void fight();}
interface CanSwim {
void swim();}
interface CanFly {
void fly();}
class ActionCharacter {
public void fight() {}}
class Hero extends ActionCharacter implements CanFight, CanSwim, CanFly {
public void swim() {}
public void fly() {}}
public class Adventure {
static void t(CanFight x) { x.fight(); }
static void u(CanSwim x) { x.swim(); }
static void v(CanFly x) { x.fly(); }
static void w(ActionCharacter x) { x.fight(); }
public static void main(String[] args) {
Hero h = new Hero();
t(h); // Treat - CanFight
u(h); // Treat - CanSwim
v(h); // Treat - CanFly
w(h); // Treat - ActionCharacter }}
Extinderea interfeții la moștenire
interface Monster {
void menace();}
interface DangerousMonster extends Monster { void destroy();}
interface Lethal {
void kill();}
class DragonZilla implements DangerousMonster {
public void menace() {}
public void destroy() {}}
interface Vampire extends DangerousMonster, Lethal {
void drinkBlood();}
class HorrorShow {
static void u(Monster b) { b.menace(); }
static void v(DangerousMonster d) { d.menace();
d.destroy(); }
public static void main(String[] args) {
DragonZilla if2 = new DragonZilla();
u(if2);
v(if2); }}
Gruparea constantelor
package c08;
public interface Months {
int JANUARY = 1,
FEBRUARY = 2,
MARCH = 3,
APRIL = 4,
MAY = 5,
JUNE = 6,
JULY = 7,
AUGUST = 8,
SEPTEMBER = 9,
OCTOBER = 10,
NOVEMBER = 11,
DECEMBER = 12;
}
Utilizarea constantelor din interfață
package c08;
public final class Month2 {
private String name;
private Month2(String nm) { name = nm; }
public String toString() { return name; }
public final static Month2
JAN = new Month2("January"), FEB = new Month2("February"),
MAR = new Month2("March"), APR = new Month2("April"),
MAY = new Month2("May"), JUN = new Month2("June"),
JUL = new Month2("July"), AUG = new Month2("August"),
SEP = new Month2("September"),OCT = new Month2("October"),
NOV = new Month2("November"), DEC = new Month2("December");
public final static Month2[] month = {JAN, JAN, FEB, MAR, APR, MAY, JUN,
JUL, AUG, SEP, OCT, NOV, DEC };
public static void main(String[] args) {
Month2 m = Month2.JAN;
System.out.println(m);
m = Month2.month[12];
System.out.println(m);
System.out.println(m == Month2.DEC);
System.out.println(m.equals(Month2.DEC)); }}
Rezultatul realizării:
run:
January
December
true
true
Inițializarea în interfață a constantelor
import java.util.*;
public interface RandVals {
int rint = (int)(Math.random() * 10);
long rlong = (long)(Math.random() * 10);
float rfloat = (float)(Math.random() * 10);
double rdouble = Math.random() * 10;}
public class TestRandVals {
public static void main(String[] args)
{ System.out.println(RandVals.rint);
System.out.println(RandVals.rlong);
System.out.println(RandVals.rfloat);
System.out.println(RandVals.rdouble);
}}
Rezultatul tralizării:
run:
1
2
6.787242
4.409594932086137
Interfețe incluse
class A {
interface B { void f(); }
public class BImp implements B {
public void f() {} }
private class BImp2 implements B {
public void f() {} }
public interface C { void f(); }
class CImp implements C {
public void f() {} }
private class CImp2 implements C {
public void f() {} }
private interface D { void f(); }
private class DImp implements D {
public void f() {} }
public class DImp2 implements D {
public void f() {} }
public D getD() { return new DImp2(); }
private D dRef;
public void receiveD(D d) { dRef = d; dRef.f(); }}
interface E { interface G { void f(); }
public interface H { void f(); }
void g(); // Nu poate fi private în interfață:
//!private interface I {}}
continuarе
public class NestingInterfaces {
public class BImp implements A.B {public void f() {} } class CImp implements A.C
{ public void f() {} }
// nu poate fi realizată private interface fără
// declararea internă a clasei:
//! class DImp implements A.D {
//! public void f() {} }
class EImp implements E {
public void g() {} }
class EGImp implements E.G {
public void f() {} }
class EImp2 implements E {
public void g() {}
class EG implements E.G {
public void f() {} } }
public static void main(String[] args) {
A a = new A();
// Нет доступа A.D:
//! A.D ad = a.getD();
// Ничего не возвращается, кроме A.D:
//! A.DImp2 di2 = a.getD();
// Nu primim acces la membrul intrfeții:
//! a.getD().f();
// Numai alt A poate lucra cu getD():
A a2 = new A();
a2.receiveD(a.getD()); }}