typescript
interface 接口
interface Girl {
name: string,
age: number,
bust: number,
wastline?: number
}
let girl = {
name: '洛璃',
age: 18,
bust: 88
}
const xuanba = (girl: Girl) => {
girl.age < 24 && girl.bust < 90 && console.log(girl.name + '可以面试')
girl.age >= 24 || girl.bust >= 90 && console.log(girl.name + '被刷掉')
}
const info = (girl: Girl) => {
console.log(girl.name + girl.age + '岁')
console.log(girl.name + girl.bust + '胸围')
console.log(girl.name + girl.wastline + '腰围')
}
xuanba(girl)
info(girl)
interface Girl {
names: string,
age: number,
bust: number,
wastline?: number,
[propnames: string]: any,
say(): string
}
interface Teacher extends Girl {
teach(): string
}
let girl = {
names: '大脚',
age: 18,
bust: 88,
sex: '女',
hobby: '羽毛球',
say() {
return '你好呀'
},
teach() {
return '这学期我来教你们'
}
}
const xuanba = (girl: Girl) => {
girl.age < 24 && girl.bust < 90 && console.log(girl.names + '可以面试')
girl.age >= 24 || girl.bust > 90 && console.log(girl.names + '被刷掉')
}
const info = (girl: Teacher) => {
console.log(girl.names + girl.age + '岁')
console.log(girl.names + girl.bust + '胸围')
console.log(girl.names + girl.bust + '腰围')
console.log(girl.names + girl.sex + '性别')
console.log(girl.names + girl.hobby + '爱好')
}
xuanba(girl)
info(girl)
类
class Persons {
constructor(count: string) {
this.count = count
}
count: string
say(): string {
return this.count
}
}
class Childs extends Persons {
constructor(count: string, love: string) {
super(count)
this.love = love
}
love: string;
say(): string {
console.log('Childcount',this.count);
return this.count + this.love
};
}
const PerOne: Persons = new Persons('handsome man')
console.log('PerOne', PerOne.say());
const Children: Childs = new Childs('girl ', 'I Love You')
console.log('Children', Children);
console.log('基类的方法', Children.say());
类的访问类型
class Person {
constructor() {
this.name = ''
}
protected name: string
public sayHello(): string {
return 'hello'
}
}
const person: Person = new Person()
class Teacher extends Person {
sayTeach() {
return this.name
}
}
console.log(person.sayHello());
const learn = new Teacher()
console.log('learn.sayTeach()', learn.sayTeach());
类的构造器
class Aoteman {
constructor(public name: string) { }
}
class DijiaAoteman extends Aoteman {
constructor(name: string, public type: string) {
super(name)
}
}
const dijia = new DijiaAoteman('dijia', 'aoteman')
console.log(dijia.name);
console.log(dijia.type);
const aote = new Aoteman('迪迦')
console.log(aote.name);
类的 get 、set 、static , readonly
class Niuheng {
constructor(private age: number) { }
get sayAge() {
console.log('获得',this.age);
return this.age - 10
}
set sayAge(sayAge: number) {
console.log('sayAge',sayAge);
this.age = sayAge + 5
}
}
const erqiu = new Niuheng(23)
erqiu.sayAge = 28
console.log('erqiu', erqiu.sayAge);
class XiaoJiejie {
static sayLove() {
return "I Love You"
}
}
const jiejie = new XiaoJiejie()
console.log('jiejie',jiejie);
console.log('静态类型',XiaoJiejie.sayLove());
class Niuheng {
public readonly _name: string
constructor(name: string) {
this._name = name
}
}
const ren = new Niuheng('牛恒')
console.log('ren', ren._name);