වස්තු-පාදක ක්රමලේඛනය (Classes & Objects)
Object-Oriented Programming (OOP) යනු සැබෑ ලෝකයේ ඇති "වස්තු" (objects) සංකල්පය මත පදනම් වූ ක්රමලේඛන ක්රමවේදයකි. මෙහිදී, අපි දත්ත (attributes) සහ එම දත්ත මත ක්රියා කරන ශ්රිත (methods) එකට එකතු කර "වස්තු" නිර්මාණය කරමු.
Java හි class එකක් නිර්මාණය කිරීමට class යන keyword එක භාවිතා කරයි.
public class MyClass {
// Attributes (fields) and methods go here
}
// We define a class named Car
public class Car {
// An attribute of the Car class
String color = "red";
}`Car` class එකෙන් `myCar` නමින් object එකක් සාදා එහි attribute එකක් පෙන්වමු.
public class Example2 {
public static void main(String[] args) {
Car myCar = new Car(); // Create an object of the Car class
System.out.println("The car color is: " + myCar.color);
}
}
// Assume the Car class from Example 1 is in a file named Car.javaThe car color is: redpublic class Student {
String firstName = "Nimal";
String lastName = "Silva";
int age = 15;
}
// In another file:
public class Example3 {
public static void main(String[] args) {
Student myStudent = new Student();
System.out.println("Name: " + myStudent.firstName + " " + myStudent.lastName);
}
}Name: Nimal Silvapublic class Example4 {
public static void main(String[] args) {
Car myCar = new Car();
myCar.color = "blue"; // Change the attribute value
System.out.println("The new car color is: " + myCar.color);
}
}The new car color is: bluepublic class Dog {
// Method inside the Dog class
public void bark() {
System.out.println("Woof! Woof!");
}
}
// In another file:
public class Example5 {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.bark(); // Calling the method
}
}Woof! Woof!Constructor යනු object එකක් සාදන විට ස්වයංක්රීයව ක්රියාත්මක වන විශේෂ method එකකි. එය class එකේ නමට සමාන වේ.
public class Person {
String name;
// Constructor
public Person(String personName) {
name = personName; // Set the initial value for the name attribute
}
public static void main(String[] args) {
Person myPerson = new Person("Kamal");
System.out.println("Person's name is: " + myPerson.name);
}
}Person's name is: Kamalpublic class Circle {
double radius;
public Circle(double r) {
radius = r;
}
public double getArea() {
return 3.14 * radius * radius;
}
public static void main(String[] args) {
Circle myCircle = new Circle(5.0);
System.out.println("Area of the circle is: " + myCircle.getArea());
}
}Area of the circle is: 78.5එකම class එකෙන් විවිධ attributes සහිත objects කිහිපයක් සෑදිය හැක.
public class Example8 {
public static void main(String[] args) {
// Assume Person class from example 6 exists
Person person1 = new Person("Amal");
Person person2 = new Person("Bimal");
System.out.println(person1.name);
System.out.println(person2.name);
}
}AmalBimalConstructor එකේ parameter නම සහ attribute නම සමාන වන විට, ඒවා වෙන් කර හඳුනා ගැනීමට `this` භාවිතා කරයි.
public class Employee {
String name;
public Employee(String name) {
// this.name refers to the class attribute
// name refers to the parameter
this.name = name;
}
}public class BankAccount {
double balance;
public BankAccount(double initialBalance) {
this.balance = initialBalance;
}
public void deposit(double amount) {
this.balance += amount;
}
public double getBalance() {
return this.balance;
}
public static void main(String[] args) {
BankAccount myAcc = new BankAccount(10000.0);
myAcc.deposit(5000.0);
System.out.println("Current balance is: " + myAcc.getBalance());
}
}Current balance is: 15000.0