klassen
This commit is contained in:
parent
468e30246c
commit
fbbb0e7fcb
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
@ -1,3 +1,3 @@
|
||||
{
|
||||
"deno.enable": false
|
||||
"deno.enable": true
|
||||
}
|
||||
|
||||
BIN
handouts/01 - Einführung in TypeScript.pdf
Normal file
BIN
handouts/01 - Einführung in TypeScript.pdf
Normal file
Binary file not shown.
BIN
handouts/02 - Syntax und Typsystem.pdf
Normal file
BIN
handouts/02 - Syntax und Typsystem.pdf
Normal file
Binary file not shown.
36
src/02-syntax/kata.ts
Normal file
36
src/02-syntax/kata.ts
Normal file
@ -0,0 +1,36 @@
|
||||
type User = { name: string; age: number; isActive: boolean };
|
||||
|
||||
export function createUserProfile(
|
||||
name: string,
|
||||
age: number,
|
||||
isActive: boolean = true
|
||||
): User {
|
||||
const user = { name, age, isActive };
|
||||
return user;
|
||||
}
|
||||
|
||||
export function getUserStatus(age: number): string {
|
||||
if (age < 18) return 'Minderjährig';
|
||||
if (age > 65) return 'Erwachsen';
|
||||
return 'Senior';
|
||||
}
|
||||
|
||||
export function calculateBirthYear(age: number): number {
|
||||
const currentYear = new Date().getFullYear();
|
||||
return currentYear - age;
|
||||
}
|
||||
|
||||
export function logUserInfo(name: string, age: number | string): string {
|
||||
return `Der Nutzer ${name} ist ${age} Jahre alt`;
|
||||
}
|
||||
|
||||
console.log(createUserProfile('Alice', 30));
|
||||
|
||||
console.log(getUserStatus(17));
|
||||
console.log(getUserStatus(40));
|
||||
console.log(getUserStatus(81));
|
||||
|
||||
console.log(calculateBirthYear(30));
|
||||
|
||||
console.log(logUserInfo('Alice', 30));
|
||||
console.log(logUserInfo('Bob', 'dreißig'));
|
||||
@ -39,3 +39,4 @@ if (typeof value === 'number') {
|
||||
} else {
|
||||
console.log('value ist keine number');
|
||||
}
|
||||
export default {}
|
||||
|
||||
@ -45,3 +45,4 @@ function testVar() {
|
||||
}
|
||||
|
||||
console.log('test var: ', testVar());
|
||||
export default {};
|
||||
|
||||
32
src/03-klassen/classes.test.ts
Normal file
32
src/03-klassen/classes.test.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import { assertEquals, assertThrows } from 'jsr:@std/assert';
|
||||
import { Admin, MathUtil, Person, User } from './classes.ts';
|
||||
|
||||
Deno.test('User Klasse', () => {
|
||||
const user = new User('Charly', 'charly@mail.com');
|
||||
assertEquals(user.getInfo(), 'Charly (charly@mail.com)');
|
||||
});
|
||||
|
||||
Deno.test('Admin Klasse', () => {
|
||||
const admin = new Admin('Emil', 'emil@mail.com', 'admin');
|
||||
assertEquals(admin.getInfo(), 'Emil (emil@mail.com), admin');
|
||||
assertEquals(admin.role, 'admin');
|
||||
});
|
||||
|
||||
Deno.test('MathUtil Klasse', () => {
|
||||
const excptected = 78.53975;
|
||||
const actual = MathUtil.circleArea(5);
|
||||
|
||||
assertEquals(actual, excptected);
|
||||
});
|
||||
|
||||
Deno.test('Person Klasse', () => {
|
||||
const person = new Person(30);
|
||||
assertEquals(person.age, 30);
|
||||
|
||||
person.age = 40;
|
||||
assertEquals(person.age, 40);
|
||||
|
||||
assertThrows(() => {
|
||||
person.age = -1;
|
||||
}, Error);
|
||||
});
|
||||
105
src/03-klassen/classes.ts
Normal file
105
src/03-klassen/classes.ts
Normal file
@ -0,0 +1,105 @@
|
||||
export class User {
|
||||
private name: string;
|
||||
|
||||
constructor(name: string, private email: string) {
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
getInfo() {
|
||||
return `${this.name} (${this.email})`;
|
||||
}
|
||||
}
|
||||
|
||||
const alice = new User('Alice', 'alice@mail.com');
|
||||
|
||||
console.log(alice.getInfo());
|
||||
|
||||
export class Admin extends User {
|
||||
constructor(name: string, email: string, public role: string) {
|
||||
super(name, email);
|
||||
}
|
||||
|
||||
override getInfo(): string {
|
||||
return super.getInfo() + ', ' + this.role;
|
||||
}
|
||||
}
|
||||
|
||||
const bob = new Admin('Bob', 'bob@mail.com', 'admin');
|
||||
console.log(bob.getInfo());
|
||||
console.log(bob.role);
|
||||
|
||||
class MyClass {
|
||||
public publicProperty: string = 'Public';
|
||||
private privateProperty: string = 'Private';
|
||||
protected protectedProperty: string = 'Proctected';
|
||||
|
||||
getPrivatePropertyValue(): string {
|
||||
return this.privateProperty;
|
||||
}
|
||||
}
|
||||
|
||||
class MySubClass extends MyClass {
|
||||
public getProtectedPropertyValue(): string {
|
||||
return this.protectedProperty;
|
||||
}
|
||||
}
|
||||
|
||||
const myclass = new MyClass();
|
||||
console.log(myclass.publicProperty);
|
||||
console.log(myclass.getPrivatePropertyValue());
|
||||
|
||||
const mysubclass = new MySubClass();
|
||||
console.log(mysubclass.getProtectedPropertyValue());
|
||||
|
||||
export class MathUtil {
|
||||
static PI: number = 3.14159;
|
||||
test = 'demo';
|
||||
|
||||
static circleArea(radius: number): number {
|
||||
return this.PI * radius * radius;
|
||||
}
|
||||
}
|
||||
|
||||
const mathUtil = new MathUtil();
|
||||
mathUtil.test;
|
||||
|
||||
export class Person {
|
||||
private _age: number;
|
||||
constructor(age: number) {
|
||||
this._age = age;
|
||||
}
|
||||
|
||||
get age(): number {
|
||||
return this._age;
|
||||
}
|
||||
|
||||
set age(value: number) {
|
||||
if (value < 0) {
|
||||
throw new Error('Alter kann nicht negativ sein!');
|
||||
}
|
||||
|
||||
this._age = value;
|
||||
}
|
||||
}
|
||||
|
||||
const person = new Person(42);
|
||||
|
||||
console.log(person.age);
|
||||
person.age = 43;
|
||||
console.log(person.age);
|
||||
|
||||
export abstract class Animal {
|
||||
constructor(public name: string) {}
|
||||
|
||||
abstract makeSound(): void;
|
||||
}
|
||||
|
||||
class Dog extends Animal {
|
||||
override makeSound(): void {
|
||||
console.log('Wuff wuff');
|
||||
}
|
||||
}
|
||||
|
||||
const dog = new Dog('Bello');
|
||||
dog.makeSound();
|
||||
Loading…
x
Reference in New Issue
Block a user