Moving Car class Car private int position 0
- Slides: 19
Moving. Car class Car { private int position = 0; public void move(){ position += 1; } public void print. Position(){ System. out. println("Car at position "+position); } } class Moving. Car { public static void main(String args[]){ Car my. Car = new Car(); my. Car. move(); my. Car. print. Position(); } }
class Car { private int position = 0; public void move(){ position += 1; } Παράμετρος της μεθόδου public void move. Many. Steps(int steps) { int delta = 1; if (steps < 0){ Το πέρασμα των παραμέτρων steps = -steps; delta = -1; γίνεται κατά τιμή (pass by value) } for (int i = 0; i < steps; i ++){ position += increment ; System. out. println("Car at position "+position); } H παράμετρος λειτουργεί ως } public void print. Position(){ System. out. println("Car at position "+position); } } class Moving. Car 2 { public static void main(String args[]){ Car my. Car = new Car(); int steps = -10; my. Car. move. Many. Steps(steps); System. out. println("--: " + steps); } } τοπική μεταβλητή της συνάρτησης και χάνεται μετά την κλήση της μεθόδου. Η τιμή του ορίσματος δεν μεταβάλλεται Όρισμα της μεθόδου Τυπώνει --: -10
class Car { private int position = 0; public void move(){ position += 1; } public void move. Many. Steps(int steps) { int delta = 1; if (steps < 0){ steps = -steps; delta = -1; } for (int i = 0; i < steps; i ++){ position += increment ; print. Position(); } } public void print. Position(){ System. out. println("Car at position "+position); } } class Moving. Car 2 { public static void main(String args[]){ Car my. Car = new Car(); int steps = -10; my. Car. move. Many. Steps(steps); System. out. println("--: " + steps); } } Μπορούμε να κάνουμε την εκτύπωση καλώντας την print. Position()
class Car { private int position = 0; public void move(){ position += 1; } Μέθοδος με πολλές παραμέτρους public void move. Many. Steps(int steps, String direction) { for (int i = 0; i < steps; i ++){ if (direction. equals(“right”){ position ++ ; } if (direction. equals(“left”) { position -- ; } print. Position(); } } public void print. Position(){ System. out. println("Car at position "+position); } } class Moving. Car 3 { Τα ορίσματα θα πρέπει να συμφωνούν με τους τύπους των παραμέτρων στην αντίστοιχη θέση public static void main(String args[]){ Car my. Car = new Car(); my. Car. move. Many. Steps(10, “left”); } } Κλήση της μεθόδου
import java. util. Random; class Car { private int MAX_VALUE = 3; private int position = 0; private Random random. Generator = new Random(); private int compute. Random. Steps() { int random. Steps = random. Generator. next. Int(2*MAX_VALUE + 1) – MAX_VALUE ; return random. Steps; } public void move(){ int steps = compute. Random. Steps(); move. Many. Steps(steps ); } public void move. Many. Steps(int steps) { int delta = 1; if (steps < 0){ steps = -steps; delta = -1; } for (int i = 0; i < steps; i ++) { position += delta ; print. Position(); } } public void print. Position(){ System. out. println("Car at position "+position); } } class Moving. Car 4 { public static void main(String args[]){ Car my. Car = new Car(); my. Car. move(); } }
class Car { private int position = 0; public void set. Position(int p){ position = p; } public int get. Position(){ return position; } public void move(){ position ++ ; } } class Moving. Car 5 { public static void main(String args[]){ Car my. Car = new Car(); my. Car. set. Position(10); my. Car. move(); System. out. println(my. Car. get. Position()); } } Υπάρχουν περιπτώσεις που μπορεί να θέλουμε η συνάρτηση set να επιστρέφει boolean (true αν η ανάθεση έγινε επιτυχώς, false αλλιως)
class Car { private int position = 0; public void set. Position(int position){ this. position = position; } public int get. Position(){ return position; } public void move(){ position ++ ; } } class Moving. Car 5 { public static void main(String args[]){ Car my. Car = new Car(); my. Car. set. Position(10); my. Car. move(); System. out. println(my. Car. get. Position()); } } Το this. position αναφέρεται στο πεδίο του αντικειμένου. Το position αναφέρεται στην παράμετρο της συνάρτησης Η κρυφή παράμετρος this προσδιορίζει το αντικείμενο που κάλεσε την μέθοδο Έτσι μπορούμε να χρησιμοποιήσουμε το ίδιο όνομα μεταβλητής χωρίς να δημιουργείται σύγχυση
- Int sum(int a int n) int sum=0 i
- Interface calculator public int add class test
- Int max(int x int y)
- Public void drawsquare(int x, int y, int len)
- Public int divide(int a int b)
- Class person string name int age
- Second position
- Qq moves
- Interface myinterface int foo(int x)
- Int main(int argc, char** argv)
- #include stdio.h void main()
- Int factorial(int n)
- Constant int arduino
- Int main int num 4
- Voidswap
- Void f(int i) int j=0
- Int max
- Char argv
- A { private int x; public a( val) { x = val; } }
- Private int