"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = {};
// 匿名函数
// const qian =function(a:number,b:number):number{
// return a+b
// }
// let arr=qian(100,200)
// console.log(arr);
// 有名函数 |命名函数 |普通函数
function hs(a, b) {
return b;
}
let res = hs(8, 1);
console.log(`我今天写了${res}小时`);
// 箭头函数
// const meimei=(time:number):void=>{
// console.log(`我今天写了${time}小时`);
// }
const meimei = (time) => console.log(`我今天写了${time}小时`);
meimei(8);
const myFunc = (a, b) => a + b;
let res2 = myFunc(3, 2);
console.log(res2);
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = {};
// 可选参数
const fun1 = (x, y) => {
return y;
};
console.log(fun1(100, 200));
const fun2 = function (a, b) {
return a;
};
fun2(10);
fun2(10, 20);
fun2(10, undefined);
// 参数的默认值
const fun3 = function (x = 2, y = 1, z = 100) {
return x + y + z;
};
let res = fun3(100, 200, 300);
fun3();
fun3(1, 2);
console.log(res);
// 函数的剩余参数
const fun4 = function (...args) {
console.log(args);
};
fun4(1, 2, 34, 5, "哈哈");
const fun5 = function (a, b, ...args) {
console.log(a);
console.log(b);
console.log(args);
};
fun5(10, "heihei", 43, "哈哈", "嘿嘿");
export default{}
//构造函数
var div =new Function("a","b","return a*b")
var div2=div(100,2)
console.log(div2);
export default{}
//函数重载
function fun1(a:number,b:number):number
function fun1(a:string,b:string):string
function fun1(a:number,b:string):number
function fun1(a:string,b:number):number
function fun1(a:any,b:any):any{
return a+b
}
console.log(fun1(10,20));
//参数数量不同
function fun2(c:string):string
function fun2(c:number,d:number):number
function fun2(c:any,d?:any):any{
console.log(c);
console.log(d);
}
fun2("刘亦菲")
fun2(18,19)
export default{}
class hs{
name:string
age:number
//构造函数
constructor(name:string,age:number){
this.name=name
this.age=age
}
//函数方法
sayHello():void{
console.log(`我的女神叫${this.name},她今年${this.age}岁了`);
}
}
// 实例化类
let p =new hs("李沁",30)
p.sayHello()
export default{}
class hs{
name:string
age:number
//构造函数
constructor(name:string,age:number){
this.name=name
this.age=age
}
//函数方法
sayHello():void{
console.log(`我的女神叫${this.name},她今年${this.age}岁了`);
}
}
// 实例化类
let p =new hs("李沁",30)
p.sayHello()
class student extends hs{
score:number
constructor(name:string,age:number,score:number){
super(name,age)
this.score=score
}
sayHello(): void {
super.sayHello()
console.log(`我的男神叫${this.name},他今年${this.age}岁了,他的考试成绩是${this.score}`);
}
}
let s=new student("陈赫",32,98)
s.sayHello()
export default{}
class father{
static gz:number
static say():void{
console.log(`我想要的工资是${father.gz}k`);
}
}
father.gz=18
father.say()
//instanceof运算符
// instanceof运算符用于判断对象是否是指定的类型,如果是返回true,否则返回false
class son{}
let p=new son()
let pd=p instanceof son;
console.log("p是son实例化出来的吗?",pd);
//继承
class Studemt extends son { }
let s = new Studemt();
let isStudent = s instanceof son;
console.log("s是Person实例化出来的吗?", isStudent);
export default{}
class Person{
public name:string
protected age:number
private sex:string
constructor(name:string,age:number,sex:string){
this.name=name
this.age=age
this.sex=sex
}
say():void{
console.log(`我的名字是${this.name}性别是${this.sex},年龄是${this.age}`);
}
}
class Studemt extends Person{
score:string
constructor(name:string,age:number,sex:string,score:string){
super(name,age,sex)
this.score=score
}
say(): void {
console.log(this.name);
console.log(this.age);
// console.log(this.sex); //私有的
console.log(this.score);
}
}
//**readonly**:可以使用‘readonly`关键字将属性设置为只读的。只读属性必须在声明时或构造函数里被初始化。
class Print{
readonly str4string="我是声明时赋值的"
readonly str2 :string
readonly str3:string
readonly str4:string
readonly str5:string
constructor(str2:string,str3:string,str4:string,str5:string)
{
this.str2=str2;
this.str3=str3;
this.str4=str4;
this.str5=str5
}
// say():void{
// this.str2="爱你"
// }
hs():void{
console.log(this.str2,this.str3,this.str4,this.str5);
}
}
let p=new Print("刘亦菲","刘亦菲2","刘亦菲3","刘亦菲4")
p.hs()
export default{}
// 注意点:
// 如果存在get,但没有set,则该属性自动是只读的
// 如果没有指定setter 参数的类型,它将从getter的返回类型中推断出来
// 访问器和设置器必须有相同的成员可见性
class myName{
// 赋予一个默认值
private Name:string="杨幂"
// 读取字段的值
get hs(){
console.log("get");
return this.Name
}
//赋予字段的值
set hs(newName:string){
console.log("set");
this.Name=newName
}
}
let n =new myName()
console.log(n.hs);//取值
n.hs="刘亦菲" //赋值
console.log(n);
console.log(n.hs);//取值
export default{}
abstract class Person{
abstract name:string
abstract age:number
abstract show():string
showName():void{
console.log("hello world");
}
}
class Studemt extends Person{
name:string="张三"
age:number=18
show(){
return "仙剑奇侠传"
}
}
let s =new Studemt()
let res =s.show()
console.log(res);
export default{}
class Old{
name:string="李易峰"
constructor(){
console.log(`名字是${this.name}`);
}
}
class yang extends Old{
name:string="韩寒"
constructor(){
super()
console.log(`名字是${this.name}`);
}
}
let y =new yang()