fix: converted single to triple backticks9 (#36236)
This commit is contained in:
@@ -6,28 +6,30 @@ localeTitle: واجهات
|
||||
|
||||
واجهة في جافا قليلا مثل فئة، ولكن مع فارق كبير: و `interface` يمكن أن يكون _فقط_ تواقيع طريقة والحقول والطرق الافتراضية. منذ Java 8 ، يمكنك أيضًا إنشاء [طرق افتراضية](https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html) . في الجزء التالي ، يمكنك مشاهدة مثال على الواجهة:
|
||||
|
||||
`public interface Vehicle {
|
||||
public String licensePlate = "";
|
||||
public float maxVel
|
||||
public void start();
|
||||
public void stop();
|
||||
default void blowHorn(){
|
||||
System.out.println("Blowing horn");
|
||||
}
|
||||
}
|
||||
`
|
||||
```java
|
||||
public interface Vehicle {
|
||||
public String licensePlate = "";
|
||||
public float maxVel
|
||||
public void start();
|
||||
public void stop();
|
||||
default void blowHorn(){
|
||||
System.out.println("Blowing horn");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
تحتوي الواجهة أعلاه على حقلين وطريقتين وطريقة افتراضية. وحدها ، ليست ذات فائدة كبيرة ، ولكنها تستخدم عادة مع الفصول. ماذا؟ بسيطة ، عليك أن تتأكد من أن بعض الصف يقوم `implements` .
|
||||
|
||||
`public class Car implements Vehicle {
|
||||
public void start() {
|
||||
System.out.println("starting engine...");
|
||||
}
|
||||
public void stop() {
|
||||
System.out.println("stopping engine...");
|
||||
}
|
||||
}
|
||||
`
|
||||
```java
|
||||
public class Car implements Vehicle {
|
||||
public void start() {
|
||||
System.out.println("starting engine...");
|
||||
}
|
||||
public void stop() {
|
||||
System.out.println("stopping engine...");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
 [تشغيل الكود](https://repl.it/CItd/0)
|
||||
|
||||
@@ -37,39 +39,41 @@ localeTitle: واجهات
|
||||
|
||||
بمجرد إنشاء فئة Java تقوم `implements` أي واجهة ، يمكن الإشارة إلى مثيل الكائن كمثيل للواجهة. هذا المفهوم مشابه لمبدأ إنشاء الوراثة.
|
||||
|
||||
`// following our previous example
|
||||
|
||||
Vehicle tesla = new Car();
|
||||
|
||||
tesla.start(); // starting engine ...
|
||||
`
|
||||
```java
|
||||
// following our previous example
|
||||
|
||||
Vehicle tesla = new Car();
|
||||
|
||||
tesla.start(); // starting engine ...
|
||||
```
|
||||
|
||||
**لا يمكن أن** تحتوي واجهة على أساليب منشئ ، لذلك **لا يمكنك** إنشاء مثيل لواجهة بنفسها. يجب إنشاء مثيل لفئة معينة تقوم بتطبيق واجهة للرجوع إليها. فكر في الواجهات على أنها نموذج عقد فارغ ، أو نموذج.
|
||||
|
||||
ما الذي يمكنك القيام به مع هذه الميزة؟ تعدد الأشكال! يمكنك استخدام الواجهات فقط للإشارة إلى مثيلات الكائن!
|
||||
|
||||
`class Truck implements Vehicle {
|
||||
public void start() {
|
||||
System.out.println("starting truck engine...");
|
||||
}
|
||||
public void stop() {
|
||||
System.out.println("stopping truck engine...");
|
||||
}
|
||||
}
|
||||
|
||||
class Starter {
|
||||
// static method, can be called without instantiating the class
|
||||
public static void startEngine(Vehicle vehicle) {
|
||||
vehicle.start();
|
||||
}
|
||||
}
|
||||
|
||||
Vehicle tesla = new Car();
|
||||
Vehicle tata = new Truck();
|
||||
|
||||
Starter.startEngine(tesla); // starting engine ...
|
||||
Starter.startEngine(tata); // starting truck engine ...
|
||||
`
|
||||
```java
|
||||
class Truck implements Vehicle {
|
||||
public void start() {
|
||||
System.out.println("starting truck engine...");
|
||||
}
|
||||
public void stop() {
|
||||
System.out.println("stopping truck engine...");
|
||||
}
|
||||
}
|
||||
|
||||
class Starter {
|
||||
// static method, can be called without instantiating the class
|
||||
public static void startEngine(Vehicle vehicle) {
|
||||
vehicle.start();
|
||||
}
|
||||
}
|
||||
|
||||
Vehicle tesla = new Car();
|
||||
Vehicle tata = new Truck();
|
||||
|
||||
Starter.startEngine(tesla); // starting engine ...
|
||||
Starter.startEngine(tata); // starting truck engine ...
|
||||
```
|
||||
|
||||
 [تشغيل الكود](https://repl.it/CItm/0)
|
||||
|
||||
@@ -77,27 +81,28 @@ localeTitle: واجهات
|
||||
|
||||
نعم ، يمكنك تنفيذ واجهات متعددة في فصل واحد. أثناء [تواجدك](//forum.freecodecamp.com/t/java-docs-inheritance) في [التوارث](//forum.freecodecamp.com/t/java-docs-inheritance) داخل الصفوف ، تم تقييدك في فئة واحدة فقط ، يمكنك هنا توسيع أي عدد من الواجهات. ولكن لا ننسى تنفيذ _جميع_ أساليب جميع واجهات ، وإلا سوف تفشل التجميع!
|
||||
|
||||
`public interface GPS {
|
||||
public void getCoordinates();
|
||||
}
|
||||
|
||||
public interface Radio {
|
||||
public void startRadio();
|
||||
public void stopRadio();
|
||||
}
|
||||
|
||||
public class Smartphone implements GPS,Radio {
|
||||
public void getCoordinates() {
|
||||
// return some coordinates
|
||||
}
|
||||
public void startRadio() {
|
||||
// start Radio
|
||||
}
|
||||
public void stopRadio() {
|
||||
// stop Radio
|
||||
}
|
||||
}
|
||||
`
|
||||
```java
|
||||
public interface GPS {
|
||||
public void getCoordinates();
|
||||
}
|
||||
|
||||
public interface Radio {
|
||||
public void startRadio();
|
||||
public void stopRadio();
|
||||
}
|
||||
|
||||
public class Smartphone implements GPS,Radio {
|
||||
public void getCoordinates() {
|
||||
// return some coordinates
|
||||
}
|
||||
public void startRadio() {
|
||||
// start Radio
|
||||
}
|
||||
public void stopRadio() {
|
||||
// stop Radio
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
 [تشغيل الكود](https://repl.it/CIto/0)
|
||||
|
||||
@@ -116,37 +121,38 @@ localeTitle: واجهات
|
||||
|
||||
لحسن الحظ ، توفر لك Java 8 الآن الطرق `default` للواجهات. _يمكن أن_ تحتوي الطريقة `default` على التنفيذ الخاص بها _مباشرة_ داخل الواجهة! لذا ، إذا لم يقم تطبيق Class بتنفيذ طريقة افتراضية ، فسيأخذ المترجم التنفيذ المذكور داخل الواجهة. جميل أليس كذلك؟ لذلك في مكتبتك ، يمكنك إضافة أي عدد من الطرق الافتراضية في واجهات دون الخوف من كسر أي شيء!
|
||||
|
||||
`public interface GPS {
|
||||
public void getCoordinates();
|
||||
default public void getRoughCoordinates() {
|
||||
// implementation to return coordinates from rough sources
|
||||
// such as wifi & mobile
|
||||
System.out.println("Fetching rough coordinates...");
|
||||
}
|
||||
}
|
||||
|
||||
public interface Radio {
|
||||
public void startRadio();
|
||||
public void stopRadio();
|
||||
}
|
||||
|
||||
public class Smartphone implements GPS,Radio {
|
||||
public void getCoordinates() {
|
||||
// return some coordinates
|
||||
}
|
||||
public void startRadio() {
|
||||
// start Radio
|
||||
}
|
||||
public void stopRadio() {
|
||||
// stop Radio
|
||||
}
|
||||
|
||||
// no implementation of getRoughCoordinates()
|
||||
}
|
||||
|
||||
Smartphone motoG = new Smartphone();
|
||||
motog.getRoughCoordinates(); // Fetching rough coordinates...
|
||||
`
|
||||
```java
|
||||
public interface GPS {
|
||||
public void getCoordinates();
|
||||
default public void getRoughCoordinates() {
|
||||
// implementation to return coordinates from rough sources
|
||||
// such as wifi & mobile
|
||||
System.out.println("Fetching rough coordinates...");
|
||||
}
|
||||
}
|
||||
|
||||
public interface Radio {
|
||||
public void startRadio();
|
||||
public void stopRadio();
|
||||
}
|
||||
|
||||
public class Smartphone implements GPS,Radio {
|
||||
public void getCoordinates() {
|
||||
// return some coordinates
|
||||
}
|
||||
public void startRadio() {
|
||||
// start Radio
|
||||
}
|
||||
public void stopRadio() {
|
||||
// stop Radio
|
||||
}
|
||||
|
||||
// no implementation of getRoughCoordinates()
|
||||
}
|
||||
|
||||
Smartphone motoG = new Smartphone();
|
||||
motog.getRoughCoordinates(); // Fetching rough coordinates...
|
||||
```
|
||||
|
||||
 [تشغيل الكود](https://repl.it/CItp/0)
|
||||
|
||||
@@ -154,35 +160,36 @@ localeTitle: واجهات
|
||||
|
||||
سؤال رائع. في هذه الحالة ، إذا لم تقدم التطبيق في الصف ، فسوف يتم خلط المترجم الضعيف ويفشل ببساطة! يجب عليك توفير تنفيذ طريقة افتراضية داخل الفئة أيضًا. هناك أيضًا طريقة رائعة تستخدم ميزة `super` للاتصال بأي تطبيق تريده:
|
||||
|
||||
`public interface Radio {
|
||||
// public void startRadio();
|
||||
// public void stopRadio();
|
||||
|
||||
default public void next() {
|
||||
System.out.println("Next from Radio");
|
||||
}
|
||||
}
|
||||
|
||||
public interface MusicPlayer {
|
||||
// public void start();
|
||||
// public void pause();
|
||||
// public void stop();
|
||||
|
||||
default public void next() {
|
||||
System.out.println("Next from MusicPlayer");
|
||||
}
|
||||
}
|
||||
|
||||
public class Smartphone implements Radio, MusicPlayer {
|
||||
public void next() {
|
||||
// Suppose you want to call MusicPlayer next
|
||||
MusicPlayer.super.next();
|
||||
}
|
||||
}
|
||||
|
||||
Smartphone motoG = new Smartphone();
|
||||
motoG.next(); // Next from MusicPlayer
|
||||
`
|
||||
```java
|
||||
public interface Radio {
|
||||
// public void startRadio();
|
||||
// public void stopRadio();
|
||||
|
||||
default public void next() {
|
||||
System.out.println("Next from Radio");
|
||||
}
|
||||
}
|
||||
|
||||
public interface MusicPlayer {
|
||||
// public void start();
|
||||
// public void pause();
|
||||
// public void stop();
|
||||
|
||||
default public void next() {
|
||||
System.out.println("Next from MusicPlayer");
|
||||
}
|
||||
}
|
||||
|
||||
public class Smartphone implements Radio, MusicPlayer {
|
||||
public void next() {
|
||||
// Suppose you want to call MusicPlayer next
|
||||
MusicPlayer.super.next();
|
||||
}
|
||||
}
|
||||
|
||||
Smartphone motoG = new Smartphone();
|
||||
motoG.next(); // Next from MusicPlayer
|
||||
```
|
||||
|
||||
 [تشغيل الكود](https://repl.it/CIts/0)
|
||||
|
||||
@@ -220,33 +227,35 @@ localeTitle: واجهات
|
||||
|
||||
من الممكن أيضًا في Java لواجهة _ترث_ واجهة أخرى ، باستخدام ، تفكر في ذلك ، `extends` الكلمة الرئيسية:
|
||||
|
||||
`public interface Player {
|
||||
public void start();
|
||||
public void pause();
|
||||
public void stop();
|
||||
}
|
||||
|
||||
public interface MusicPlayer extends Player {
|
||||
default public void next() {
|
||||
System.out.println("Next from MusicPlayer");
|
||||
}
|
||||
}
|
||||
`
|
||||
```java
|
||||
public interface Player {
|
||||
public void start();
|
||||
public void pause();
|
||||
public void stop();
|
||||
}
|
||||
|
||||
public interface MusicPlayer extends Player {
|
||||
default public void next() {
|
||||
System.out.println("Next from MusicPlayer");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
هذا يعني ، أن تطبيق Class `MusicPlayer` Interface (واجهة `MusicPlayer` يجب أن يقوم بتنفيذ _جميع_ أساليب `MusicPlayer` بالإضافة إلى `Player` :
|
||||
|
||||
`public class SmartPhone implements MusicPlayer {
|
||||
public void start() {
|
||||
System.out.println("start");
|
||||
}
|
||||
public void stop() {
|
||||
System.out.println("stop");
|
||||
}
|
||||
public void pause() {
|
||||
System.out.println("pause");
|
||||
}
|
||||
}
|
||||
`
|
||||
```java
|
||||
public class SmartPhone implements MusicPlayer {
|
||||
public void start() {
|
||||
System.out.println("start");
|
||||
}
|
||||
public void stop() {
|
||||
System.out.println("stop");
|
||||
}
|
||||
public void pause() {
|
||||
System.out.println("pause");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
 [تشغيل الكود](https://repl.it/CIty/0)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user