Na poczatek pare pojec i wyjasnien, dla lepszego pozniej rozumiena bede "defefinicje" podawal w dwoch jezykach
ulatwia to pozniej uczenie sie do SCJP (bynjmniej mi) tak wiec zaczynamy:
- Klasa
- Szablon ktory opisuje stan i zachowanie jakiegos obiektu.
- Class
- A template that describes the kinds of state and behavior that objects of its type support.
- Obiekt
- Przy starcie wirtualnej maszyny kiedy napotyka ona na slowo kluczowe
new
, bedzie uzywac wlasciwej klasy przy tworzeniu obiektu, ktory bedzie instancja tej klasy. Obiekt bedzie posiadal swoj wlasny stan i dostep do wszystkich zachowan(metod) zdefiniowanych w jego klasie. - Object
- At runtime, when the Java Virtual Machine (JVM) encounters the new keyword, it will use the appropriate class to make an object which is an instance of that class. That object will have its own state, and access to all of the behaviors defined by its class.
- Stan (instancja zmiennych)
- Kazdy obiekt (instacja odpowiedniej klasy) bedzie mial swoj wlasny unikalny zbior zmiennych, zgodnie z definicja klasy. Wspolnie, wartosci przypisane do zmiennych w instancji obiektu tworza stan obiektu.
- State (instance variables)
- Each object (instance of a class) will have its own unique set of instance variables as defined in the class. Collectively, the values assigned to an object's instance variables make up the object's state.
- Zachowanie (metody)
- Kiedy program tworzy klase, ona tworzy metody dla tej klasy. Metody sa tam gdzie logika klasy jest przechowywana. Mtody sa tam gdzie cala praca jest zrobiona. Sa tam gdzie algorytmy sa wykonywane i tam gdzie zmieniaja sie dane.
- Behavior (methods)
- When a programmer creates a class, she creates methods for that class. Methods are where the class logic is stored. Methods are where the real work gets done. They are where algorithms get executed, and data gets manipulated.
- Indentyfikatory i Slowa kluczowe
- Wszystkie komponenty o ktorych jest mowa (klasy, zmienne, metody) potzrebuja nazw. W Javie nazwy te nazywane sa indentyfikatorami i sa odpowiednie zasady które mowia co stanowi dany iindentyfikator. Programisci Javy i sama firma (Sun) stworzyli pewne konwencje dla nazewnictwa metod, zmiennych i klas. Jak we wszystkich jezykach programowania, Java posiada zestaw wbudowanych słów kluczowych. Tych nazw nie mozna uzywac jako identyfikatorow.
- Identifiers and Keywords
- All the Java components we just talked about—classes, variables, and methods—need names. In Java these names are called identifiers, there are rules for what constitutes a legal Java identifier. Beyond what's legal, though, Java programmers (and Sun) have created conventions for naming methods, variables, and classes. Like all programming languages, Java has a set of built-in keywords. These keywords must not be used as identifiers.
- Dziedziczenie
- Dla Javy i innych jezykow programowania obiektowego dzidziczenie jest kluczowym mechanizmem, ktory pozwala dla raz zdefiniowanej klasy uzyc tego kodu ponownie dla innej klasy. W Javie mozesz definiowac ogolne klasy zwane nadklasa czy tez superklasa (chodzi o klasy abstrakcyjne), a pozniej rozszezenie tych nadklas na konkretne podklasy. Nadklasa nie bedzie wiedziala kto po niej dziedziczy, ale wszystkie podklasy musza miec zadeklarowane po jakiej nadklasie dziedzicza. Podklasa ktora dziedziczy z nadklasy, automatycznie daje jej dostep do swojch (zdefiniowanych w niej) zmiennych i metod, ale takze daje mozliwosc do nadpisania jakies metody, do zdefiniowania bardziej dokladnego zachowania. Na przyklad, klasa Auto niech bedzie klasa abstrakcyjna czyli nasz nadklasa, moze definiowac wszystkie ogolne metody, zachowania jakie posiadaja wszystkie samochody, ale np. klasa Ferrari bedzie nasza podklasa dziedziczaca po nadklasie moze nadpisac metode przyspieszenie() i tym samym okreslic dokladniej jak ma sie zachowywac.
- Inheritance
- Central to Java and other object-oriented languages is the concept of inheritance, which allows code defined in one class to be reused in other classes. In Java, you can define a general (more abstract) superclass, and then extend it with more specific subclasses. The superclass knows nothing of the classes that inherit from it, but all of the subclasses that inherit from the superclass must explicitly declare the inheritance relationship. A subclass that inherits from a superclass is automatically given accessible instance variables and methods defined by the superclass, but is also free to override superclass methods to define more specific behavior. For example, a Car superclass class could define general methods common to all automobiles, but a Ferrari subclass could override the accelerate() method.
- Interfejs
- Jest to narzedzie towarzyszace dziedziczeniu. Interfejs jest jak klasa abstrakcyjna (nadklasa) ktora definiujac metody zmusza podklase do nadpisania tych metod w podklasie, ale nie definiuja w jaki sposob maja byc obslugiwane. Innymi slowy np. Zwierze jest interfejsem i deklaruje ze wszystkie zwierzeta zawirac beda metode jesc(), ale interfejs Zwierze nie zawiera zadnej logiki dla metody jesc(). To znaczy ze zalezy od klasy ktora implementuje interfejs Zwierz, do zdefiniowania metody i logiki dla tej metody, czyli zachowan dla danego typu zwierzecia podczas jedzenia.
- Interfaces
- A powerful companion to inheritance is the use of interfaces. Interfaces are like a 100-percent abstract superclass that defines the methods a subclass must support, but not how they must be supported. In other words, an Animal interface might declare that all Animal implementation classes have an eat() method, but the Animal interface doesn't supply any logic for the eat() method. That means it's up to the classes that implement the Animal interface to define the actual code for how that particular Animal type behaves when its eat() method is invoked.
abstract | boolean | break | byte | case | catch | char |
class | const | continue | default | do | double | else |
extends | final | float | for | goto | if | implements |
import | instanceof | int | interface | long | native | new |
package | private | protected | public | return | short | static |
strictfp | super | swich | synchronized | this | throw | throws |
finally | transient | try | void | volatile | while | assert |
Słowa true, false
oraz null
takze sa zarezerwowane i nie jest możliwe tworzenie zmiennych, klas czy metod o takich nazwach (nie sa one slowami kluczowymi lecz literalami). Zapomniałem umieścić jeszcze jedno ze słów kluczowych, a mianowicie enum
- private - daje metodzie lub zmiennej dostep tylko z wnetrza klasy
- protected - daje metodzie lub zmiennej dostep tylko dla klas w tym samym pakiecie, lub klasie dziedziczacej
- public - daje klasie, metodzie lub zmiennej dostep z jakiej kolwiek innej klasy lub pakietu
- default - nie jest modyfikatorem dostepu (deklaracja zmiennej String str ma dostep default - domyslny) zmienna taka jest dostepna tylko w tym samym pakiecie
- // This is line 1.
- package Modifiers.A;
- /**
- *
- * @author adas
- */
- public class NadKlasa {
- protected String x = "protected";
- String str = "default";
- }
- package Modifiers.B;
- import Modifiers.A.NadKlasa;
- /**
- *
- * @author adas
- */
- public class Podklasa extends NadKlasa{
- public void metoda1() {
- System.out.println(x);
- }
- public void metoda2() {
- System.out.println(new NadKlasa().x);
- }
- public void metoda3() {
- System.out.println(str);
- }
- }
- package Modifiers;
- import Modifiers.B.Podklasa;
- /**
- *
- * @author adas
- */
- public class Check {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- new Podklasa().metoda1();
- new Podklasa().metoda2();
- new Podklasa().metoda3();
- }
- }
- metoda1() daje wynik:
- - protected
- metoda2() daje wyjatek:
- - Exception in thread "main" java.lang.RuntimeException:
- Uncompilable source code - x has protected access in
- Modifiers.A.NadKlasa
- metoda3() daje wyjatek:
- - Exception in thread "main" java.lang.RuntimeException:
- Uncompilable source code - str is not public in Modifiers.A.NadKlasa;
- cannot be accessed from outside package
Class, Method, and\or Variable Modifiers
(Modyfikatory klas, metod i\lub zmiennych)
- abstract - uzywany przy deklarowaniu klasy, ktorej nie mozna utworzyc instancji, lub metodach ktore musza byc zaimplementowane w nie abstrakcyjnych podklasach.
- class - slowo uzywane przy deklaracji klasy.
- extends - sluzy do wskazania nadklasy, ktorej podklasa jest rozszezeniem.
- final - sprawia, ze nie mozna dziedziczyc po klasie, dla metody sprawia ze nie mozna jej nadpisac, a dla zmiennej sprawia ze nie mozna zmienic jej wartosci.
- implements - sluzy do wskazania interfejsu, ktory bedzie zaimplementowany w danej klasie.
- interface - slowo uzywane do okreslenia interfejsu.
- native - wskazuje, ze metoda jest napisana w innym jezyku programowania, takich jak C
- new - uzywany przy tworzeniu instancji obiektu, wywoluje konstruktor
- static - sprawia ze metoda czy zmienna nalezy do klasy, anie nalezy do instancji, czyli mamy dostep do bez tworzenia obiektu.
- strictfp - może być użyty jedynie w odniesieniu do klas i metod (klasa nie musi być oznaczona przez strictfp, aby metoda wewnątrz tej klasy mogła zostać oznaczona takim modyfikatorem). strictfp tyczy się liczb zmiennoprzecinkowych, wymusza jednakowe ich traktowanie na różnych platformach systemowych (standard IEEE 754)
- synchronized - wskazuje ze metoda moze byc dostepna tylko dla jednego watku na raz
- transient - zmienna oznaczona jako transient jest pomijana w procesie serializacji obiektu (serializacja (dodana w Java 5) – zapis stanu obiektu np. do pliku, aby móc go w późniejszym czasie odtworzyć).
- volatile - wskazuje ze zmienna moze zmieniac sie synchronicznie poniewaz jest uzywana przez watki. Modyfikator volatile wymusza na wątku (thread) sprawdzanie zgodności jego wersji (kopii) zmiennej z oryginałem.
Brak komentarzy:
Prześlij komentarz