mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Add snapshot of release-1.0.3 sources
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
===== TypeScript Sample: Simple =====
|
||||
|
||||
=== Overview ===
|
||||
|
||||
Simple use of classes and inheritance:
|
||||
- Interface: A simple interface that defines the interface for something that can drive.
|
||||
- Class: An implementation of a car.
|
||||
|
||||
=== Keep Playing ===
|
||||
|
||||
Want to experiment? Try adding a second interface: Flyable. Implement it in a Helicopter class, then write a FlyingCar class that implements both Drivable and Flyable!
|
||||
|
||||
interface Flyable { ... }
|
||||
class Helicopter implements Flyable { ... }
|
||||
class FlyingCar implements Drivable, Flyable { ... }
|
||||
|
||||
=== Running ===
|
||||
tsc interfaces.ts
|
||||
@@ -0,0 +1,52 @@
|
||||
interface Drivable {
|
||||
|
||||
// Starts the car's ignition so that it can drive.
|
||||
start(): void;
|
||||
// Attempt to drive a distance. Returns true or false based on whether or not the drive was successful.
|
||||
drive(distance: number): boolean;
|
||||
// Give the distance from the start.
|
||||
getPosition(): number;
|
||||
}
|
||||
|
||||
class Car implements Drivable {
|
||||
private _isRunning: boolean;
|
||||
private _distanceFromStart: number;
|
||||
|
||||
constructor() {
|
||||
this._isRunning = false;
|
||||
this._distanceFromStart = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the car's ignition so that it can drive.
|
||||
*/
|
||||
public start() {
|
||||
this._isRunning = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to drive a distance. Returns true or false based on whether or not the drive was successful.
|
||||
*
|
||||
* @param {number} distance The distance attempting to cover
|
||||
*
|
||||
* @returns {boolean} Whether or not the drive was successful
|
||||
*/
|
||||
public drive(distance: number): boolean {
|
||||
if (this._isRunning) {
|
||||
this._distanceFromStart += distance;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gives the distance from starting position
|
||||
*
|
||||
* @returns {number} Distance from starting position;
|
||||
*/
|
||||
public getPosition(): number {
|
||||
return this._distanceFromStart;
|
||||
}
|
||||
}
|
||||
|
||||
// Want to experiment? Try adding a second interface: Flyable. Implement it in a Helicopter class, then write a FlyingCar class that implements both Drivable and Flyable!
|
||||
Reference in New Issue
Block a user