Compare commits
2 Commits
master
...
Lesson_1_1
Author | SHA1 | Date |
---|---|---|
|
d58ece266b | |
|
4f1310bbcf |
149
src/index.ts
149
src/index.ts
|
@ -1,137 +1,26 @@
|
||||||
|
function CalculateRectangleArea(sideA: number, sideB: number) : number {
|
||||||
// ########################## УЧЕБНЫЙ ТЕКСТ ############################################################
|
return sideA * sideB;
|
||||||
//******************************************************************************************************
|
|
||||||
|
|
||||||
interface People {
|
|
||||||
name: string;
|
|
||||||
age: number | undefined;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let people01: People = {
|
//Virtual console
|
||||||
name: "Kostya",
|
let chisloOne: string = '2';
|
||||||
age: undefined
|
let chisloTwo: string = 'A';
|
||||||
};
|
|
||||||
|
|
||||||
let people02: People = {
|
//Solution
|
||||||
name: "Yura",
|
let sideA = Number.parseInt(chisloOne, 10);
|
||||||
age: 32
|
let sideB = Number.parseInt(chisloTwo, 10);
|
||||||
|
|
||||||
|
// && - AND
|
||||||
|
// || - OR
|
||||||
|
// !
|
||||||
|
if (Number.isNaN(sideA) || sideA <= 0 ){
|
||||||
|
console.log(`Your first value is invalid: ${sideA}`)
|
||||||
|
}
|
||||||
|
if (Number.isNaN(sideB) || sideB <= 0){
|
||||||
|
console.log(`Your second value is invalid: ${sideB}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
people01.age = 60;
|
else{
|
||||||
// console.log(people01);
|
console.log(`Area of rectangle is: ${CalculateRectangleArea(sideA, sideB)}`);
|
||||||
// console.log(JSON.stringify(people01));
|
|
||||||
// console.log(people01.age);
|
|
||||||
console.log(`${people01.name} имеет возраст ${people01?.age ?? '40'}`); //Kostya --- 60
|
|
||||||
console.log(`${people02.name} имеет возраст ${people02?.age ?? '40'}`); //Yura --- 32
|
|
||||||
console.log(`${people01.name} имеет возраст ${people02?.age ?? '40'}`);
|
|
||||||
|
|
||||||
interface PeopleLocation extends People {
|
|
||||||
location: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let peopleLocation01: PeopleLocation = {
|
|
||||||
name: "djdf",
|
|
||||||
age: 42342,
|
|
||||||
location: "dafdf"
|
|
||||||
}
|
|
||||||
class ProcessingPeoples implements PeopleLocation {
|
|
||||||
name: string;
|
|
||||||
age: number | undefined;
|
|
||||||
location: string = '';
|
|
||||||
|
|
||||||
constructor(name: string){
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let str: string = 'test text';
|
|
||||||
let proc1 = new ProcessingPeoples(str)
|
|
||||||
console.log(proc1)
|
|
||||||
proc1.name = 'new test'
|
|
||||||
console.log(proc1)
|
|
||||||
|
|
||||||
class ProcessingPeoplesExt extends ProcessingPeoples {
|
|
||||||
isActive: boolean = false;
|
|
||||||
|
|
||||||
constructor(name: string, isActive: boolean){
|
|
||||||
super(name);
|
|
||||||
if(isActive) {
|
|
||||||
this.location = 'Gorkyi street';
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
this.location = '';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let a: ProcessingPeoples = new ProcessingPeoplesExt("Vasya", true);
|
|
||||||
|
|
||||||
class Duck {
|
|
||||||
type: string = 'Duck';
|
|
||||||
weight: number = 0;
|
|
||||||
log(){
|
|
||||||
console.log("I'm simple duck");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let Utka: Duck = new Duck();
|
|
||||||
Utka.log();
|
|
||||||
|
|
||||||
interface Eateable {
|
|
||||||
eat(): void; //пустота, функция ничего не принимает и не возвращает.
|
|
||||||
}
|
|
||||||
|
|
||||||
class GreyDuck extends Duck implements Eateable{ //наследоваться можно от другого класса только один раз, но!! реализация интерфейсов может быть не ограниченно количество раз
|
|
||||||
constructor() {
|
|
||||||
super(); // в данном примере, если используется конструктор и при этом этот конструктор наслеедуется от родителя, то мы обязаны вызывать конструктор родителя.
|
|
||||||
this.type = 'GreyDuck'
|
|
||||||
}
|
|
||||||
override log(){
|
|
||||||
console.log("I'm grey duck");
|
|
||||||
}
|
|
||||||
|
|
||||||
eat(){
|
|
||||||
console.log("I'm eat");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class RedDuck extends Duck implements Eateable {
|
|
||||||
constructor() {
|
|
||||||
super();
|
|
||||||
this.type = 'RedDuck'
|
|
||||||
console.log(`Object created ${this.type}`)
|
|
||||||
}
|
|
||||||
eat(): void {
|
|
||||||
console.log("I'm eat");
|
|
||||||
}
|
|
||||||
override log(){
|
|
||||||
console.log("I'm red duck");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let duck1 = new RedDuck();
|
|
||||||
let duck2 = new GreyDuck();
|
|
||||||
|
|
||||||
let array: Eateable[] = [
|
|
||||||
duck1,
|
|
||||||
duck2
|
|
||||||
];
|
|
||||||
|
|
||||||
let result = array[0]; //мы можем проверить содержание массива с именем array. в квадратных скобках мы указали ноль = это первый эелемент массива.
|
|
||||||
console.log(`результат содержимого массива${JSON.stringify(result)}`); //вывод на экран
|
|
||||||
|
|
||||||
for (let item of array) {
|
|
||||||
item.eat();
|
|
||||||
}
|
|
||||||
|
|
||||||
let array2: Duck[] = [
|
|
||||||
duck2,
|
|
||||||
duck1
|
|
||||||
];
|
|
||||||
|
|
||||||
for (let item of array2) {
|
|
||||||
item.log();
|
|
||||||
}
|
|
||||||
|
|
||||||
array2.filter(item => "eat" in item); //тестирование не относится к тексту выше
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,19 @@
|
||||||
|
import readline from 'node:readline';
|
||||||
|
|
||||||
|
export function ConsoleInput(text: string): string {
|
||||||
|
|
||||||
|
let result: string = '';
|
||||||
|
|
||||||
|
const rl = readline.createInterface({
|
||||||
|
input: process.stdin,
|
||||||
|
output: process.stdout,
|
||||||
|
});
|
||||||
|
|
||||||
|
rl.question('Enter something:', value => {
|
||||||
|
result = value;
|
||||||
|
rl.close;
|
||||||
|
});
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue