TypeScript接口中的多继承,多实现以及接口继承类
接口的继承与实现:interface A{save(o:any):void;select();update();delete();}interface B extends A{count():number;page();}class User implements B {count(): n...
·
接口的继承与实现 :
interface A{
save(o:any):void;
select();
update();
delete();
}
interface B extends A{
count():number;
page();
}
class User implements B {
count(): number {
throw new Error("Method not implemented.");
}
page() {
throw new Error("Method not implemented.");
}
save(o: any): void {
throw new Error("Method not implemented.");
}
select() {
throw new Error("Method not implemented.");
}
update() {
throw new Error("Method not implemented.");
}
delete() {
throw new Error("Method not implemented.");
}
constructor(parameters) {
}
}
接口的多实现:
interface A{
save(o:any):void;
select();
update();
delete();
}
interface B {
count():number;
page();
}
class User1 implements B,A {
count(): number {
throw new Error("Method not implemented.");
}
page() {
throw new Error("Method not implemented.");
}
save(o: any): void {
throw new Error("Method not implemented.");
}
select() {
throw new Error("Method not implemented.");
}
update() {
throw new Error("Method not implemented.");
}
delete() {
throw new Error("Method not implemented.");
}
constructor(parameters) {
}
}
接口的多继承:
interface A{
save(o:any):void;
select();
update();
delete();
}
interface X{
x();
}
// 接口多继承
interface B extends A,X{
count():number;
page();
}
class User2 implements B {
x() {
throw new Error("Method not implemented.");
}
count(): number {
throw new Error("Method not implemented.");
}
page() {
throw new Error("Method not implemented.");
}
save(o: any): void {
throw new Error("Method not implemented.");
}
select() {
throw new Error("Method not implemented.");
}
update() {
throw new Error("Method not implemented.");
}
delete() {
throw new Error("Method not implemented.");
}
constructor(parameters) {
}
}
扩展:
// TS中
class Point2D {
x:number;
y:number;
}
// 接口继承类
interface Point3D extends Point2D{
z:number;
}
let a:Point3D={x:1,y:2,z:3};//创建对象,未调用构造器
let a1:Point2D=new Point2D();//类实例化
// let a3:Point3D=new Point3D();//接口不能实例化
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献3条内容
所有评论(0)