1、接口相关
/**
* 1、接口对类的约束
*/
//接口定义
interface Iprinter{
printing(msg:string):string;
}
interface Imessage{
getMsg():string;
}
//接口实现
class ColorPrinter implements Iprinter,Imessage{
printing(msg: string): string {
return `打印${msg}成功`
}
getMsg():string{
return `msg001`
}
}
let bd1 = new ColorPrinter()
console.log(bd1.printing("简历"))
console.log(bd1.getMsg())
/**
* 2、接口对方法的约束
*/
interface Imyfunction{
(a:string,b:number):boolean
}
let fun1:Imyfunction
fun1 = function(a:string,b:number):boolean{
return true
}
/**
* 3、接口对数组的约束
*/
interface Iarr{
[index:number]:number
}
let arr1:Iarr
arr1 = [12,1]
/**
* 4、接口对json的约束
*/
interface Idata{
name:string,
age?:number,
readonly email:string
}
function showData(n:Idata){
console.log(JSON.stringify(n))
}
showData({name:"zhangsan",email:"123@qq.com"})
/**
* 5、接口的继承
*/
interface Icolor extends Iprinter{
consolHello():void
}
class Print implements Icolor{
printing(msg:string):string{
return msg
}
consolHello():void{
console.log("hello")
}
}
let p2=new Print();
console.log(p2.printing("我是接口继承后实现的类输出信息"))
p2.consolHello()
二、类相关
/**
* 1、类的定义
*/
class Person{
name:string
age:number
constructor(name:string,age:number){
this.name=name
this.age=age
}
print(){
return `name:${this.name}--age:${this.age}`
}
}
let p1 = new Person("zhangsan",12)
console.log(p1.print())
/**
* 2、类的继承
*/
class Student extends Person{
cardNumber:string
school:string
constructor(name:string,age:number,cardNumber:string,school:string){
super(name,age)
this.cardNumber=cardNumber
this.school=school
}
print(){
return `name:${this.name}--age:${this.age}--cardNumber:${this.cardNumber}--school:${this.school}`
}
}
let s1 = new Student("zhangsan",12,"1234","beijingdaxue")
console.log(s1.print())
/**
* 3、多态
*/
class Animal{
eat(){
console.log("animal eat")
}
}
class Wolf extends Animal{
eat(){
console.log("狼吃肉")
}
}
class Dog extends Animal{
eat(){
console.log("狗吃屎")
}
}
let w=new Wolf()
let d=new Dog()
w.eat()
d.eat()
/**
* 4、抽象类、抽象方法
* 抽象类是提供其他类继承的基类,不能直接被实例化
* 抽象方法只能存在于抽象类中,抽象类中可以有抽象方法和非抽象方法
* 子类继承抽象类,实现基类的抽象方法
*/
abstract class Shape{
abstract run():void
fly():void{
console.log("fly")
}
}
class Circle extends Shape{
run(): void {
console.log("circle")
}
}
class Round extends Shape{
run():void{
console.log("round")
}
}
let c1=new Circle()
let r1=new Round()
c1.run()
r1.run()
三、方法相关
/**
* 1、方法重载
*/
function getInfo(info:string):void
function getInfo(info:number):void
function getInfo(info:any):void{
switch(typeof info){
case 'string':
console.log(`string:${info}`)
break;
case 'number':
console.log(`number:${info}`)
break;
default:
console.log(`any:${info}`)
}
}
getInfo("zhangsan")
getInfo(12)
四、泛型相关
/**
* 1、泛型方法
*/
function printArr<T>(arr:T[]):void{
for(let item of arr){
console.log(item)
}
}
printArr<number>([11,22,33])
printArr<string>(["aa","dd","ee"])
/**
* 2、泛型类
*/
class ArrayList<T>{
public list:T[]=[]
add(val:T):void{
this.list.push(val)
}
}
let numberList = new ArrayList<number>();
numberList.add(12)
numberList.add(23)
console.log(numberList.list)
/**
* 3、泛型接口
*/
interface IArrayList<T>{
(x:T,y:T):T
}
let stringList:IArrayList<string>
stringList = function(x:string,y:string):string{
return `${x}${y}`
}
console.log(stringList("1","2"))