Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

4.1 Οι μέθοδοι στην Java

Μια μέθοδος είναι ένα τμήμα κώδικα που ορίζεται με ένα όνομα και μπορεί να καλεστεί από το πρόγραμμα και να εκτελεστεί όταν χρειαστεί. Η μέθοδος μπορεί να δέχεται παραμέτρους όταν καλείται και μπορεί να επιστρέφει μια τιμή ή να μην επιστρέφει τίποτα. Οι μέθοδοι χρησιμοποιούνται στην Java για να επαναχρησιμοποιούν κώδικα και να κάνουν το πρόγραμμα πιο δομημένο και ευανάγνωστο.

Μια μέθοδος πρέπει να δηλωθεί μέσα σε μια κλάση. Καθορίζεται με το όνομα της μεθόδου, ακολουθούμενο από παρενθέσεις (). Η Java παρέχει μερικές προκαθορισμένες μεθόδους, όπως η System.out.println (), αλλά μπορείτε επίσης να δημιουργήσετε τις δικές σας μεθόδους για να εκτελέσετε συγκεκριμένες ενέργειες:

Παράδειγμα
Δημιουργία μιας μεθόδου μέσα στην Main:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class Main {
static void myMethod() {
System.out.println("Hello World!");
}
public static void main(String[] args) {
myMethod();
}
}
public class Main { static void myMethod() { System.out.println("Hello World!"); } public static void main(String[] args) { myMethod(); } }
public class Main {
  static void myMethod() {
    System.out.println("Hello World!");
  }

  public static void main(String[] args) {
    myMethod();
  }
}

Στο παραπάνω παράδειγμα, η myMethod() είναι το όνομα της μεθόδου. Το static σημαίνει ότι η μέθοδος ανήκει στην κλάση Main και όχι σε ένα αντικείμενο της κλάσης Main. Θα μάθετε περισσότερα σχετικά με τα αντικείμενα και το πώς να έχετε πρόσβαση στις μεθόδους μέσω αντικειμένων αργότερα σε αυτό το μάθημα. Το void σημαίνει ότι αυτή η μέθοδος δεν έχει επιστρεφόμενη τιμή. Θα μάθετε περισσότερα σχετικά με τις τιμές επιστροφής αργότερα σε αυτό το μάθημα.

Για να καλέσετε μια μέθοδο στη Java, γράψτε το όνομα της μεθόδου, ακολουθούμενο από δύο παρενθέσεις () και ένα ερωτηματικό ερωτηματικό ερωτηματικό.

Στο παρακάτω παράδειγμα, η μέθοδος myMethod() χρησιμοποιείται για να εκτυπώσει ένα κείμενο (την ενέργεια), όταν καλείται:

Παράδειγμα:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class Main {
static void myMethod() {
System.out.println("Hello World!");
}
public static void main(String[] args) {
myMethod();
}
}
public class Main { static void myMethod() { System.out.println("Hello World!"); } public static void main(String[] args) { myMethod(); } }
public class Main {
  static void myMethod() {
    System.out.println("Hello World!");
  }

  public static void main(String[] args) {
    myMethod();
  }
}

Αυτό το πρόγραμμα θα καλέσει τη μέθοδο myMethod() και θα εκτυπώσει το κείμενο “Hello World!” στην έξοδο του προγράμματος.

Μια μέθοδος μπορεί επίσης να καλείται πολλές φορές:

Παράδειγμα:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class Main {
static void myMethod() {
System.out.println("Hello World!");
}
public static void main(String[] args) {
myMethod();
myMethod();
myMethod();
}
}
public class Main { static void myMethod() { System.out.println("Hello World!"); } public static void main(String[] args) { myMethod(); myMethod(); myMethod(); } }
public class Main {
  static void myMethod() {
    System.out.println("Hello World!");
  }

  public static void main(String[] args) {
    myMethod();
    myMethod();
    myMethod();
  }
}

Αυτό το πρόγραμμα θα καλέσει τη μέθοδο myMethod() τρεις φορές και θα εκτυπώσει το κείμενο “Hello World!” τρεις φορές στην έξοδο του προγράμματος.

Δωρεα μεσω Paypal

Για την κάλυψη αναγκών φιλοξενίας και δημιουργίας περιεχομένου.

κατηγοριες μαθηματων

Ιστορικο ενοτητων

top
error: Content is protected !!
Μετάβαση σε γραμμή εργαλείων