เรียนเขียนโปรแกรมภาษา Java OOP ฉบับคนไม่เคยเขียนโปรแกรม

Why take this course?
ในส่วนที่ 1 ของการเรียนเขียนโปรแกรม Java,คุณจะพบให้ด้วยการใช้งานคำสั่งตัวเลือก (Switch Case), การใช้งานลูป (For Loop, While Loop, Do-While Loop), และคำสั่งการตกทางของโปรแกรม (Break และ Continue).นี่เป็นตัวอย่างหนึ่งในการเขียนโปรแกรมที่ใช้งานคำสั่งตัวเลือก (Switch Case) และการลูป (For Loop):
import java.util.Scanner;
public class SwitchCaseAndForLoopExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a day of the week (Monday, Tuesday, etc.):");
String inputDay = scanner.nextLine();
switch (inputDay) {
case "Monday":
System.out.println("Today is the start of the workweek.");
break;
case "Friday":
System.out.println("Hooray, it's Friday!");
break;
case "Saturday", "Sunday":
System.out.println("It's the weekend!");
break;
default:
System.out.println("Sorry, that's not a valid day of the week.");
break;
}
System.out.println("\nFibonacci sequence using For Loop:");
int first = 0, second = 1, nextTerm;
for (int i = 0; i < 10; i++) {
nextTerm = first + second;
System.out.print(first + " ");
first = second;
second = nextTerm;
}
scanner.close();
}
}
ในส่วนที่ 2 ของการเรียนเขียนโปรแกรม Java,คุณจะพบให้ด้วยการใช้งานคุณสมบัติดังนี้:
- ถ่ายทอด (Inheritance)
- สัมพันธ์ (Association, Aggregation, Composition)
- การใช้งานแบบทั่วโลก (Overloading) และการเข้าตัวแปร (Overriding)
ตัวอย่างโค้ดสำหรับการใช้งานคุณสมบัติถ่ายทอด:
class Animal {
public void makeSound() {
System.out.println("Some animal sound");
}
}
class Dog extends Animal { // การถ่ายทอด Dog จาก Animal
@Override
public void makeSound() {
System.out.println("Bark!");
}
}
public class InheritanceExample {
public static void main(String[] args) {
Dog dog = new Dog();
dog.makeSound(); // ในขณะนี้ dog จะเรียกใช้แบบที่หลายรูปของ makeSound() (Overriding)
}
}
ตัวอย่างโค้ดสำหรับการใช้งานสัมพันธ์ Aggregation:
class Car {
private Engine engine; // สัมพันธ์ Aggregation: Car เป็นเวลาของ Engine
public void startEngine() {
engine.start();
}
}
class Engine {
public void start() {
System.out.println("The engine is starting...");
}
}
public class AggregationExample {
public static void main(String[] args) {
Car car = new Car();
Engine engine = new Engine();
car.engine = engine; // การกำหนดเวลาของ Car ที่เป็นตัวแปร Engine
car.startEngine(); // เมื่อเริ่ม motor จะใช้งาน start() จาก Engine
}
}
สำหรับคุณสมบัตติอื่นๆ นี่เป็นตัวอย่างที่แข็งแกร่ง:
- การใช้งานแบบทั่วโลก (Overloading):
class Calculator {
public int add(int a, int b) {
return a + b;
}
// Overloaded method with different parameters
public double add(double a, double b) {
return a + b;
}
}
- การเข้าตัวแปร (Overriding):
class BaseCalculator {
public int add(int a, int b) {
return a + b;
}
}
class AdvancedCalculator extends BaseCalculator {
// Overridden method
@Override
public int add(int a, int b) {
System.out.println("Advanced calculator is adding the numbers.");
return super.add(a, b); // ใช้งานการเข้าตัวแปร (super) เพื่อเรียกใช้ method ที่ถ่ายทอดมาจาก class ตัวแปรแรก (BaseCalculator)
}
}
ในขณะที่เรากำหนด AdvancedCalculator
จะเห็นว่าเราสามารถเข้าถึง add
ที่ถ่ายทอดได้โดยการใช้ super.add(a, b)
, และเมื่อเราเรียกใช้ AdvancedCalculator
ในโค้ดอย่างใด จะเป็นการเรียกใช้ของ method add
ที่ได้ถูกเขียนใน class AdvancedCalculator
ทังหมด.
Course Gallery




Loading charts...