class Car { // Properties // Se crean las propiedades /*_make: string; _color: string; _doors: number;*/ private _make: string; private _color: string; private _doors: number; private static numberOfCars: number = 0; // New static property // ... // Constructor // Inicializamos nuestras propiedades constructor(make:string, color:string, doors = 4){ // a traves del this le decimos que vamos a acceder a nuestra propiedad a traves de una variable this._make = make; this._color = color; this._doors = doors; Car.numberOfCars++; // Increments the value of the static property //agregamos una validacion para cuando se pase un parametro a un objeto hijo y se puede validar directamente /*if ((doors % 2) === 0) { this._doors = doors; } else { throw new Error('Doors must be an even number'); }*/ } // Accessors // a traves de typescript podemos decir si queremos acceder desde la clase public a nuestras propiedadaes en este caso si se puede // hacer pero de esta manera le decimos que queremos acceder a traves de una funcion y que nos lo devuelva get make(){ return this._make; } get color(){ return 'El color del carro es: ' + this._color; } get doors(){ return this._doors; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // aca le decimos que setee o que coloque nuestro make en la variable make del objeto set make(make){ this._make = make; } set color(color){ this._color = color; } set doors(doors) { if ((doors % 2) === 0) { this._doors = doors; } else { throw new Error('Doors must be an even number'); } } // Methods // en los metodos de una funcion en typescript no usamos la palabra function en metodos // Methods accelerate(speed: number): string { return `${this.worker()} is accelerating to ${speed} MPH.` } brake(): string { return `${this.worker()} is braking with the standard braking system.` } turn(direction: 'left' | 'right'): string { return `${this.worker()} is turning ${direction}`; } // This function performs work for the other method functions /*worker(): string { return this._make; }*/ protected worker(): string { // <- cambiamos private por protected para que pueda ser accesible en sub clases de car return this._make; } public static getNumberOfCars(): number { return Car.numberOfCars; } } // creando un nuevo objeto a partir de otro let myCar1 = new Car('Cool Car Company', 'blue', 2); // Instantiates the Car object with all parameters //console.log(myCar1.color);//parametro que pasa al constructor //console.log(myCar1._color);//propiedad definida en la clase let myCar2 = new Car('Galaxy Motors', 'red', 2); //console.log(myCar2.doors)//no genera ningun error por que no se evalua en el Constructor //myCar2.doors = 5; //console.log(myCar2)//aca si genera el error let myCar3 = new Car('Galaxy Motors', 'gray'); //console.log(myCar3.doors); // returns 4, the default value //console.log(Car.getNumberOfCars()); <- Contador de carros que se han creado //volviendolos privados // Properties //-_-_-_-_-_-_-_-_EXTENDIENDO CAR -_-_-_-_-_-_-_-_-_-_-_-_-_-_ //usando herencia class ElectricCar extends Car { // Properties unique to ElectricCar private _range: number; // Constructor constructor(make: string, color: string, range:number, doors = 2){ super(make,color,doors); //sirve para usar las propiedades de el objeto original this._range = range; } // Accessors get range() { return this._range; } set range(range) { this._range = range; } // Methods charge() { console.log(this.worker() + " is charging.") } // Overrides the brake method of the Car class brake(): string { return `${this.worker()} is braking with the regenerative braking system.` } } let spark = new ElectricCar('Spark Motors','silver', 124, 2); let eCar = new ElectricCar('Electric Car Co.', 'black', 263); /*console.log(eCar.doors); // returns the default, 2 spark.charge(); // returns "Spark Motors is charging" console.log(spark.brake()); // returns "Spark Motors is braking with the regenerative braking system"*/ //para ver si el objeto cumple con las validaciones usamos interface agregado de implement interface Vehicle { make: string; color: string; doors: number; accelerate(speed: number): string; brake(): string; turn(direction: 'left' | 'right'): string; } // de esta manera: /*class Car implements Vehicle { // ... }*/