Özet - Interface
- Java kursu
- Orta seviye
- Ders 52/116
- 5 dk
interface ve default metodu birleştirerek bir ödeme sistemi yazarsın.
Özet: Ödeme sistemi
Bu özette bir ödeme sistemi tasarlayacaksın. PaymentSystem interface'i pay metodu sözleşmesini ve bir default metot ile makbuz yazdırma davranışını tanımlar. CreditCard sınıfı bu sözleşmeyi uygular.
Önce PaymentSystem interface'ini tanımlayacaksın: void pay(double amount) imzası ve default void receipt(double amount) gövdesi. receipt, tutarı ekrana yazdırır. Sonra CreditCard sınıfı implements PaymentSystem ile pay metodunu yazar. Bu yapı, interface ve default metodunun gerçek bir senaryoda nasıl çalıştığını gösterir.
Uygulama PaymentSystem interface'ini tanımla
PaymentSystem adında bir interface tanımla. İçine void pay(double amount) imzasını ekle. default void receipt(double amount) metodu ekle; gövdesi System.out.println("Makbuz: " + amount); yazsın. main içinde bir interface değişkeni tanımlama; sadece interface tanımını yaz. Sonra class Main içinde main metoduyla System.out.println("Hazır"); yazdır.
Başlangıç kodu
// PaymentSystem interface'ini tanımla
// pay imzası ve default receipt metodu ekle
// Main sınıfında hazır mesajı yazdırÖrnek çözüm
interface PaymentSystem {
void pay(double amount);
default void receipt(double amount) {
System.out.println("Makbuz: " + amount);
}
}
class Main {
public static void main(String[] args) {
System.out.println("Hazır");
}
}Beklenen çıktı
Hazır
Uygulama CreditCard sınıfını uygula
Bu dosyaya hem PaymentSystem interface'ini hem de CreditCard sınıfını yaz. Interface içinde void pay(double amount) imzası ve default void receipt(double amount) metodu olsun; receipt gövdesi System.out.println("Makbuz: " + amount); yazsın. class CreditCard implements PaymentSystem yaz; public void pay(double amount) metodunu gövdesiyle yaz ve içine System.out.println("Kredi kartıyla ödendi: " + amount); koy. main içinde CreditCard card = new CreditCard(); ile card.pay(150) ve card.receipt(150) çağır.
Başlangıç kodu
// PaymentSystem interface'ini tanımla
// CreditCard implements PaymentSystem yaz
// pay metodunu gövdesiyle yaz
// main'de kartla öde ve makbuzu çağırÖrnek çözüm
interface PaymentSystem {
void pay(double amount);
default void receipt(double amount) {
System.out.println("Makbuz: " + amount);
}
}
class CreditCard implements PaymentSystem {
public void pay(double amount) {
System.out.println("Kredi kartıyla ödendi: " + amount);
}
}
class Main {
public static void main(String[] args) {
CreditCard card = new CreditCard();
card.pay(150);
card.receipt(150);
}
}Beklenen çıktı
Kredi kartıyla ödendi: 150 Makbuz: 150
Soru ve cevaplar
Dersin sorularını cevaplarıyla birlikte inceleyebilirsin. Kendini denemek istersen dersi interaktif olarak çöz.
1 PaymentSystem interface'inde receipt metodu nasıl olmalı?
- abstract metot
- default metot Doğru cevap
- private metot
- static metot
Açıklama: receipt default metot olarak gövdeyle tanımlanır.
2 Ödeme sistemi parçalarını eşle
| Kavram | Anlamı |
|---|---|
PaymentSystem |
Sözleşme |
CreditCard |
Uygulayan sınıf |
pay |
Soyut metot |
receipt |
default metot |
3 implements satırını tamamla
class CreditCard ____ PaymentSystem {
public void pay(double amount) { }
}
class CreditCard implements PaymentSystem {
public void pay(double amount) { }
}
Açıklama: CreditCard implements PaymentSystem yazar.