ผลต่างระหว่างรุ่นของ "Oop lab/example codes"

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
แถว 2: แถว 2:
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
 
public class Dog {
 
public class Dog {
private int weight;
+
    private int weight;
private double speed;
+
    private double speed;
private String name;
+
    private String name;
+
   
Dog(String n, int w, double speed) {
+
    Dog(String n, int w, double speed) {
name = n;
+
        name = n;
weight = w;
+
        weight = w;
this.speed = speed;
+
        this.speed = speed;
}
+
    }
  
public void sayHello() {
+
    public void sayHello() {
System.out.println("Hello, my name is " + name);
+
        System.out.println("Hello, my name is " + name);
}
+
    }
+
   
public String getName() {
+
    public String getName() {
return name;
+
        return name;
}
+
    }
+
   
public double getSpeed() {
+
    public double getSpeed() {
return speed;
+
        return speed;
}
+
    }  
+
   
public double runDuration(double distance) {
+
    public double runDuration(double distance) {
return distance / speed;
+
        return distance / speed;
}
+
    }
+
   
public double weightOnMoon() {
+
    public double weightOnMoon() {
return ((double)(weight)) / 6.0;
+
        return ((double)(weight)) / 6.0;
}
+
    }
  
public static void main(String[] args) {
+
    public static void main(String[] args) {
Dog d = new Dog("Dang",15, 4.5);
+
        Dog d = new Dog("Dang",15, 4.5);
+
       
d.sayHello();
+
        d.sayHello();
System.out.println("It takes " + d.getName() +  
+
        System.out.println("It takes " + d.getName() +  
" " + d.runDuration(100) +  
+
                " " + d.runDuration(100) +  
" seconds.");
+
                " seconds.");
System.out.println("In the moon, " + d.getName() +  
+
        System.out.println("In the moon, " + d.getName() +  
" weights " + d.weightOnMoon());
+
                " weights " + d.weightOnMoon());
}
+
    }
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
แถว 48: แถว 48:
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
 
public class Dog {
 
public class Dog {
private int weight;
+
    private int weight;
private double speed;
+
    private double speed;
private String name;
+
    private String name;
  
// ....
+
    // ....
  
static double foodPriceRate;
+
    static double foodPriceRate;
+
   
public static double getFoodPriceRate() { return foodPriceRate; }
+
    public static double getFoodPriceRate() { return foodPriceRate; }
public static void setFoodPriceRate(double rate) { foodPriceRate = rate; }
+
    public static void setFoodPriceRate(double rate) { foodPriceRate = rate; }
  
public double getFoodPrice() {
+
    public double getFoodPrice() {
return weight * foodPriceRate;
+
        return weight * foodPriceRate;
}
+
    }
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

รุ่นแก้ไขเมื่อ 02:13, 10 พฤศจิกายน 2560

class และ instance variables

public class Dog {
    private int weight;
    private double speed;
    private String name;
    
    Dog(String n, int w, double speed) {
        name = n;
        weight = w;
        this.speed = speed;
    }

    public void sayHello() {
        System.out.println("Hello, my name is " + name);
    }
    
    public String getName() {
        return name;
    }
    
    public double getSpeed() {
        return speed;
    }    
    
    public double runDuration(double distance) {
        return distance / speed;
    }
    
    public double weightOnMoon() {
        return ((double)(weight)) / 6.0;
    }

    public static void main(String[] args) {
        Dog d = new Dog("Dang",15, 4.5);
        
        d.sayHello();
        System.out.println("It takes " + d.getName() + 
                " " + d.runDuration(100) + 
                " seconds.");
        System.out.println("In the moon, " + d.getName() + 
                " weights " + d.weightOnMoon());
    }
}

static variables (class variables) และ static method

public class Dog {
    private int weight;
    private double speed;
    private String name;

    // ....

    static double foodPriceRate;
    
    public static double getFoodPriceRate() { return foodPriceRate; }
    public static void setFoodPriceRate(double rate) { foodPriceRate = rate; }

    public double getFoodPrice() {
        return weight * foodPriceRate;
    }
}