Classes

It is used to create new objects, classify objects and functions, and ensure code reusability. If you didn't create class structure in A file you created, it is automatically created in "public" access type.

Class Creation: <accessType> class <ClassName>{<codes>}

Create Object: <accessType> ClassName objectName=new ClassName();

Example:

Let's make a representative user management panel. Let's create AdminPanel.a file.

AdminPanel.a

class AdminPanel{

func _add(Text userName){

print("User Added: {userName}");

}


func _remove(Text userName){

print("User Removed: {userName}");

}

func _showList(){

print("User List");

}

}

MainClass.a

mainfunc(){

AdminPanel admin=new AdminPanel();

//We have created "AdminPanel" object named "admin".

admin._add("HAK");

admin._remove("HAK");

admin._showList();

//We used functions of admin object.

}

Output:

User Added: HAK

User Removed: HAK

User List

Access Types

There are 4 types of access. Classes use "public" or "default".

public

Accessible by all classes.

Example:

public class Example{

public Text message="Hello";

}

protected

It is accessible in class, folder in which it was created, and in classes that inherit class in which it was created.

Example:

protected Number=10.5;

default

It is accessible in class and folder in which it was created.

Example:

class Example{

Text message="Hello";

}

private

It can only be accessed in class in which it was created.

Example:

private Number=10.5;

Static and Const

We saw expression "static" in functions section. "static" objects and functions occupy only one memory space. That is, class it is in is used without recreating it. When a "static" object is valued, value remains unchanged until program is finished.

"const" objects and functions are written once. So it cannot be changed later.

Example:

Let's make a message class. Let's create Message.a file.

Message.a

public static const Text constMessage="GOOD DAY";

public static Text message="WELCOME";

public static func showMessage() print(message);

MainClass.a

mainfunc(){

Message.showMessage();

Message.message="NEW MESSAGE";

Message.showMessage();

print(Message.constMessage);

Message.constMessage="GOOD MORNING"; //Error

}

Output:

WELCOME

NEW MESSAGE

GOOD DAY

Init

It is first function that runs when object is created from class.

Example:

Let's make a student class. Let's create Student.a file.

Student.a

public Text name;

public Text surname;

public Int _class;

public Text branch;

public Int number;

init(Text name, Text surname, Int _class, Text branch, Int number){

this.name=name;

this.surname=surname;

this._class=_class;

this.branch=branch;

this.number=number;

//"this" indicates that it is an object of class.

//We used "this" because objects of init and class are same.

}

MainClass.a

mainfunc(){

Student michael=new Student("Michael","AB",10,"B"t,600);

Student raphel=new Student("Raphel","BE",12,"D"t,300);


print("{michael._class}/{ahmet.branch}");

print("{raphel._class}/{mehmet.branch}");

}

Output:

10/B

12/D

Package

Packages are created when we divide our classes into folders. Packages are used in 2 ways. To use "use" statement, class structure must be created.

Example:

Let's create a folder named "Math" and create "FourOperation.a" file in it.

FourOperation.a

Number num1;

Number num2;

init(Number num1, Number num2){

this.num1=num1;

this.num2=num2;

}

public func sum() print(num1+num2);

public func difference() print(num1-num2);

public func multiplication() print(num1*num2);

public func division() print(num1/num2);

We created a package named "Math". Let's use our package in 2 ways.

  1. Usage: FolderName.FolderName.ClassName

MainClass.a

mainfunc(){

Math.FourOperation operation=new Math.FourOperation(10,5);

operation.sum();

operation.difference();

operation.multiplication();

operation.division();

}

  1. Usage: use FolderName.FolderName.*; | ClassName

MainClass.a

use Math.*;

class MainClass{

mainfunc(){

FourOperation operation=new FourOperation(10,5);

operation.sum();

operation.difference();

operation.multiplication();

operation.division();

}

}

Output:

15.0

5.0

50.0

2.0

Inherit

To use objects and functions written in one class without recreating them in another class, they are inherited from first class. Class structure should be created. "maininit" function runs "init" function of inherited class. "over" statement overrides specified function of inherited class and replaces it with specified function.

Usage: class Class2 inherited Class1{<codes>}

Example:

Let's create Product, Computer and Dress classes. Let Computer and Dress classes inherit Product class. We overwrite "information" function with a new information function, since there is a "color" object in addition to Product class in Dress class.

Product.a

public Text name;

public Number price;

init(Text name, Number price){

this.name=name;

this.price=price;

}

public func info() print("Name: {name}, Price: {price}");

Computer.a

class Computer inherit Product{

public Text type="Computer";

init(Text name, Number price) maininit(name, price);

}

Dress.a

class Dress inherit Product{

public Text type="Dress";

Text color;

init(Text name, Text color, Number price){

maininit(name, price);

this.color=color;

}


over func info()

print("Name: {name}, Color: {color}, Price: {price}");

}

MainClass.a

mainfunc(){

Computer computer=new Computer("Laptop", 3000);

Dress dress=new Dress("Sweater", "Blue", 100);

print(computer.type);

computer.info();

print(dress.type);

dress.info();

}

Output:

Computer

Name: Laptop, Price: 3000.0

Dress

Name: Sweater, Color: Blue, Price: 100.0