// function hellow(){}
// 1: 设置没有返回值
function hellow():void{
// return 1 // bug
}
// 1.1: 设置有返回值,并指定静态类型
// 设置返回值的类型为number类型
function hellow1():number{
return 1
}
// 1.2.1 返回值为数组 数组中元素为【任意类型】???
// 1.2.2 返回值为对象 函数
function hellow2():any{
// return {}
return []
}
// 1.3 :never 函数永远都不能执行到最后
function hellow3():never{
// throw new Error(); // 场景1 有错误了;所以不可能执行完毕
while(true){} // 场景2 一直为true 所以下面一直不可能执行完毕
if(true){
console.log('123');
}
}
// 2 参数的类型注解
function foo(params:number,a:string) {
}
// 2.1参数为对象
function ajax(options:{url:string,method:string,data:{},success:Function}) {
}
// 2.2 参数结构赋值
function foo1({a,b}:{a:number,b:number}) {
}
// foo2 参数为对象 对象中属性 a numebr b string 返回值 number
function foo2({a,b}:{a:number,b:string}):number {
return 1
}
// 2.3 类型别名
type ajax = {url:string,method:string,data:{},success:Function}
// 使用类型别名 指定形参的类型
function ajax1(options:ajax) {
}
// 类型别名 的赋值可以为任意静态类型
type zy = number;
let zy:zy = 123;
// 2.4 interface 接口
// interface 定义一个interface接口 名字叫做 blc
interface blc {
a:number,
b:string
}
function foo3({a,b}:blc):number {
return 1
}
// 1:变量wwl 类型函数
// 1.1:函数形参1个类型string
// 1.2:函数没有返回值
let wwl:(name:string)=>void = (name)=>{
}
// zyfoo 赋值类型是什么
let zyfoo:(option:blc)=>blc=()=>{
return {
a:1,
b:'123+'
}
}
// zyfoo 相当于 下面的写法
// let zyfoo1:(options:{a:number,b:string})=>{a:number,b:string} =()=> {
// return {
// a:1,
// b:'ss'
// }
// }
// 类名别名 与 interface 接口区别
// 都是为了制定静态类型
// type 可以定义任意类型 比如 number 对象类型
// interface 只能定义 对象或者函数
// 在ts开发规范中;能用 interface 绝不用type
class类
// 定义一个 Student类
class Student {
// 类中静态属性
// 将来实例化的对象都具备 name getName属性
a = 'name';
getName() {
return this.a
}
// 注意:指定构造器中 this.xx 类型 (key的类性)
eye:string;
bizi:string;
zuiba:string;
// constructor Student 构造器
// 1:形参赋值在Student类执行时,传入的实参
// 2 每个class类都有构造器
constructor(eye:string,bizi:string,zuiba:string){
// this 是 Student 实例的对象 blc/wwl
this.eye = eye;
this.bizi = bizi;
this.zuiba = zuiba
}
}
let blc = new Student('大眼睛','高鼻梁','小嘴');
console.log(blc);
console.log(blc.getName());
console.log(blc.eye);
let wwl = new Student('小眼睛','大鼻子','大嘴巴');
console.log(wwl);
console.log(wwl.getName());
console.log(wwl.eye);
// 类的继承
// extentds 继承
// 1 zy 类中将具有 Student 中的所有 属性
// 2 zy 创建的对象 中具有 Sudent和zy 中的属性
class zy extends Student {
age = 12;
getName() {
// super 相当于父类;super 一搬用来干什么呢? 一般用来调用父类的方法
return super.getName() + 'yan'
};
// 继承, 注意 子类中的constuctor 必须执行super
constructor(){
// super 触发父类中的构造器
super()
}
}
let wlj = new zy('aa','cc','cc')
console.log(wlj);
console.log(wlj.getName());
// class类中属性的访问权限:
// private 私有属性,允许在【自己类内】被调用
// protected 私有属性 允许在【自己类预计子类内部】调用
// public 公共属性,哪里都可以调用
// 以上 为 ts 中语法
class a {
private name = '123';
protected age(){
console.log('age');
}
public c = 45;
getName(){
// 类的内部 获取 类的属性
console.log(this.name);
console.log(this.age());
console.log(this.c);
return this.name
}
}
let zy = new a();
// console.log(zy.name); // bug
// console.log(zy.age()); // bug
console.log(zy.getName());
console.log(zy.c);
class b extends a{
num = 1;
getAage(){
// console.log(this.name); // roperty 'name' is private and only accessible within class 'a'
console.log(super.age());
console.log(this.c,'===================');
}
}
let blc = new b()
console.log(blc);
console.log(blc.getName());
// a 类的外币获取 name
// console.log(blc.name); // bug
// console.log(blc.age); // bug
console.log(blc.getAage());
console.log(blc.c);
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- huatuo0.cn 版权所有 湘ICP备2023017654号-2
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务