Iltimos, ko'p trol qilmang, men endi maqolalarni tarjima qilishni boshlayapman
Interfeys boshqa interfeysdan meros bo'lishi mumkin, agar interfeys funktsional interfeysdan meros bo'lsa va yangi mavhum usullarni o'z ichiga olmasa, u holda bu interfeys ham funktsional hisoblanadi. Ammo interfeys faqat bitta mavhum usul va ko'plab standart usullarni o'z ichiga olishi mumkin va u hali ham funktsional hisoblanadi.
Bu Java 8-dagi funktsional interfeyslar tushunchasi va ularni qanday amalga oshirish mumkinligi haqida qisqacha ma'lumot edi. lambda ifodalaridan foydalanish.
Funktsional interfeyslarga kirish - Java 8 da qayta yaratilgan tushunchalar
Dunyodagi har bir Java dasturchisi kamida bir marta quyidagi interfeyslardan birini ishlatgan:java.lang.Runnable
, java.awt.event.ActionListener
, java.util.Comparator
, java.util.concurrent.Callable
. Ularning barchasida bitta umumiy narsa bor va bu ularning barchasida faqat bitta usul bor. JDK-da Java dasturchilari tomonidan yaratilgan boshqa ko'plab bunday interfeyslar mavjud. Single Abstract Method interfaces
Ushbu interfeyslar (SAM interfeyslari) deb ham ataladi . Ushbu interfeyslardan foydalanishning eng mashhur usullaridan biri quyidagi misolda bo'lgani kabi ushbu interfeyslardan foydalanadigan anonim ichki sinflarni yaratishdir:
public class AnonymousInnerClassTest {
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("A thread created and running...");
}
}).start();
}
}
Java 8 da SAM kontseptsiyasi qayta yaratiladi va funksional interfeyslar deb ataladi. Ular lambda ifodalari, usul havolalari va mos yozuvlar konstruktorlari yordamida ifodalanishi mumkin. Yangi @FunctionalInterface izohi yaratildi, u siz izoh bergan interfeys funksional darajada ishlamasa, kompilyator darajasida xatoliklarni chiqarish uchun ishlatiladi. Keling, bitta mavhum usul bilan oddiy funktsional interfeysni ko'rib chiqaylik:
@FunctionalInterface
public interface SimpleFunInterface {
public void doWork();
}
Interfeys sinfdan mavhum usullarni ham e'lon qilishi mumkin java.lang.Object
, ammo bu holda interfeys funktsional deb e'lon qilinishi ham mumkin:
@FunctionalInterface
public interface SimpleFuncInterface {
public void doWork();
public String toString();
public Boolean equals(Object o);
}
Interfeysga boshqa mavhum usulni qo'shganingizdan so'ng, IDE uni rasmdagi kabi xato deb belgilaydi: ![Funktsional interfeyslarga kirish - 2](https://cdn.javarush.com/images/article/0aa77e6f-dc93-4309-99fd-b86fbcdb220f/1024.jpeg)
Yana nimani o'qish kerak: |
---|
@FunctionalInterface
public interface ComplexFunctionalInterface extends SimpleFuncInterface {
default public void doSomeWork() {
System.out.println("Doing some work in interface impl...");
}
default public void doSomeWork() {
System.out.println("Doing some other work in interface impl...");
}
}
Yuqori misol hali ham funktsional interfeysdir. Endi funktsional interfeyslarni amalga oshirish uchun anonim ichki sinfni almashtirish uchun lambda ifodalaridan qanday foydalanishimiz mumkinligini ko'rib chiqamiz:
/*
*Implementation the interface by creating an
*anonymoous inner class versus using
*lambda expression.
*/
public class SimpleFunInterfaceTest {
public static void main(String[] args) {
carryOutWork(new SimpleFunInterface() {
@Override
public void doWork() {
System.out.println("Do work in SimpleFun impl...");
}
});
carryOutWork(() -> System.out.println("Do work in lambda exp impl..."));
}
public static void carryOutWork(SimpleFuncInterface sfi) {
sfi.work();
}
}
Dasturning natijasi quyidagicha bo'ladi:
Do work in SimpleFun impl...
Do work in lambda exp impl...
Agar siz Java lambda ifodasi sintaksisini qo'llab-quvvatlaydigan IDE dan foydalanayotgan bo'lsangiz (Netbeans 8 Nightly Builds) - Anonim ichki sinflardan foydalanishda sizga maslahat beriladi: ![Funktsional interfeyslarga kirish - 3](https://cdn.javarush.com/images/article/b188b9a5-c454-44e7-afcb-6e74177d8b80/1024.jpeg)
GO TO FULL VERSION