ООП классическое против прототипного.pptx
- Количество слайдов: 8
ООП Классическое против прототипного
Прототип
Объявление классов public abstract class Deposit { protected Deposit(Deposit. Types type) { Type = type; } public readonly Deposit. Types Type; } function Deposit(type) { this. type = type; } export abstract class Deposit { public readonly type: Deposit. Types; constructor(type: Deposit. Types) { this. type = type; } }
Наследование public class Money. Deposit : Deposit { public Money. Deposit(decimal sum) : base(Deposit. Types. Money) { if (sum < 0) throw new function Money. Deposit(sum) { Deposit. call( this, Deposit. Types. Money ); if (sum < 0) throw new Error('sum < 0'); Argument. Out. Of. Range. Exception(nameof(sum)); this. sum = sum Sum = sum; } } public readonly decimal Sum; } Money. Deposit. prototype = Object. create(Deposit. prototype); Money. Deposit. prototype. constructor = Money. Deposit;
Наследование export class Money. Deposit extends Deposit { public readonly sum: number; constructor(sum: number) { super(Deposit. Types. Money); if (sum < 0) throw new Error('Invalid sum'); this. sum = sum; } }
Сокрытие public class Repository<TEntity> : IRepository<TEntity> where TEntity : IEntity { private readonly List<TEntity> _list = new List<TEntity>(); public void Add(TEntity entity) { _list. Add(entity); } public IEnumerable<TEntity> All() { return _list; } }
Сокрытие function Repository() { var _list = []; export class Repository<TEntity extends IEntity> extends IRepository<TEntity> { private readonly _list: TEntity[] = []; this. add = function(entity){ _list. push(entity); }; this. all = function(){ public void Add(TEntity entity) { this. _list. push(entity); } return _list. slice(0); public Readonly. Array<TEntity> All() { return this. _list; } }
Полиморфизм ad hoc статический динамический параметрический
ООП классическое против прототипного.pptx