1.2 类继承
2016-05-22 22:41
204 查看
extend实现类继承:
class Person{
name:string;
age:number;
constructor(name:string,age:number){
this.name=name;
this.age=age;
}
tell(){
return this.name+":"+this.age;
}
}
class Student extends Person{
school: string;
constructor(school:string){
this.school = school;
super("imi",80);
}
tell() {
return this.name + ":" + this.age+":"+this.school;
}
}
var s = new Student("逗币学员");
alert(s.tell())
编译后:
typescript帮我们省了这一步,
var __extends = (this && this.__extends) || function(d, b) {
for (var p in b)
if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var Person = (function() {
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.tell = function() {
return this.name + ":" + this.age;
};
return Person;
}());
var Student = (function(_super) {
__extends(Student, _super);
function Student(school) {
this.school = school;
_super.call(this, "imi", 80); //初始化父级,如果这个没有传参的话,将会是“undefined”
}
Student.prototype.tell = function() {
return this.name + ":" + this.age + ":" + this.school;
};
return Student;
}(Person));
var s = new Student("逗币学员");
alert(s.tell());
class Person{
name:string;
age:number;
constructor(name:string,age:number){
this.name=name;
this.age=age;
}
tell(){
return this.name+":"+this.age;
}
}
class Student extends Person{
school: string;
constructor(school:string){
this.school = school;
super("imi",80);
}
tell() {
return this.name + ":" + this.age+":"+this.school;
}
}
var s = new Student("逗币学员");
alert(s.tell())
编译后:
typescript帮我们省了这一步,
var __extends = (this && this.__extends) || function(d, b) {
for (var p in b)
if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var Person = (function() {
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.tell = function() {
return this.name + ":" + this.age;
};
return Person;
}());
var Student = (function(_super) {
__extends(Student, _super);
function Student(school) {
this.school = school;
_super.call(this, "imi", 80); //初始化父级,如果这个没有传参的话,将会是“undefined”
}
Student.prototype.tell = function() {
return this.name + ":" + this.age + ":" + this.school;
};
return Student;
}(Person));
var s = new Student("逗币学员");
alert(s.tell());
相关文章推荐
- RPC 的概念模型与实现解析
- IO流
- hdu 1094 A+B for Input-Output Practice (VI)
- jQuery是什么
- RPC 的概念模型与实现解析
- Android HelloWorld
- thinkphp集成系列之短信验证码、订单通知
- Foundation => Objective-C - NSDate & NSCalendar
- 高可用的工作心得分享
- 虚函数表
- Hive学习资料
- Java 鼠标点击事件实例
- HTTP响应
- 大话设计模式java版--适配器模式-关于不接口设计错误导致的处理方案
- HDU 3966 Aragorn's Story (树链点权剖分,成段修改单点查询)
- B00008 C++实现的大整数计算(一)
- Gridiew——表的内容居中
- 《zabbix用户权限分配管理》-8
- spring的依赖注入
- App 研发录、架构设计、Crash分析和竞品技术分析------读书笔记(第四章)