java:this_operator

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision Both sides next revision
java:this_operator [2015/01/21 17:56]
gthanos [Ο τελεστής super]
java:this_operator [2015/01/21 17:59]
gthanos [Ο τελεστής super]
Line 41: Line 41:
 ===== Ο τελεστής super ===== ===== Ο τελεστής super =====
  
-Σε αναλογία με τον τελεστή **this**, ο τελεστής **super** δείχνει στο αντικείμενο της γονικής κλάσης. Προϋπόθεση για την χρήση του τελεστή **super** είναι να υφίσταται γονική κλάση μέσω της κληρονομικότητας.+Σε αναλογία με τον τελεστή **this**, ο τελεστής **super** δείχνει στο αντικείμενο της γονικής κλάσης. Προϋπόθεση για την χρήση του τελεστή **super** είναι να υφίσταται γονική κλάση μέσω ​της διαδικασίας ​της κληρονομικότητας.  
 + 
 +Λαμβάνοντας κώδικα από το προηγούμενο παράδειγμα του ποδηλάτου έχουμε τα εξής: 
 + 
 +<code java> 
 +public class MountainBike extends Bicycle { 
 +  
 +    // the MountainBike subclass has 
 +    // one field 
 +    public int seatHeight;​ 
 +  
 +    // the MountainBike subclass has 
 +    // one constructor 
 +    public MountainBike(int startHeight,​ int startCadence,​ 
 +                        int startSpeed, int startGear) { 
 +        super(startCadence,​ startSpeed, startGear);​ 
 +        seatHeight = startHeight;​ 
 +    }    
 +  
 +    // the MountainBike subclass has 
 +    // one method 
 +    public void setHeight(int newValue) { 
 +        seatHeight = newValue; 
 +    }    
 +  
 +
 +</​code>​ 
 + 
 +H χρήση του τελεστή super χρησιμοποιείται για να προσπελάσει τον κατασκευαστή της γονικής κλάσης και να αρχικοποιήσει τις μεταβλητές που ανήκουν στην κλάση αυτή.
  
  
java/this_operator.txt · Last modified: 2017/02/16 11:24 by gthanos