mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-08 15:56:47 +02:00
final Java translation
can be used
This commit is contained in:
committed by
Andre Polykanine A.K.A. Menelion Elensúlë
parent
5668de0646
commit
dee2ed5f7c
@@ -9,8 +9,8 @@ contributors:
|
|||||||
- ["Cameron Schermerhorn", "http://github.com/cschermerhorn"]
|
- ["Cameron Schermerhorn", "http://github.com/cschermerhorn"]
|
||||||
- ["Rachel Stiyer", "https://github.com/rstiyer"]
|
- ["Rachel Stiyer", "https://github.com/rstiyer"]
|
||||||
translators:
|
translators:
|
||||||
- ["Oleksandr Tatarchuk", "http://github.com/tatarchuk"]
|
- ["Oleksandr Tatarchuk", "https://github.com/tatarchuk"]
|
||||||
filename: LearnJava.java
|
filename: LearnJavaUa.java
|
||||||
lang: uk-ua
|
lang: uk-ua
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -252,92 +252,88 @@ public class LearnJava {
|
|||||||
System.out.println(--i); // i = 0, prints 0 (pre-decrement)
|
System.out.println(--i); // i = 0, prints 0 (pre-decrement)
|
||||||
|
|
||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
// Control Structures
|
// Управляючі конструкції
|
||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
System.out.println("\n->Control Structures");
|
System.out.println("\n->Control Structures");
|
||||||
|
|
||||||
// If statements are c-like
|
// Оператор if використовується так само, як у мові С
|
||||||
int j = 10;
|
int j = 10;
|
||||||
if (j == 10) {
|
if (j == 10) {
|
||||||
System.out.println("I get printed");
|
System.out.println("Це надрукується");
|
||||||
} else if (j > 10) {
|
} else if (j > 10) {
|
||||||
System.out.println("I don't");
|
System.out.println("А це - ні");
|
||||||
} else {
|
} else {
|
||||||
System.out.println("I also don't");
|
System.out.println("Це - також ні");
|
||||||
}
|
}
|
||||||
|
|
||||||
// While loop
|
// Цикл з передумовою While
|
||||||
int fooWhile = 0;
|
int fooWhile = 0;
|
||||||
while(fooWhile < 100) {
|
while(fooWhile < 100) {
|
||||||
System.out.println(fooWhile);
|
System.out.println(fooWhile);
|
||||||
// Increment the counter
|
// Інкремент лічильника
|
||||||
// Iterated 100 times, fooWhile 0,1,2...99
|
// Виконається 100 разів, fooWhile 0,1,2...99
|
||||||
fooWhile++;
|
fooWhile++;
|
||||||
}
|
}
|
||||||
System.out.println("fooWhile Value: " + fooWhile);
|
System.out.println("fooWhile Value: " + fooWhile);
|
||||||
|
|
||||||
// Do While Loop
|
// Цикл з післяумовою Do While
|
||||||
int fooDoWhile = 0;
|
int fooDoWhile = 0;
|
||||||
do {
|
do {
|
||||||
System.out.println(fooDoWhile);
|
System.out.println(fooDoWhile);
|
||||||
// Increment the counter
|
// Інкремент лічильника
|
||||||
// Iterated 99 times, fooDoWhile 0->99
|
// Виконається 99 разів, fooDoWhile 0->99
|
||||||
fooDoWhile++;
|
fooDoWhile++;
|
||||||
} while(fooDoWhile < 100);
|
} while(fooDoWhile < 100);
|
||||||
System.out.println("fooDoWhile Value: " + fooDoWhile);
|
System.out.println("fooDoWhile Value: " + fooDoWhile);
|
||||||
|
|
||||||
// For Loop
|
// Цикл з параметром For
|
||||||
// for loop structure => for(<start_statement>; <conditional>; <step>)
|
// структура циклу => for(<початковий стан>; <умова завершення>; <крок>)
|
||||||
for (int fooFor = 0; fooFor < 10; fooFor++) {
|
for (int fooFor = 0; fooFor < 10; fooFor++) {
|
||||||
System.out.println(fooFor);
|
System.out.println(fooFor);
|
||||||
// Iterated 10 times, fooFor 0->9
|
// Виконається 10 разів, fooFor 0->9
|
||||||
}
|
}
|
||||||
System.out.println("fooFor Value: " + fooFor);
|
System.out.println("fooFor Value: " + fooFor);
|
||||||
|
|
||||||
// Nested For Loop Exit with Label
|
// Вихід з вкладеного циклу через Label
|
||||||
outer:
|
outer:
|
||||||
for (int i = 0; i < 10; i++) {
|
for (int i = 0; i < 10; i++) {
|
||||||
for (int j = 0; j < 10; j++) {
|
for (int j = 0; j < 10; j++) {
|
||||||
if (i == 5 && j ==5) {
|
if (i == 5 && j ==5) {
|
||||||
break outer;
|
break outer;
|
||||||
// breaks out of outer loop instead of only the inner one
|
// вихід із зовнішнього циклу, а не лише внутрішнього
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// For Each Loop
|
// Цикл For Each
|
||||||
// The for loop is also able to iterate over arrays as well as objects
|
// Призначений для перебору масивів та колекцій
|
||||||
// that implement the Iterable interface.
|
|
||||||
int[] fooList = {1, 2, 3, 4, 5, 6, 7, 8, 9};
|
int[] fooList = {1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||||
// for each loop structure => for (<object> : <iterable>)
|
|
||||||
// reads as: for each element in the iterable
|
|
||||||
// note: the object type must match the element type of the iterable.
|
|
||||||
|
|
||||||
for (int bar : fooList) {
|
for (int bar : fooList) {
|
||||||
System.out.println(bar);
|
System.out.println(bar);
|
||||||
//Iterates 9 times and prints 1-9 on new lines
|
//Iterates 9 times and prints 1-9 on new lines
|
||||||
}
|
}
|
||||||
|
|
||||||
// Switch Case
|
// Оператор вибору Switch Case
|
||||||
// A switch works with the byte, short, char, and int data types.
|
// Оператор вибору працює з типами даних byte, short, char, int.
|
||||||
// It also works with enumerated types (discussed in Enum Types), the
|
// Також працює з переліками Enum,
|
||||||
// String class, and a few special classes that wrap primitive types:
|
// класом String та класами-обгортками примітивних типів:
|
||||||
// Character, Byte, Short, and Integer.
|
// Character, Byte, Short та Integer.
|
||||||
int month = 3;
|
int month = 3;
|
||||||
String monthString;
|
String monthString;
|
||||||
switch (month) {
|
switch (month) {
|
||||||
case 1: monthString = "January";
|
case 1: monthString = "Січень";
|
||||||
break;
|
break;
|
||||||
case 2: monthString = "February";
|
case 2: monthString = "Лютий";
|
||||||
break;
|
break;
|
||||||
case 3: monthString = "March";
|
case 3: monthString = "Березень";
|
||||||
break;
|
break;
|
||||||
default: monthString = "Some other month";
|
default: monthString = "Інший місяць";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
System.out.println("Switch Case Result: " + monthString);
|
System.out.println("Switch Case результат: " + monthString);
|
||||||
|
|
||||||
// Starting in Java 7 and above, switching Strings works like this:
|
// Починаючи з Java 7 і далі, вибір рядкових змінних здійснюється так:
|
||||||
String myAnswer = "maybe";
|
String myAnswer = "maybe";
|
||||||
switch(myAnswer) {
|
switch(myAnswer) {
|
||||||
case "yes":
|
case "yes":
|
||||||
@@ -354,59 +350,55 @@ public class LearnJava {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Conditional Shorthand
|
// Тернарний оператор вибору
|
||||||
// You can use the '?' operator for quick assignments or logic forks.
|
// Можна використовувати '?' оператор для визначення умови.
|
||||||
// Reads as "If (statement) is true, use <first value>, otherwise, use
|
// Читається так "Якщо (умова) вірна, то <перше значення>, інакше
|
||||||
// <second value>"
|
// <друге значення"
|
||||||
int foo = 5;
|
int foo = 5;
|
||||||
String bar = (foo < 10) ? "A" : "B";
|
String bar = (foo < 10) ? "A" : "B";
|
||||||
System.out.println(bar); // Prints A, because the statement is true
|
System.out.println(bar); // Надрукується А, бо умова вірна
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////
|
////////////////////////////////////////
|
||||||
// Converting Data Types And Typecasting
|
// Перетворення типів
|
||||||
////////////////////////////////////////
|
////////////////////////////////////////
|
||||||
|
|
||||||
// Converting data
|
// Перетворення String на Integer
|
||||||
|
Integer.parseInt("123");//поверне числову версію рядка "123"
|
||||||
|
|
||||||
// Convert String To Integer
|
// Перетворення Integer на String
|
||||||
Integer.parseInt("123");//returns an integer version of "123"
|
Integer.toString(123);//повертає рядкову версію 123
|
||||||
|
|
||||||
// Convert Integer To String
|
// Для інших перетворень є наступні класи:
|
||||||
Integer.toString(123);//returns a string version of 123
|
|
||||||
|
|
||||||
// For other conversions check out the following classes:
|
|
||||||
// Double
|
// Double
|
||||||
// Long
|
// Long
|
||||||
// String
|
// String
|
||||||
|
|
||||||
// Typecasting
|
// Приведення типів
|
||||||
// You can also cast Java objects, there's a lot of details and deals
|
// Тут можна прочитати про приведення об'єктів:
|
||||||
// with some more intermediate concepts. Feel free to check it out here:
|
|
||||||
// http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
|
// http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
// Classes And Functions
|
// Класи та функції
|
||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
|
|
||||||
System.out.println("\n->Classes & Functions");
|
System.out.println("\n->Classes & Functions");
|
||||||
|
|
||||||
// (definition of the Bicycle class follows)
|
// (Клас Bicycle наведений нижче)
|
||||||
|
|
||||||
// Use new to instantiate a class
|
// Новий об'єкт класу
|
||||||
Bicycle trek = new Bicycle();
|
Bicycle trek = new Bicycle();
|
||||||
|
|
||||||
// Call object methods
|
// Виклик методу об'єкта
|
||||||
trek.speedUp(3); // You should always use setter and getter methods
|
trek.speedUp(3); // Постійно використовуються методи з назвами set і get
|
||||||
trek.setCadence(100);
|
trek.setCadence(100);
|
||||||
|
|
||||||
// toString returns this Object's string representation.
|
// toString повертає рядкове представленя об'єкту.
|
||||||
System.out.println("trek info: " + trek.toString());
|
System.out.println("trek info: " + trek.toString());
|
||||||
|
|
||||||
// Double Brace Initialization
|
// У Java немає синтаксису для явного створення статичних колекцій.
|
||||||
// The Java Language has no syntax for how to create static Collections
|
// Це можна зробити так:
|
||||||
// in an easy way. Usually you end up in the following way:
|
|
||||||
|
|
||||||
private static final Set<String> COUNTRIES = new HashSet<String>();
|
private static final Set<String> COUNTRIES = new HashSet<String>();
|
||||||
static {
|
static {
|
||||||
@@ -415,9 +407,7 @@ public class LearnJava {
|
|||||||
validCodes.add("FINLAND");
|
validCodes.add("FINLAND");
|
||||||
}
|
}
|
||||||
|
|
||||||
// But there's a nifty way to achieve the same thing in an
|
// Але є інший спосіб - Double Brace Initialization.
|
||||||
// easier way, by using something that is called Double Brace
|
|
||||||
// Initialization.
|
|
||||||
|
|
||||||
private static final Set<String> COUNTRIES = new HashSet<String>() {{
|
private static final Set<String> COUNTRIES = new HashSet<String>() {{
|
||||||
add("DENMARK");
|
add("DENMARK");
|
||||||
@@ -425,49 +415,44 @@ public class LearnJava {
|
|||||||
add("FINLAND");
|
add("FINLAND");
|
||||||
}}
|
}}
|
||||||
|
|
||||||
// The first brace is creating a new AnonymousInnerClass and the
|
// Використовується анонімний внутрішній клас
|
||||||
// second one declares an instance initializer block. This block
|
|
||||||
// is called when the anonymous inner class is created.
|
|
||||||
// This does not only work for Collections, it works for all
|
|
||||||
// non-final classes.
|
|
||||||
|
|
||||||
} // End main method
|
} // Кінець методу main
|
||||||
} // End LearnJava class
|
} // Кінець класу LearnJava
|
||||||
|
|
||||||
|
|
||||||
// You can include other, non-public outer-level classes in a .java file,
|
// Можна додавати інші, не public класи зовнішнього рівня у .java файл,
|
||||||
// but it is good practice. Instead split classes into separate files.
|
// але це не є хорошою практикою. Розміщуйте класи в окремих файлах.
|
||||||
|
|
||||||
|
|
||||||
// Class Declaration Syntax:
|
// Синтаксис оголошення класу:
|
||||||
// <public/private/protected> class <class name> {
|
// <public/private/protected> class <class name> {
|
||||||
// // data fields, constructors, functions all inside.
|
// // поля, конструктори, функції та ін.
|
||||||
// // functions are called as methods in Java.
|
// // у Java функції називаються методами.
|
||||||
// }
|
// }
|
||||||
|
|
||||||
class Bicycle {
|
class Bicycle {
|
||||||
|
|
||||||
// Bicycle's Fields/Variables
|
// Поля (змінні) класу Bicycle
|
||||||
public int cadence; // Public: Can be accessed from anywhere
|
public int cadence; // Public: доступно звідусіль
|
||||||
private int speed; // Private: Only accessible from within the class
|
private int speed; // Private: доступно лише у межах класу
|
||||||
protected int gear; // Protected: Accessible from the class and subclasses
|
protected int gear; // Protected: доступно лише класу та нащадкам
|
||||||
String name; // default: Only accessible from within this package
|
String name; // default: доступно у даному пакеті
|
||||||
|
|
||||||
static String className; // Static class variable
|
static String className; // статична змінна класу
|
||||||
|
|
||||||
// Static block
|
// статичний блок
|
||||||
// Java has no implementation of static constructors, but
|
// Java не має статичних конструкторів, але
|
||||||
// has a static block that can be used to initialize class variables
|
// має статичний блок ініціалізації змінних класу
|
||||||
// (static variables).
|
// Цей блок виконується при завантаженні класу.
|
||||||
// This block will be called when the class is loaded.
|
|
||||||
static {
|
static {
|
||||||
className = "Bicycle";
|
className = "Bicycle";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Constructors are a way of creating classes
|
// Конструктори є способом створення класу
|
||||||
// This is a constructor
|
// Це - конструктор
|
||||||
public Bicycle() {
|
public Bicycle() {
|
||||||
// You can also call another constructor:
|
// Можна викликати інший конструктор:
|
||||||
// this(1, 50, 5, "Bontrager");
|
// this(1, 50, 5, "Bontrager");
|
||||||
gear = 1;
|
gear = 1;
|
||||||
cadence = 50;
|
cadence = 50;
|
||||||
@@ -475,7 +460,7 @@ class Bicycle {
|
|||||||
name = "Bontrager";
|
name = "Bontrager";
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is a constructor that takes arguments
|
// Цей конструктор приймає аргументи
|
||||||
public Bicycle(int startCadence, int startSpeed, int startGear,
|
public Bicycle(int startCadence, int startSpeed, int startGear,
|
||||||
String name) {
|
String name) {
|
||||||
this.gear = startGear;
|
this.gear = startGear;
|
||||||
@@ -484,18 +469,18 @@ class Bicycle {
|
|||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Method Syntax:
|
// Синтаксис методу:
|
||||||
// <public/private/protected> <return type> <function name>(<args>)
|
// <public/private/protected> <тип повернутого значення> <ім'я методу>(<аргументи>)
|
||||||
|
|
||||||
// Java classes often implement getters and setters for their fields
|
// Java класи часто мають методи для отримання та встановлення змінних
|
||||||
|
|
||||||
// Method declaration syntax:
|
// Синтаксис оголошення методу:
|
||||||
// <access modifier> <return type> <method name>(<args>)
|
// <модифікатор доступу> <тип повернутого значення> <ім'я методу>(<аргументи>)
|
||||||
public int getCadence() {
|
public int getCadence() {
|
||||||
return cadence;
|
return cadence;
|
||||||
}
|
}
|
||||||
|
|
||||||
// void methods require no return statement
|
// void методи не повертають значень
|
||||||
public void setCadence(int newValue) {
|
public void setCadence(int newValue) {
|
||||||
cadence = newValue;
|
cadence = newValue;
|
||||||
}
|
}
|
||||||
@@ -520,26 +505,26 @@ class Bicycle {
|
|||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Method to display the attribute values of this Object.
|
//Метод показує значення змінних об'єкту.
|
||||||
@Override // Inherited from the Object class.
|
@Override // Inherited from the Object class.
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "gear: " + gear + " cadence: " + cadence + " speed: " + speed +
|
return "gear: " + gear + " cadence: " + cadence + " speed: " + speed +
|
||||||
" name: " + name;
|
" name: " + name;
|
||||||
}
|
}
|
||||||
} // end class Bicycle
|
} // кінець класу Bicycle
|
||||||
|
|
||||||
// PennyFarthing is a subclass of Bicycle
|
// PennyFarthing є розширенням (нащадком) класу Bicycle
|
||||||
class PennyFarthing extends Bicycle {
|
class PennyFarthing extends Bicycle {
|
||||||
// (Penny Farthings are those bicycles with the big front wheel.
|
// (Penny Farthings мають велике переднє колесо.
|
||||||
// They have no gears.)
|
// Вони не мають передач.)
|
||||||
|
|
||||||
public PennyFarthing(int startCadence, int startSpeed){
|
public PennyFarthing(int startCadence, int startSpeed){
|
||||||
// Call the parent constructor with super
|
// Виклик батьківського конструктора через super
|
||||||
super(startCadence, startSpeed, 0, "PennyFarthing");
|
super(startCadence, startSpeed, 0, "PennyFarthing");
|
||||||
}
|
}
|
||||||
|
|
||||||
// You should mark a method you're overriding with an @annotation.
|
// Перевизначений метод має відти відмічений аннотацією @annotation.
|
||||||
// To learn more about what annotations are and their purpose check this
|
// Для ознайомлення з аннотаціями перейдіть за посиланням
|
||||||
// out: http://docs.oracle.com/javase/tutorial/java/annotations/
|
// out: http://docs.oracle.com/javase/tutorial/java/annotations/
|
||||||
@Override
|
@Override
|
||||||
public void setGear(int gear) {
|
public void setGear(int gear) {
|
||||||
@@ -547,17 +532,17 @@ class PennyFarthing extends Bicycle {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Interfaces
|
// Інтерфейси
|
||||||
// Interface declaration syntax
|
// Синтаксис оголошення інтерфейсів
|
||||||
// <access-level> interface <interface-name> extends <super-interfaces> {
|
// <рівень доступу> interface <ім'я інтерфейсу> extends <супер-інтерфейс> {
|
||||||
// // Constants
|
// // Констатнти
|
||||||
// // Method declarations
|
// // Оголошення методів
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// Example - Food:
|
//Приклад - Food:
|
||||||
public interface Edible {
|
public interface Edible {
|
||||||
public void eat(); // Any class that implements this interface, must
|
public void eat(); // Будь-які класи, що реалізують цей інтерфейс
|
||||||
// implement this method.
|
// повинні реалізувати цей метод.
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface Digestible {
|
public interface Digestible {
|
||||||
@@ -565,7 +550,7 @@ public interface Digestible {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// We can now create a class that implements both of these interfaces.
|
// Можна створити клас, що реалізує обидва інтерфейси.
|
||||||
public class Fruit implements Edible, Digestible {
|
public class Fruit implements Edible, Digestible {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -579,8 +564,8 @@ public class Fruit implements Edible, Digestible {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// In Java, you can extend only one class, but you can implement many
|
// В Java можна успадковувати лише один клас, але реалізовувати багато
|
||||||
// interfaces. For example:
|
// інтерфейсів. Наприклад:
|
||||||
public class ExampleClass extends ExampleClassParent implements InterfaceOne,
|
public class ExampleClass extends ExampleClassParent implements InterfaceOne,
|
||||||
InterfaceTwo {
|
InterfaceTwo {
|
||||||
|
|
||||||
@@ -594,37 +579,35 @@ public class ExampleClass extends ExampleClassParent implements InterfaceOne,
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Abstract Classes
|
// Абстрактні класи
|
||||||
|
|
||||||
// Abstract Class declaration syntax
|
// Синтаксис оголошення абстрактних класів:
|
||||||
// <access-level> abstract <abstract-class-name> extends <super-abstract-classes> {
|
// <ріаень доступу> abstract <ім1я класу> extends <супер-абстрактний клас> {
|
||||||
// // Constants and variables
|
// // Константи і змінні
|
||||||
// // Method declarations
|
// // Оголошення методів
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// Marking a class as abstract means that it contains abstract methods that must
|
// Позначення класу як абстрактного означає, що оголошені у ньому методи мають
|
||||||
// be defined in a child class. Similar to interfaces, abstract classes cannot
|
// бути реалізовані у дочірніх класах. Подібно до інтерфейсів, не можна створити екземпляри
|
||||||
// be instantiated, but instead must be extended and the abstract methods
|
// абстракних класів, але їх можна успадковувати. Нащадок зобов'язаний реалізувати всі абстрактні
|
||||||
// defined. Different from interfaces, abstract classes can contain a mixture of
|
// методи. на відміну від інтерфейсів, абстрактні класи можуть мати як визначені,
|
||||||
// concrete and abstract methods. Methods in an interface cannot have a body,
|
// так і абстрактні методи. Методи в інтерфейсах не мають тіла,
|
||||||
// unless the method is static, and variables are final by default, unlike an
|
// за вийнятком статичних методів, а змінні неявно мають модифікатор final, на відміну від
|
||||||
// abstract class. Also abstract classes CAN have the "main" method.
|
// абстрактного класу. Абстрактні класи МОЖУТЬ мати метод "main".
|
||||||
|
|
||||||
public abstract class Animal
|
public abstract class Animal
|
||||||
{
|
{
|
||||||
public abstract void makeSound();
|
public abstract void makeSound();
|
||||||
|
|
||||||
// Method can have a body
|
// Метод може мати тіло
|
||||||
public void eat()
|
public void eat()
|
||||||
{
|
{
|
||||||
System.out.println("I am an animal and I am Eating.");
|
System.out.println("I am an animal and I am Eating.");
|
||||||
// Note: We can access private variable here.
|
// Зауваження: є доступ до privat змінних.
|
||||||
age = 30;
|
age = 30;
|
||||||
}
|
}
|
||||||
|
|
||||||
// No need to initialize, however in an interface
|
// Ініціалізація не потрібна
|
||||||
// a variable is implicitly final and hence has
|
|
||||||
// to be initialized.
|
|
||||||
protected int age;
|
protected int age;
|
||||||
|
|
||||||
public void printAge()
|
public void printAge()
|
||||||
@@ -632,7 +615,7 @@ public abstract class Animal
|
|||||||
System.out.println(age);
|
System.out.println(age);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Abstract classes can have main function.
|
// Абстрактні класи МОЖУТЬ мати метод "main".
|
||||||
public static void main(String[] args)
|
public static void main(String[] args)
|
||||||
{
|
{
|
||||||
System.out.println("I am abstract");
|
System.out.println("I am abstract");
|
||||||
@@ -641,20 +624,19 @@ public abstract class Animal
|
|||||||
|
|
||||||
class Dog extends Animal
|
class Dog extends Animal
|
||||||
{
|
{
|
||||||
// Note still have to override the abstract methods in the
|
// Слід помічати перевизначення абстрактних методів
|
||||||
// abstract class.
|
|
||||||
@Override
|
@Override
|
||||||
public void makeSound()
|
public void makeSound()
|
||||||
{
|
{
|
||||||
System.out.println("Bark");
|
System.out.println("Bark");
|
||||||
// age = 30; ==> ERROR! age is private to Animal
|
// age = 30; ==> ПОМИЛКА! age є private для Animal
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: You will get an error if you used the
|
// NOTE: Буде помилка, якщо використати аннотацію
|
||||||
// @Override annotation here, since java doesn't allow
|
// @Override тут, так як у java не можна
|
||||||
// overriding of static methods.
|
// перевизначати статичні методи.
|
||||||
// What is happening here is called METHOD HIDING.
|
// Те, що тут відбувається, називається METHOD HIDING.
|
||||||
// Check out this awesome SO post: http://stackoverflow.com/questions/16313649/
|
// Більш детально: http://stackoverflow.com/questions/16313649/
|
||||||
public static void main(String[] args)
|
public static void main(String[] args)
|
||||||
{
|
{
|
||||||
Dog pluto = new Dog();
|
Dog pluto = new Dog();
|
||||||
@@ -664,22 +646,20 @@ class Dog extends Animal
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Final Classes
|
// Final класи
|
||||||
|
|
||||||
// Final Class declaration syntax
|
// Синтаксис оголошення Final класів
|
||||||
// <access-level> final <final-class-name> {
|
// <рівень доступу> final <ім'я класу> {
|
||||||
// // Constants and variables
|
// // Константи і змінні
|
||||||
// // Method declarations
|
// // Оголошення методів
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// Final classes are classes that cannot be inherited from and are therefore a
|
// Final не можуть мати нащадків, також самі вони є останніми нащадками.
|
||||||
// final child. In a way, final classes are the opposite of abstract classes
|
// Final класи є протилежністю абстрактних у цьому плані.
|
||||||
// because abstract classes must be extended, but final classes cannot be
|
|
||||||
// extended.
|
|
||||||
public final class SaberToothedCat extends Animal
|
public final class SaberToothedCat extends Animal
|
||||||
{
|
{
|
||||||
// Note still have to override the abstract methods in the
|
// Перевизначення методу
|
||||||
// abstract class.
|
|
||||||
@Override
|
@Override
|
||||||
public void makeSound()
|
public void makeSound()
|
||||||
{
|
{
|
||||||
@@ -687,14 +667,14 @@ public final class SaberToothedCat extends Animal
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Final Methods
|
// Final методи
|
||||||
public abstract class Mammal()
|
public abstract class Mammal()
|
||||||
{
|
{
|
||||||
// Final Method Syntax:
|
// Синтаксис Final методів:
|
||||||
// <access modifier> final <return type> <function name>(<args>)
|
// <модифікаор доступу> final <тип повернутого значення> <ім'я функції>(<аргументи>)
|
||||||
|
|
||||||
// Final methods, like, final classes cannot be overridden by a child class,
|
// Final методи не можуть бути перевизначені класом-нащадком,
|
||||||
// and are therefore the final implementation of the method.
|
// вони є остаточною реалізацією методу.
|
||||||
public final boolean isWarmBlooded()
|
public final boolean isWarmBlooded()
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
@@ -702,21 +682,20 @@ public abstract class Mammal()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Enum Type
|
// Тип Enum (перелік)
|
||||||
//
|
//
|
||||||
// An enum type is a special data type that enables for a variable to be a set
|
// Enum є спеціальним типом даних, який дозволяє змінним бути певною множиною
|
||||||
// of predefined constants. The variable must be equal to one of the values that
|
// визначених констант. Змінна має відповідати одному зі значень, що
|
||||||
// have been predefined for it. Because they are constants, the names of an enum
|
// заздалегідь визначені для неї. Так як це константи, імена типів полів у enum
|
||||||
// type's fields are in uppercase letters. In the Java programming language, you
|
// задаються у верхньому регістрі. У Java задається тип переліку за допомогою
|
||||||
// define an enum type by using the enum keyword. For example, you would specify
|
// ключового слова. Наприклад, перелік днів тижня можна задати так:
|
||||||
// a days-of-the-week enum type as:
|
|
||||||
|
|
||||||
public enum Day {
|
public enum Day {
|
||||||
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
|
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
|
||||||
THURSDAY, FRIDAY, SATURDAY
|
THURSDAY, FRIDAY, SATURDAY
|
||||||
}
|
}
|
||||||
|
|
||||||
// We can use our enum Day like that:
|
// Можна використовувати перелік Day так:
|
||||||
|
|
||||||
public class EnumTest {
|
public class EnumTest {
|
||||||
|
|
||||||
@@ -756,13 +735,13 @@ public class EnumTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enum types are much more powerful than we show above.
|
// Переліки набагато потужніші, ніж тут показано.
|
||||||
// The enum body can include methods and other fields.
|
// Тіло переліків може містити методи та інші змінні.
|
||||||
// You can se more at https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
|
// Дивіться більше тут: https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Додатково для прочитання
|
## Додатково для читання
|
||||||
|
|
||||||
The links provided here below are just to get an understanding of the topic, feel free to Google and find specific examples.
|
The links provided here below are just to get an understanding of the topic, feel free to Google and find specific examples.
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user