function run():string {
return 'run'
}
let fun = function():number{
return 123
}
function getInfo(name:string,age:number):string{
return `${name}--${age}`
}
console.log(getInfo('zhansan',18);
var getInfo1 = function(name:string,age:number):string{
return `${name}---${age}`
}
console.log(getInfo1('zhansan',18));
// es5里面方法里面实参和形参可以不一样,但是ts中必须一样,如果不一样就需要配置可选参数
// 注意:可选参数必须配置到参数的后面
function getInfo2(name:string,age?:number):string{
if(age){
return `${name}--${age}`
}else{
return `${name}--`
}
}
console.log(getInfo2('zhangsan'));
console.log(getInfo2('zhangsan',19));
// es5里面没法设置默认参数,ts和es6都可以设置默认参数
function getInfo3(name:string,age:number=20):string{
if(age){
return `${name}--${age}`
}else{
return `${name}--`
}
}
console.log(getInfo3('zhangsan'));
console.log(getInfo3('zhangsan',19));
function sum(a:number,...result:number[]):number{
let sum1 = a;
for(let i=0; i
// java中方法的重载:重载指的是两个或者两个以上同名函数,但他们参数不一样,此时会出现重载情况
// typescript中的重载:通过为一个函数提供多个函数类型定义来实现多种功能的目的
// ts中的重载
function getInfo(name:string):string;
function getInfo(age:number):string;
function getInfo(str:any):any{
if(typeof str === 'string') {
return '我叫:'+ str
}else{
return '今年:' + str
}
}
console.log(getInfo('zs'));
console.log(getInfo(18));
// (-)
class Person{
name:string; // 属性 前面省略了public关键字
constructor(n:string){
// 构造函数 实例初始化类的时候触发的方法
this.name = n;
}
run():void{
alert(this.name);
}
}
let p = new Person('张三');
p.run()
// (二)
class Person1{
name:string;
constructor(name:string){
this.name = name;
}
getName():string{
return this.name
}
setName(name:string):void{
this.name = name
}
}
let p1 = new Person1('张三');
console.log(p1.getName());
p1.setName("李四");
console.log(p1.getName());
// 定义一个Person类
class Person{
name:string;
constructor(name:string){
this.name = name
}
run():string{
return `${this.name}在运动`
}
}
let p = new Person('王五');
console.log(p.run());
// Web类来继承Person类
class Web extends Person{
constructor(name:string){
super(name); // 初始化父类的构造函数
}
}
let w = new Web('赵六');
console.log(w.run());
// 注意:如果子类和父类有同样的方法,会优先使用子类的方法
class Person{
name:string;
constructor(name:string){
this.name=name;
}
run():string{
return `${this.name}在运动`
}
}
class Web extends Person{
constructor(name:string){
super(name); /*初始化父类的构造函数*/
}
run():string{
return `${this.name}在运动-子类`
}
work(){
alert(`${this.name}在工作`)
}
}
var w=new Web('李四');
alert(w.run()); // 李四在运动-子类
class Person{
public name:string;
constructor(name:string){
this.name = name;
}
getName():string{
return this.name
}
}
let p1 = new Person('张三');
console.log(p1.getName()); // ‘张三’ 在类里面访问
console.log(p1.name); // ‘张三’ 在类外面访问
class Man extends Person{
constructor(name:string){
super(name)
}
}
let m = new Man('李四');
console.log(m.name); // ‘李四’ 在子类里面访问
class Person{
protected name:string;
constructor(name:string){
this.name = name;
}
getName():string{
return this.name
}
}
let p1 = new Person('张三');
console.log(p1.getName()); // ‘张三’ 在类里面访问
// console.log(p1.name); // 无法类外面访问 error Property 'name' is protected and only accessible within class 'Person' and its subclasses.
class Man extends Person{
constructor(name:string){
super(name)
}
}
let m = new Man('李四');
console.log(m.name); // ‘李四’ 在子类里面访问
class Person{
private name:string;
constructor(name:string){
this.name = name;
}
getName():string{
return this.name
}
}
let p1 = new Person('张三');
console.log(p1.getName()); // ‘张三’ 在类里面访问
// console.log(p1.name); // 无法类外面访问 error Property 'name' is protected and only accessible within class 'Person' and its subclasses.
class Man extends Person{
constructor(name:string){
super(name)
}
}
let m = new Man('李四');
// console.log(m.name); // 无法再子类里面访问 error Property 'name' is private and only accessible within class 'Person'
如果不加修饰符,默认为public公有