class Animal {
// property - vždy nad metodama
type: string = "animal"; // určený datového typu string
color: "black" | "white" | "green" | string = "black"; // určení bary a datovéjho typu skrze datový typ enum a nebo libovolný string
gender?: "male" | "female"; // ? = optional, může být definované, ale také nemusí
weight!: number; // ! = striktní řečení že bude definováno (pouze pro kompilátor)
// methods - vždy pod property
// metoda move(...) s argumentem direction, který je přípustný pouze pro hodnoty v enum (výčtovém datovém typu))
move(direction: 'left'|'right'|'back'|'forward') {
for(let i = 0; i < 10; i++) { // klasický cyklus
console.log("Pohyb: " + direction); // klasický výpis do konzole
}
}
}
// klasická práce v kódu
let animal1 = new Animal(); // vytvoření instance
animal1.move("left"); // volání metody z třídy (objektu) animal1 (Animal)
Dědičnost
class Animal {
type: string = "";
color: "black"|"white"|"green"|string = "black";
gender?: "male"|"female";
weight!: number;
private aura: number = 6;
move(direction: 'left'|'right'|'back'|'forward') {
for(let i = 0; i < 3; i++) {
if(this.aura === 0){
console.log("Došla ti aura");
break;
}
console.log("Pohyb: " + direction);
this.aura--;
}
}
}
class Dog extends Animal{
color = "green";
weight = 200;
bark() {
console.log("haf haf");
}
}
class Cow extends Animal{
color = "white";
weight = 400;
}
let animal = new Animal();
let dog = new Dog();
let cow = new Cow();
console.log(animal.weight);
console.log(dog.weight);
console.log(cow.weight);
dog.bark();
/*let animal1 = new Animal();
animal1.move("right");
animal1.move("forward");
animal1.move("left");*/
/*let dog1 = new Dog();
dog1.move("right");
dog1.move("forward");
dog1.move("left");*/
/*let cow1 = new Cow();
cow1.move("right");
cow1.move("forward");
cow1.move("left")*/
Construktor
class Car {
public readonly weight: number = 400;
private unit = "kg";
constructor(){
}
drive() {
console.log("Jeduuu" + this.weight + this.unit);
}
protected startEngine(){
console.log("Staruti engine");
}
}
class Plane extends Car{
liftOf(){
this.startEngine();
this.drive();
}
};
let pp = new Plane();
pp