当前位置:网站首页>TypeScript-类和接口、类和泛型、接口合并现象
TypeScript-类和接口、类和泛型、接口合并现象
2022-06-11 07:54:00 【YY小怪兽】
详情可见
1.类和接口
1.类"实现"接口
interface PersonInterface {
name:string;
say():void;
}
// 只要实现的某一个接口, 那么就必须实现接口中所有的属性和方法
class Person implements PersonInterface{
name:string = 'lnj';
say():void{
console.log(`我的名字叫:${
this.name}`);
}
}
let p = new Person();
p.say();
2.接口"继承"类
class Person {
// protected name:string = 'lnj';
name:string = 'lnj';
age:number = 34;
protected say():void{
console.log(`name = ${
this.name}, age = ${
this.age}`);
}
}
// let p = new Person();
// p.say();
注意点: 只要一个接口继承了某个类, 那么就会继承这个类中所有的属性和方法
但是只会继承属性和方法的声明, 不会继承属性和方法实现
注意点: 如果接口继承的类中包含了protected的属性和方法, 那么就只有这个类的子类才能实现这个接口
interface PersonInterface extends Person{
gender:string;
}
class Student extends Person implements PersonInterface{
gender:string = 'male';
name:string = 'zs';
age:number = 18;
say():void{
console.log(`name = ${
this.name}, age = ${
this.age}, gender = ${
this.gender}`);
}
}
let stu = new Student();
stu.say();
2.类和泛型
// 泛型类
class Chache<T> {
arr:T[] = [];
add(value:T):T{
this.arr.push(value);
return value;
}
all():T[]{
return this.arr;
}
}
let chache = new Chache<number>();
chache.add(1);
chache.add(3);
chache.add(5);
console.log(chache.all());
3.接口合并现象
当我们定义了多个同名的接口时, 多个接口的内容会自动合并
interface TestInterface {
name:string;
}
interface TestInterface {
age:number;
}
变成
interface TestInterface {
name:string;
age:number;
}
class Person implements TestInterface{
age:number = 19;
name:string = 'lnj';
}
边栏推荐
- 排序——交换排序
- 【CodeForces908H】New Year and Boolean Bridges (FWT)
- 如何开始参与开源社区
- [atcoder1998] stamp Rally
- SOCKET【5】- struct linger 用法
- Login and parameterization of interface test
- Rabin Miller prime test
- Detailed explanation of shift operator and bit operator in C language
- 【HDU6357】Hills And Valleys(DP)
- [atcoder2376] black and white tree (game)
猜你喜欢

The solution of "no startup device" after running Bochs

Zero foundation self-study SQL course | outer join external connection

Getting started with bladed tutorial (video)

TiDB Cloud 上線 Google Cloud Marketplace,以全新一棧式實時 HTAP 數據庫賦能全球開發者
![[atcoder1980] mystious light (mathematical simulation)](/img/c0/7de31b36e11ff71328d927c1d1c2d3.png)
[atcoder1980] mystious light (mathematical simulation)

SOCKET【5】- struct linger 用法

【 史上最全的ENSP【安装图解】!】

签到体系设计:签到功能该怎么画

使用 COCO 数据集训练 YOLOv4-CSP 模型

2022.6.7 special student simulation
随机推荐
Alchemy experience (model training of deep learning) the necessity of timely adjusting training parameters for some situations (the adjustment of learning rate LR is the primary) summarizes some metho
How to output the percent sign "%" in printf function in C language
2021-10-17
Post - payload of interface test
Detailed explanation of character function and string function (including simulation implementation)
通过ComponentCallbacks2来接收onTrimMemory等回调,并mock对应的场景
自定义ViewGroup的知识点总结-持续更新
Crawl Baidu Baipin dynamic page
Lesson 1 about Xiaobai's C language
Space geometry
C wechat upload form data
Database connection pool and bdutils tool
Activity中,View#postDelay会导致内存泄漏,但是不会影响Activity的生命周期执行。
multi-sig SC
如何做好空状态设计?来看这份全面总结
The solution of "no startup device" after running Bochs
How to start participating in the open source community
[codeforces1019e] raining season
[atcoder2306] rearranging (topology)
134. gas station