ผลต่างระหว่างรุ่นของ "Oop lab/example codes"
ไปยังการนำทาง
ไปยังการค้นหา
Jittat (คุย | มีส่วนร่วม) (หน้าที่ถูกสร้างด้วย '== class และ instance variables == <syntaxhighlight lang="java"> public class Dog { private int weight; private double speed; ...') |
Jittat (คุย | มีส่วนร่วม) |
||
(ไม่แสดง 4 รุ่นระหว่างกลางโดยผู้ใช้คนเดียวกัน) | |||
แถว 2: | แถว 2: | ||
<syntaxhighlight lang="java"> | <syntaxhighlight lang="java"> | ||
public class Dog { | 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 / getSpeed(); | |
− | + | } | |
− | + | ||
− | + | 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()); | |
− | + | } | |
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
แถว 47: | แถว 47: | ||
== static variables (class variables) และ static method == | == static variables (class variables) และ static method == | ||
<syntaxhighlight lang="java"> | <syntaxhighlight lang="java"> | ||
+ | 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; | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | <syntaxhighlight lang="java"> | ||
+ | public static void main(String[] args) { | ||
+ | Dog d = new Dog("Dang",15, 4.5); | ||
+ | |||
+ | // ... | ||
+ | |||
+ | Dog.setFoodPriceRate(0.25); | ||
+ | System.out.println("Current price rate is " + Dog.getFoodPriceRate()); | ||
+ | System.out.println(d.getName() + " has to pay " + d.getFoodPrice()); | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | == inheritance == | ||
+ | <syntaxhighlight lang="java"> | ||
+ | |||
+ | public class SuperDog extends Dog { | ||
+ | |||
+ | private boolean isUpgraded; | ||
+ | |||
+ | SuperDog(String n, int w, double speed) { | ||
+ | super(n, w, speed); | ||
+ | isUpgraded = false; | ||
+ | } | ||
+ | |||
+ | public void upgrade() { | ||
+ | isUpgraded = true; | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public double getSpeed() { | ||
+ | double s = super.getSpeed(); | ||
+ | if(isUpgraded) { | ||
+ | return s * 2; | ||
+ | } else { | ||
+ | return s; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | <syntaxhighlight lang="java"> | ||
+ | public class Main { | ||
+ | public static void testRun(Dog dog) { | ||
+ | System.out.println("It takes " + dog.getName() + | ||
+ | " " + dog.runDuration(100) + | ||
+ | " seconds."); | ||
+ | } | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | Dog d1 = new Dog("Dang",15, 4.5); | ||
+ | SuperDog d2 = new SuperDog("Dum",15,4.5); | ||
+ | Dog d3 = new SuperDog("Dang",15,4.5); | ||
+ | |||
+ | testRun(d1); | ||
+ | |||
+ | testRun(d2); | ||
+ | d2.upgrade(); | ||
+ | testRun(d2); | ||
+ | |||
+ | testRun(d3); | ||
+ | // d3.upgrade(); ---- can't call upgrade. Why?? | ||
+ | testRun(d3); | ||
+ | } | ||
+ | } | ||
</syntaxhighlight> | </syntaxhighlight> |
รุ่นแก้ไขปัจจุบันเมื่อ 07:32, 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 / getSpeed();
}
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;
}
}
public static void main(String[] args) {
Dog d = new Dog("Dang",15, 4.5);
// ...
Dog.setFoodPriceRate(0.25);
System.out.println("Current price rate is " + Dog.getFoodPriceRate());
System.out.println(d.getName() + " has to pay " + d.getFoodPrice());
}
inheritance
public class SuperDog extends Dog {
private boolean isUpgraded;
SuperDog(String n, int w, double speed) {
super(n, w, speed);
isUpgraded = false;
}
public void upgrade() {
isUpgraded = true;
}
@Override
public double getSpeed() {
double s = super.getSpeed();
if(isUpgraded) {
return s * 2;
} else {
return s;
}
}
}
public class Main {
public static void testRun(Dog dog) {
System.out.println("It takes " + dog.getName() +
" " + dog.runDuration(100) +
" seconds.");
}
public static void main(String[] args) {
Dog d1 = new Dog("Dang",15, 4.5);
SuperDog d2 = new SuperDog("Dum",15,4.5);
Dog d3 = new SuperDog("Dang",15,4.5);
testRun(d1);
testRun(d2);
d2.upgrade();
testRun(d2);
testRun(d3);
// d3.upgrade(); ---- can't call upgrade. Why??
testRun(d3);
}
}