tyscript2学习 函数继承等

ts2///tyscript2

// 类型推论

let x;

let x2='hello';

console.log(x2.toLowerCase())

// 内部实现 在js有一个自动装箱的过程,如果你对一个基本类型的数据进行调用方法的话,

// 它在内部会迅速做一次包装,把这个基本类型包装成对象类型,然后可以调用方法

console.log(new String(x).toLowerCase());// 内部实现的 我们不用管

let zname1:number|string

// toString 和 valueOf number 和string共有的方法

// 断言 强行告诉ts是一个什么类型 as 把变量当成什么来用

console.log((zname1 as string).length)

console.log((zname1 as number).toFixed(2))

// as断言的一定是联合类型的某一个类型

// 函数的定义

function hello(name:string):string{

return 'hello'+name;

}

// 定义函数有两种方法

// 函数表达式

type GetFunction = (firstname:string,lastname:string)=>string;

let getUsername:GetFunction = function(firstname:string,lastname:string){

return firstname+lastname

}

 

// 可选参数

function print(name:string,age?:number):void{

console.log(name,age)

}

print('郭子墨',10)//两个参数缺一不可

print('guozimo');// 第二个参数可有可无 age后面加一个?

// 默认参数

function ajax(url:string,method:string='GET'){

console.log(url,method);

}

ajax('/users')

 

function sum1(...numbers:Array<number>){

console.log(numbers);

}

console.log(sum1(1,2,3,4))

// 函数的重载

// java 中 两个函数方法名一样 但是参数的数量或者类型不一样

// ts中指为一个函数提供多个函数定义

function sum3(a,b){

 

}

function sum3(a,b,c){

}

let obj = {name:'guozimo',age:10}

// 改成any就可以写true了

// 通过重载定义传入变量的值

function attr(val:string):void

function attr(val:number):void

function attr(val:any){

if(typeof val==='string'){

obj.name=val

}else if(typeof val=='number'){

obj.age=val

}

}

attr('guozimo')

attr(10)

attr(true)// 报错

 

// type 定义别名 interface 是一个真正的类型

// 类

class Person{

state={}

// 实例的属性

name:string;

constructor(name:string){

this.name = name;

//类的属性初始化检查需要 所以我们需要在构造函数中赋值

}

getName():void{

console.log(this.name)

}

}

// getter setter

class User{

myname:string;

constructor(myname){

this.myname= myname;

}

get name(){

return this.myname

}

set name(newName:string){

this.myname=newName;

}

}

let u = new User('a')

console.log(u.name);

u.name='b'

console.log(u.name)// 最新的

console.log(u.hasOwnProperty(u.name)) // false

console.log(Object.getPrototypeOf(u).hasOwnProperty(u.name))// true

// readonly 只能读 不能改

class Animal{

public readonly name:string

private money:number=10

constructor(name:string){

this.name = name

}

}

let a = new Animal('guozimo');

a.name='x'// 是不允许的 只读属性

// 但是可以在构造函数中赋值// 在构造函数中传入

// 外边不允许赋值

// 通过类型的集成来讲一下 访问修饰符

// public protected private

// public 自己 自己的子类 其他类都能访问

class Dog extends Animal{

getName(){

console.log(this.name)

}

getMoney(){

// console.log(this.money)// 无法访问

}

// 加类的属性

static className = 'dog'

static getClassName(){

connsole.log(Dog.className)

}

}

let a =new Animal('guozimo')

a.name// public外部任何访问

// protected 自己和子类能访问 其他的地方不能访问

a.name //不能访问

// private 私有的 只有自己可以访问 子类和其他都不能访问

//抽象类 不能被实力化 只能被集成

abstract class Animal{

 

};// 抽象类一般是用来封装一些功能的方法的 抽象一些给子类方法

// 接口interface





















 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值