//2020 - 9 - 18
// 重载
// function cz(name: string): any
// function cz(name: string, age?: number): any {
// if (age) {
// return `我叫${name}今年${age}岁`;
// } else {
// return `我叫${name}`;
// }
// }
// // alert(cz("张三"))
// alert(cz("张三", 21))
// 定义类
class person {
public name: string;
constructor(name: string) {
this.name = name;
}
}
// 继承类
class pers extends person {
constructor(name: string) {
super(name)
}
run() {
console.log("子类的方法")
}
}
// 静态方法和静态属性
// 静态方法和静态属性就是由类名进行调用的
// 实例方法:指的就是通过new关键字实例构造函数后,通过实例的方式进行调用
class tests {
static names: string;
static runss() {
console.log("这是静态方法")
}
}
// 接口:接口主要的作用是起到一些限制和规范的作用
// 定义接口的关键字:interface
interface personTest {
// 这里我们定义一个接口,且必穿参数2个,分别是name,age还有一个可选参数text
name: string;
text?: any;
age: number;
}
function personTestFn(person: personTest) {
console.log(person.name)
if (person.text) {
console.log(person.name)
console.log(person.age)
console.log(person.text)
} else {
console.log(person.name)
console.log(person.age)
}
}
personTestFn({
name: "张三",
age: 15,
text: "这是内容"
})
// * ts中类抽象类、多态
// * 抽象类: abstract 修饰, 里面可以没有抽象方法。但有抽象方法(abstract method)的类必须声明为抽象类(abstract class)
// * 多态:父类定义一个方法不去实现,让继承它的子类去实现 每一个子类有不同的表现
// * 注意:使用多态基础是类的继承或者接口实现
//多态:多态就是定义一个方法,让继承它的子类去实现,每一个子类有不同的表现
//多态属于继承
// class tesst {
// public name: string;
// constructor(name: string) {
// this.name = name;
// }
// run(): any {
// };
// }
// class tesstJx extends tesst {
// constructor(name: string) {
// super(name);
// }
// run(): any {
// console.log(this.name + "吃东西")
// }
// }
// class tesstJxs extends tesst {
// constructor(name: string) {
// super(name);
// }
// run(): any {
// console.log(this.name + "玩游戏")
// }
// }
// let testFns = new tesstJx("张三");
// let testFnss = new tesstJxs("张三");
// testFns.run();
// testFnss.run();
//抽象类
//抽象类:抽象类是不能被实例化的
//抽象类里面的抽象方法,我们不需要去实现它,主要是让我们的子类去实现
// eg:
// abstract run(): any;
// 写抽象类里面不写抽象方法就没意义
// 抽象类的子类必须实现抽象类里面的抽象方法
abstract class tesst {
public name: string;
constructor(name: string) {
this.name = name;
}
abstract run(): any;
}
class tesstJx extends tesst {
constructor(name: string) {
super(name);
}
run(): any {
console.log(this.name + "吃东西")
}
}
class tesstJxs extends tesst {
constructor(name: string) {
super(name);
}
run(): any {
console.log(this.name + "玩游戏")
}
}
let testFns = new tesstJx("张三");
let testFnss = new tesstJxs("张三");
testFns.run();
testFnss.run();
// 新知识
// 函数类型接口
// 加密函数类型接口
interface encypt {
(key: string, value: string): string;
}
var md5: encypt = function (key, value): string {
return key + ' ' + value // 模拟加密操作
}
console.log(md5('李', '二狗'))
var sha1: encypt = function (key, value): string {
return key + '--' + value
}
console.log(sha1('dog', 'zi'))
// ts中定义数组的方式
// 对数组的约束,可索引接口
interface UserArr {
[index: number]: String
}
let shuzu: UserArr = ["aaa", "bbb"]
// 可索引接口,对对象的约束
interface UserObj {
[index: string]: string
}
// 类 类型接口,对类的约束 和 抽象类有点相似
interface Animo {
name: String
eact(str: string): void
}
class dog1 implements Animo {
name: String;
constructor(name: string) {
this.name = name;
}
eact() {
}
}
ts
最新推荐文章于 2024-04-15 19:27:28 发布