/
GermanezZ
/
JavaExercises
Обзор
Документация
Войти
/
GermanezZ
/
JavaExercises
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
AllMetanitLessons/ClassesAndObjects/Interfaces/InterProg.java
76 строк
2 KB
germa
Уроки по Metanit
04 дек 2024, 20:39
04 дек 2024, 20:39
48d575a
Код
Авторство
О чём код?
package AllMetanitLessons.ClassesAndObjects.Interfaces; public class InterProg { public static void main(String[] args) { Book book1 = new Book("Anna Korenina","Lev Tolstoj"); book1.print(); NewInterface newInterface = new Journal("Booook1"); newInterface.print(); String name = ((Journal)newInterface).getName(); System.out.println(name); newInterface.Hello(); NewInterface.read(); // STATIC METHOD NewInterface newInterface1 = createPrintable("SomeRoman",true); newInterface1.print(); reading(newInterface); WaterPipe waterPipe = new WaterPipe(); waterPipe.printStatus(1); } static void reading(NewInterface n){ n.print(); } static NewInterface createPrintable(String name, boolean option){ if (option){ return new Book(name,"Unknown"); } else return new Journal(name); } } class Book implements NewInterface{ private String name; private String author; Book(String name, String author){ this.name = name; this.author = author; } @Override public void print() { System.out.printf("Book: %s, Author: %s \n", name,author); } @Override public void Hello(){ System.out.println("Hello, my friend"); } } class Journal implements NewInterface{ private String name; Journal(String name){ this.name = name; } public String getName() { return name; } @Override public void print() { System.out.printf("Journal: %s \n",name); } } class WaterPipe implements NewInterface{ }