• Classes

    A class in programming is like a group of people. For example, if you have a group of friends, each person in the group is called a "friend". In programming, we call each person in the group a "class". Just like your friends might all have different names and personalities, classes can also have different names and qualities. So when you write code, you can create different classes with different names and abilities to do things.

      • Object Classes

      • class Person {
        	constructor(name, age) {
        		this.name = name;
        		this.age = age;
        	}
        
        	greet() {
        		console.log('Hello my name is ' + this.name);
        	}
        }
        
        var human = new Person('John', 36);
        
        console.log(human.name); // John
        console.log(human.age); // 36
        human.greet(); // Hello my name is John
      • class Person {
          String name;
          int age;
        
          Person(this.name, this.age);
        
          void greet() {
        	print('Hello my name is ' + this.name);
          }
        }
        
        void main() {
          var human = new Person('John', 36);
        
          print(human.name); // John
          print(human.age); // 36
          human.greet(); // Hello my name is John
        }
        
      • import std.stdio : write, writeln;
        import std.string;
        
        class Person {
        	string name;
        	int age;
        
        	this(string name, int age) {
        		this.name = name;
        		this.age = age;
        	}
        
        	void greet() {
        		write("Hello my name is ", this.name);
        	}
        }
        
        void main()
        {
        	Person human = new Person("John", 36);
        
        	writeln(human.name); // John
        	writeln(human.age); // 36
        	human.greet(); // Hello my name is John
        }
      • <?php
        class Person {
        	public $name, $age;
        
        	function __construct($name, $age) {
        		$this->name = $name;
        		$this->age = $age;
        	}
        
        	public function greet() {
        		print('Hello my name is ' . $this->name);
        	}
        }
        
        $human = new Person('John', 36);
        
        print($human->name); // John
        print($human->age); // 36
        $human->greet(); // Hello my name is John
      • class Person:
        	def __init__(self, name, age):
        		self.name = name
        		self.age = age
        
        	def greet(self):
        		print('Hello my name is ' + self.name)
        
        
        human = Person('John', 36)
        
        print(human.name)  # John
        print(human.age)  # 36
        human.greet()  # Hello my name is John
        
      • package main
        
        import "fmt"
        
        type Person struct {
        	name string
        	age  int
        }
        
        func (p Person) greet() {
        	fmt.Println("Hello my name is " + p.name)
        }
        
        func main() {
        	var human = Person{"John", 36}
        
        	fmt.Println(human.name) // John
        	fmt.Println(human.age)  // 36
        	human.greet()		   // Hello my name is John
        }
        
      • Trait Classes

      • const HasFlippers = {
        	swim: function () {
        		return 'can swim';
        	}
        };
        
        const HasFeathers = {
        	fly: function () {
        		return 'can fly';
        	}
        };
        
        class Duck { }
        
        Object.assign(Duck.prototype, HasFlippers, HasFeathers);
        
        class Seal { }
        
        Object.assign(Seal.prototype, HasFlippers);
        
        var a = new Duck();
        console.log(a.swim()); // can swim
        console.log(a.fly()); // can fly
        
        var b = new Seal();
        console.log(b.swim()); // can swim
      • mixin HasFlippers {
          String swim() {
        	return 'can swim';
          }
        }
        
        mixin HasFeathers {
          String fly() {
        	return 'can fly';
          }
        }
        
        class Duck with HasFlippers, HasFeathers {}
        
        class Seal with HasFlippers {}
        
        void main() {
          var a = new Duck();
          print(a.swim()); // can swim
          print(a.fly()); // can fly
        
          var b = new Seal();
          print(b.swim()); // can swim
        }
        
      • import std.stdio : writeln;
        import std.string;
        
        mixin template HasFlippers() {
        	string swim() {
        		return "can swim";
        	}
        }
        
        mixin template HasFeathers() {
        	string fly() {
        		return "can fly";
        	}
        }
        
        class Duck {
        	mixin HasFlippers;
        	mixin HasFeathers;
        }
        
        class Seal {
        	mixin HasFlippers;
        }
        
        void main() {
        	Duck a = new Duck();
        	writeln(a.swim()); // can swim
        	writeln(a.fly()); // can fly
        
        	Seal b = new Seal();
        	writeln(b.swim()); // can swim
        }
      • <?php
        trait HasFlippers
        {
        	public function swim()
        	{
        		return 'can swim';
        	}
        }
        
        trait HasFeathers
        {
        	public function fly()
        	{
        		return 'can fly';
        	}
        }
        
        class Duck
        {
        	use HasFlippers, HasFeathers;
        }
        
        class Seal
        {
        	use HasFlippers;
        }
        
        $a = new Duck();
        print($a->swim()); // can swim 
        print($a->fly()); // can fly 
        
        $b = new Seal();
        print($b->swim()); // can swim
      • import math
        
        
        class HasFlippers(object):
        	def swim(self):
        		return 'can swim'
        
        
        class HasFeathers(object):
        	def fly(self):
        		return 'can fly'
        
        
        class Duck(HasFlippers, HasFeathers):
        	pass
        
        
        class Seal(HasFlippers):
        	pass
        
        
        a = Duck()
        print(a.swim())
        print(a.fly())
        
        b = Seal()
        print(b.swim())
        
      • package main
        
        import "fmt"
        
        type HasFlippers string
        
        type HasFeathers string
        
        func (h HasFlippers) swim() string {
        	return "can swim"
        }
        
        func (h HasFeathers) fly() string {
        	return "can fly"
        }
        
        type Duck struct {
        	HasFlippers
        	HasFeathers
        }
        
        type Seal struct {
        	HasFlippers
        }
        
        func main() {
        	var a Duck
        	fmt.Println(a.swim())
        	fmt.Println(a.fly())
        
        	var b Seal
        	fmt.Println(b.swim())
        }
        
      • Interface Classes

      • // NA
      • import "dart:math" show pi;
        
        abstract class Shape {
          double area();
        }
        
        class Rectangle implements Shape {
          double width;
          double height;
        
          Rectangle(this.width, this.height);
        
          double area(){
        	return this.width * this.height;
          }
        }
        
        class Circle implements Shape {
          double radius;
        
          Circle(this.radius);
        
          double area(){
        	return pi * this.radius * this.radius;
          }
        }
        
        void main() {
          Rectangle a = new Rectangle(3, 5);
          Circle b = new Circle(4);
        
          print("area of a is ${a.area().toStringAsPrecision(8)}");  // area of a is 15.000000
          print("area of b is ${b.area().toStringAsPrecision(8)}");  // area of b is 50.265482
        }
      • import std.stdio : writefln;
        import std.math.constants;
        
        interface Shape {
        	double area();
        }
        
        class Rectangle : Shape {
        	double width;
        	double height;
        
        	this(double width, double height) {
        		this.width = width;
        		this.height = height;
        	}
        
        	double area() {
        		return this.width * this.height;
        	}
        }
        
        class Circle : Shape {
        	double radius;
        
        	this(double radius) {
        		this.radius = radius;
        	}
        
        	double area() {
        		return PI * this.radius * this.radius;
        	}
        }
        
        void main()
        {
        	Rectangle a = new Rectangle(3, 5);
        	Circle b = new Circle(4);
        
        	writefln("area of a is %f", a.area());  // area of a is 15.000000
        	writefln("area of b is %f", b.area());  // area of b is 50.265482
        }
      • <?php
        interface Shape {
        	public function area();
        }
        
        class Rectangle implements Shape {
        	public $width, $height;
        
        	function __construct($width, $height){
        		$this->width = $width;
        		$this->height = $height;
        	}
        
        	public function area()
        	{
        		return $this->width * $this->height;
        	}
        }
        
        class Circle implements Shape {
        	public $radius;
        
        	function __construct($radius){
        		$this->radius = $radius;
        	}
        
        	public function area()
        	{
        		return pi() * $this->radius * $this->radius;
        	}
        }
        
        $a = new Rectangle(3, 5);
        $b = new Circle(4);
        
        printf('area of a is %f', $a->area()); // area of a is 15.000000
        printf('area of b is %f', $b->area()); // area of b is 50.265482
      • import math
        
        
        class Shape:
        	def area():
        		pass
        
        
        class Rectangle(Shape):
        	def __init__(self, width, height):
        		self.width = width
        		self.height = height
        
        	def area(self):
        		return self.width * self.height
        
        
        class Circle(Shape):
        	def __init__(self, radius):
        		self.radius = radius
        
        	def area(self):
        		return math.pi * self.radius * self.radius
        
        
        a = Rectangle(width=3, height=5)
        b = Circle(radius=4)
        
        print('area of a is {:f}'.format(a.area()))  # area of a is 15.000000
        print('area of b is {:f}'.format(b.area()))  # area of b is 50.265482
        
      • package main
        
        import (
        	"fmt"
        	"math"
        )
        
        type Shape interface {
        	Area() float64
        }
        
        type Rectangle struct {
        	width  float64
        	height float64
        }
        
        type Circle struct {
        	radius float64
        }
        
        func (r Rectangle) Area() float64 {
        	return r.width * r.height
        }
        
        func (c Circle) Area() float64 {
        	return math.Pi * c.radius * c.radius
        }
        
        func main() {
        	var a, b Shape
        	
        	a = Rectangle{width: 3, height: 5}
        	b = Circle{radius: 4}
        
        	fmt.Printf("area of a is %f", a.Area()) // area of a is 15.000000
        	fmt.Printf("area of b is %f", b.Area()) // area of b is 50.265482
        }
        
      • Struct Classes

      • class Person {
        	constructor(name, age) {
        		this.name = name;
        		this.age = age;
        	}
        }
        
        const human = new Person('John', 36);
        
        console.log(human.name); // John
        console.log(human.age); // 36
      • class Person {
          String name;
          int age;
        
          Person(this.name, this.age);
        }
        
        void main() {
          var human = new Person('John', 36);
        
          print(human.name); // John
          print(human.age); // 36
        }
      • import std.stdio : writeln;
        import std.string;
        
        struct Person {
        	string name;
        	int age;
        
        	this(string name, int age) {
        		this.name = name;
        		this.age = age;
        	}
        }
        
        void main()
        {
        	Person human = Person("John", 36);
        
        	writeln(human.name); // John
        	writeln(human.age); // 36
        }
      • <?php
        class Person
        {
        	public string $name;
        	public int $age;
        
        	function __construct(string $name, int $age)
        	{
        		$this->name = $name;
        		$this->age = $age;
        	}
        }
        
        $human = new Person('John', 36);
        
        print($human->name); // John
        print($human->age); // 36
      • from typing import NamedTuple
        
        class Person(NamedTuple):
        	name: str
        	age: int
        
        
        human = Person(name='John', age=36)
        
        print(human.name)  # John
        print(human.age)  # 36
        
      • package main
        
        import "fmt"
        
        type Person struct {
        	name string
        	age  int
        }
        
        func main() {
        	var human = Person{"John", 36}
        
        	fmt.Println(human.name) // John
        	fmt.Println(human.age)  // 36
        }
        
      • Enum Classes

      • const Days = {
        	Sunday: 0,
        	Monday: 1,
        	Tuesday: 2,
        	Wednesday: 3,
        	Thursday: 4,
        	Friday: 5,
        	Saturday: 6
        };
        
        var day = 4;
        
        if (day == Days.Saturday || day == Days.Sunday) {
        	console.log('a weekend');
        } else {
        	console.log('a weekday'); // a weekday
        }
      • enum Days {
        	sunday,
        	monday,
        	tuesday,
        	wednesday,
        	thursday,
        	friday,
        	saturday
        }
        
        void main() {
        	var day = 4;
        
        	if (day == Days.saturday.index || day == Days.sunday.index) {
        	print('a weekend');
        	} else {
        	print('a weekday'); // a weekday
        	}
        }
        
      • import std.stdio : write;
        
        enum Days {
        	Sunday,
        	Monday,
        	Tuesday,
        	Wednesday,
        	Thursday,
        	Friday,
        	Saturday
        }
        
        void main()
        {
        	int day = 4;
        
        	if (day == Days.Saturday || day == Days.Sunday) {
        		write("a weekend");
        	} else {
        		write("a weekday"); // a weekday
        	}
        }
        
      • <?php
        enum Days: int
        {
        	case Sunday = 0;
        	case Monday = 1;
        	case Tuesday = 2;
        	case Wednesday = 3;
        	case Thursday = 4;
        	case Friday = 5;
        	case Saturday = 6;
        }
        
        $day = 4;
        
        if ($day == Days::Saturday->value || $day == Days::Sunday->value) {
        	print('a weekend');
        } else {
        	print('a weekday'); // a weekday
        }
        
      • from enum import Enum
        
        class Days:
        	Sunday = 0
        	Monday = 1
        	Tuesday = 2
        	Wednesday = 3
        	Thursday = 4
        	Friday = 5
        	Saturday = 6
        
        day = 4;
        
        if day == Days.Saturday or day == Days.Sunday :
        	print('a weekend');
        else:
        	print('a weekday'); # a weekday
        
      • package main
        
        import "fmt"
        
        const (
        	Sunday	int = 0
        	Monday	int = 1
        	Tuesday   int = 2
        	Wednesday int = 3
        	Thursday  int = 4
        	Friday	int = 5
        	Saturday  int = 6
        )
        
        func main() {
        	var day int = 4
        
        	if day == Saturday || day == Sunday {
        		fmt.Println("a weekend")
        	} else {
        		fmt.Println("a weekday") // a weekday
        	}
        }