ืืื ืืฉืชื ืื ืกืืืืื?
ืืฉืชื ืื ืกืืืืื ืื ืืฉืชื ื ืืืืงื ืืฉืืชืคืื ืืื ืื ืืืืคืขืื ืฉื ืืืืงื ืื ืืชื ืืืฉืช ืืืืื ืืืฉื ืืช ืืืชื ืืืื ืืืฆืืจ ืืืคืข ืฉื ืืืืืงื. ืืืื ื ืฆืืื ืืขืืืง ืืื ืืืืื ืืช ืืฉืืืืฉืื ืืืืืฉืื ืฉื ืืฉืชื ืื ืกืืืืื ื-Java.ืืืืื ืืืฉืชื ื ืกืืื ื-Java
ืืื ืืฉืืืืฉืื ืื ืคืืฆืื ืืืืชืจ ืืืฉืชื ืื ืกืืืืื ืืื ืฉืืชืืฃ ืืื ืื ืืืืคืขืื ืฉื ืืืืงื. ืืืืืื, ืืฉืชื ื ืืื ื ืฉืขืืงื ืืืจ ืืกืคืจ ืืืืคืขืื ืฉื ืืฆืจื, ืื ืืฉืชื ื CONSTANT ืฉืืืืืง ืขืจื ืฉืืื ื ืืฉืชื ื ืืขืืื. ืฉืงืื ืืืชื ืืืืืฆืืช ืืฉืืื ืื ืง. ืงืืข ืืงืื ืืื ืืจืื ืืืฆื ื ืืชื ืืืฉืชืืฉ ืืืฉืชื ื ืกืืื nextAccountNumber ืืื ืืืงืฆืืช ืืกืคืจื ืืฉืืื ืืืืืืืื ืืื ืืฉืืื ืืืฉ:ืืืืื
// BankAccount class definition
class BankAccount {
// Declare static variable nextAccountNumber
static int nextAccountNumber = 1001;
// Declare instance variables for accountNumber and balance.
int accountNumber;
double balance;
// BankAccount constructor that assigns unique account number to each instance
BankAccount() {
// Assign the current value of nextAccountNumber to accountNumber.
accountNumber = nextAccountNumber;
// Increment nextAccountNumber for the next BankAccount object.
nextAccountNumber++;
}
// Method to get the account number of this BankAccount object.
int getAccountNumber() {
return accountNumber;
}
// Method to get the balance of this BankAccount object.
double getBalance() {
return balance;
}
// Method to deposit an amount into this BankAccount object.
void deposit(double amount) {
// Increase balance by the amount deposited.
balance += amount;
}
// Method to withdraw an amount from this BankAccount object.
void withdraw(double amount) {
// Decrease balance by the amount withdrawn.
balance -= amount;
}
}
// Main method definition
class Main {
static void main(String[] args) {
// Create three BankAccount objects: account1, account2, and account3.
BankAccount account1 = new BankAccount();
BankAccount account2 = new BankAccount();
BankAccount account3 = new BankAccount();
// Display the value of the static variable nextAccountNumber after creating each BankAccount object.
System.out.println("Value of nextAccountNumber after creating account1: " + account1.getAccountNumber());
System.out.println("Value of nextAccountNumber after creating account2: " + account2.getAccountNumber());
System.out.println("Value of nextAccountNumber after creating account3: " + account3.getAccountNumber());
}
}
ืชึฐืคืึผืงึธื
ืขืจื NextAccountNumber ืืืืจ ืืฆืืจืช ืืฉืืื1: 1001 ืขืจื NextAccountNumber ืืืืจ ืืฆืืจืช ืืฉืืื2: 1002 ืขืจื NextAccountNumber ืืืืจ ืืฆืืจืช ืืฉืืื3: 1003
ืืืืืื ืื, ืืฉืืื ืืจืืฉืืช ืืืฆืจืช ืฉืืืฉื ืืืคืขืื ืฉื ืืืืืงื BankAccount . ืื ืื BankAccount ืืงืฆื ืืช ื- nextAccountNumber ืืืฉืืื ืืืืฉ ืืืืืื ืืช ืืืฉืชื ื nextAccountNumber ื-1. ืฉืืืช getAccountNumber ืืืืืจื ืืช ืืกืคืจ ืืืฉืืื ืฉื ืื ืืฉืืื. ืืืืจ ืืื, ืืฉืืื ืืจืืฉืืช ืืืคืืกื ืืช ืืกืคืจ ืืืฉืืื ืืืช ืืืฉืชื ื nextAccountNumber ืขืืืจ ืื ืืฉืืื. ืืชื ืืืื ืืจืืืช ืฉืื ืืกืคืจ ืืฉืืื ืืื ืืืืืื ืืืืฉืชื ื nextAccountNumber ืืื ืืืื ืขืืืจ ืื ืืฉืืื ืืืฉ ืฉื ืืฆืจ. ื ืืชื ืืืฉืชืืฉ ืืืฉืชื ืื ืกืืืืื ืื ืืื ืืืืฉื ืืช ืืคืืก ืืขืืฆืื ืฉื Singleton, ืฉืื ื ืืชื ืืืฆืืจ ืจืง ืืืคืข ืืื ืฉื ืืืืงื. ืืืืงื ืืืืื ืืื ืืืืงื ืฉืืืืื ืืืืืช ืื ืจืง ืืืคืข ืืื ืืื ืืืฉืื Java ืืื ืืื ืืช ืืกืคืงืช ื ืงืืืช ืืืฉื ืืืืืืืช ืืืืคืข ืื. ืงืืข ืืงืื ืืื ืืจืื ืืืฆื ื ืืชื ืืืฉืชืืฉ ืืืืคืข ืืฉืชื ื ืกืืื ืืื ืืืืฉื ืืืืงื ืืืืื:
ืืืืื
public class Singleton {
// using static variable in the singleton class
private static Singleton instance;
private Singleton() { }
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
public void displayMessage() {
System.out.println("This is a singleton class");
}
public static void main(String[] args) {
Singleton singleton1 = Singleton.getInstance();
singleton1.displayMessage();
Singleton singleton2 = Singleton.getInstance();
singleton2.displayMessage();
Singleton singleton3 = Singleton.getInstance();
singleton3.displayMessage();
}
}
ืชึฐืคืึผืงึธื
ืืื ืืืืงื ืืืืื ืืช ืื ืืืืงื ืืืืื ืืช ืื ืืืืงื ืืืืื ืืช
ืืืืืื ืื, ืืฉืืื ืืจืืฉืืช ืืืฆืจืช ืฉืืืฉื ืืืคืขืื ืฉื ืืืืืงื Singleton ืขื ืืื ืงืจืืื ืืืชืืื getInstance() . ืืฉืืื getInstance() ืืืืืจื ืืช ืืืืคืข ืฉื ืืืืืงื ืืืืฆืจืช ืืืคืข ืืืฉ ืื ืืื ื ืงืืื. ืฉืืืช displayMessage ืจืง ืืืคืืกื ืืืืขื . ืืืืจ ืืื ืืฉืืื ืืจืืฉืืช ืงืืจืืช ืืฉืืืช displayMessage ืขืืืจ ืื ืืืคืข. ืืคื ืฉืืชื ืืืื ืืจืืืช, ืืืจืืช ืฉืื ื ืืืฆืจืื ืฉืืืฉื ืืืคืขืื, ืจืง ืืืืืืงื ืืื ื ืืฆืจ ืืืฉืืชืฃ ืืื ืื ืืืืคืขืื. ืื ืืืืื ืฉืจืง ืืืคืข ืืื ืฉื ืืืืืงื ื ืืฆืจ ืืืฉืืชืฃ ืืื ืื ืืืืคืขืื ืืืืฉืชื ื ืืกืืื 'instance' ืืชืงืืื. ืืฉืื ืืืืืจ ืฉืืขืจื ืฉื ืืฉืชื ื ืกืืื ืืฉืืชืฃ ืืื ืืืืคืขืื ืฉื ืืืืงื, ืื ืฉืฉืื ืืืื ืืืฉืชื ื ืฉื ืขืฉื ืขื ืืื ืืืคืข ืืื ืืืื ืืืืืื ืืื ืฉืืจ ืืืืคืขืื. ืืฉ ืืฉืงืื ืืืื ืืฉืชื ืื ืกืืืืื ืืื ืืื ืืข ืืชื ืืฉืืืืช ืืืืืื ืคืืื ืฆืืืืืื ืืงืื ืฉืื. ืืืืืื, ืฉืืืืฉ ืืืฉืชื ื ืกืืื ืืื ืืืืืืง ืขืจื ืกืคืฆืืคื ืืืืคืข ืฉื ืืืืงื, ืืืื ืืชืจืช ืืฉืืื, ืืื ื ืืืืืฅ. ืืื ืืืืง ืืช ืื ืฉืืืืช, ืื ื ืืฆืืขืื ืื ืืฆืคืืช ืืฉืืขืืจ ืืืืื ืืงืืจืก Java ืฉืื ื
GO TO FULL VERSION