标签:his 自定义 定义 标签 web 你好 只读 types data-
npm install typescript -g
复制代码
或
yarn global add typescript
复制代码
新建demo.ts
:
function test() {
let web: string="hello world"
console.log(web);
}
test();
复制代码
编译:
tsc temo.ts
node temo.js
复制代码
或
npm install -g ts-node
复制代码
安装成功后:
ts-node Demo1.ts
复制代码
被定义后就不可改变该亦是的类型了。
js是弱类型语言,可以被改变。
// 定义count是number类型
const count : number = 1;
count = ‘hello‘; // 此时改变类型为string就会报错编译不通过
复制代码
interface XisoJieJie {
uname: string,
age: number
}
const xh : XisoJieJie = {
uname: ‘小红‘,
age: 15
}
console.log(xh);
复制代码
null
,undefinde
,symbol
,boolean
,void
等。
对象类型
const xiaoJieJie: {
name: string,
age: number
} = {
name: ‘小明‘,
age: 14
}
console.log(xiaoJieJie.name)
复制代码
数组类型
const arr : number [] = [1,2,3];
const xiaoJieJies : String [] = [‘小明‘,‘小红‘,‘小黄‘];
console.log(xiaoJieJies)
复制代码
类类型
class Person{}
const xm : Person = new Person();
console.log(xm);
复制代码
函数类型
const j : () => string = ()=> {return ‘小明‘};
console.log(j);
复制代码
let count : number;
count = 123
复制代码
let countInference = 123
复制代码
这时编辑器和编译器都会推断出是number类型。
无定义时:
function getTotal(one : number, two :number){
return one + two
}
const total = getTotal(1,2)
复制代码
虽然能推断出是返回number,但这样不规范。
定义时:
function getTotal(one : number, two :number) : number{
return one + two
}
const total = getTotal(1,2)
复制代码
无需return时:void
function sayHello(){
console.log(‘hello world!);
}
复制代码
异常或死循环:never返回值类型
异常:
function errorFuntion() : never{
throw new Error()
console.log(‘Hello World‘)
}
复制代码
死循环:
function forNever() : never{
while(true){}
console.log(‘Hello JSPang‘)
}
复制代码
TS函数参数解构:
function add ({one, two} : {one: number, two: number}) {
return one + two;
}
const three = add({one:1,two:3});
console.log(three); // 4
复制代码
ES6函数参数解构:
function add({x,y} = {}){
return x+y;
}
var sum = add({x:3,y:5});
console.log(sum); // 8
复制代码
const numberArr : number [] = [1,2,3];
const stringArr : string [] = [‘a‘,‘b‘,‘c‘];
const undefinedArr : undefined [] = [undefined, undefined];
const emptyArr : null [] = [null,null];
const arr : (number | string) [] = [1,‘a‘,1,2,‘b‘];
// 类型别名
type Lady = {name: string, age: number};
const xiaoJieJies: Lady[] = [{
name: ‘小明‘,age:14
},{
name: ‘小红‘, age: 35
}]
class Lady1 {name: string;age: number};
const xiaoJieJies1: Lady1[] = [{
name: ‘小明‘,age:14
},{
name: ‘小红‘, age: 35
}]
复制代码
type
和class
的区别是class
会编译出来,type
会忽略。
// 无约束
const arr1 : (number | string) [] = [1,‘a‘,1,2,‘b‘];
// 有约束
const arr2 : [string,string,number] = [‘a‘,‘b‘,3];
复制代码
interface Girl {
name: string;
age: number;
bust: number
}
const getResume = (girl: Girl) => {
console.log(girl.name);
}
const xh = {
name: ‘小红‘,
age: 19,
bust:38
}
getResume(xh);
复制代码
interface
)和类型别名(type
)的区别string
,但接口必须给对象。?:
语法:?:
interface Girl {
name: string;
age: number;
bust ?: number
}
复制代码
[anyname:string]:any;
语法:[anyname:string]:any;
string
的意思是对象名是string类型,any
的意思是对象值是任意类型。
interface Girl1 {
name: string;
age: number;
bust ?: number;
[anyname:string]: any;
}
const xh = {
name: ‘小红‘,
age: 19,
sex:‘女‘,
hobby:‘兴趣‘,
year:22
}
复制代码
语法:fun(): type
比如:say(): string;
代表say
方法返回string
类型的值。
interface Girl1 {
name: string;
age: number;
bust ?: number;
[anyname:string]: any;
say(): string;
}
const xh = {
name: ‘小红‘,
age: 19,
sex:‘女‘,
hobby:‘兴趣‘,
year:22,
say(){
return ‘hello World!‘;
}
}
复制代码
类实现接口时必须把所有要必填的属性和方法实现,否则就会报错。
错误的:
class XiaoJieJie implements Girl{
}
复制代码
正确的:
class XiaoJieJie implements Girl{
name = ‘小红‘;
age = 12;
bust= 90;
say(){
return ‘你好‘
}
}
console.log(XiaoJieJie)
复制代码
重点:
interface...extends
;function
里面的约束定型改为新接口名;interface Teacher extends Girl {
teach():string;
}
const getResume2 = (girl: Teacher) => {
...
girl.teach();
}
const girl2 = {
... // girl的所有属性
teach(){
return ‘hello‘;
}
}
getResume2(girl2);
复制代码
接口更像是一种约束,约束用户的对象的属性类型。
class Lady {
content = ‘hello world!‘;
sayHello () {
return this.content
};
}
class XiaoJieJie extends Lady {
sayLove () {
return ‘I love you.‘
};
sayHello () {
return super.sayHello() + ‘ 你好!‘;
}
}
复制代码
JAVA中类的继承
class Animal{
public void move(){
System.out.println("动物可以移动");
}
}
class Dog extends Animal{
@Override // 表示方法重写
public void move(){
super.move(); // 引用父类的方法
System.out.println("狗可以跑和走");
}
}
复制代码
区别:
@Override
标签@Override标签的作用与好处
默认就是它
私有,只有自己能用。
class Lady {
private content = ‘hello world!‘;
private privateFn () {
return ‘private‘ + this.content; // 能用
};
}
class XiaoJieJie extends Lady {
privateFn() { // 编译不通过
return ‘e‘;
};
}
console.log(new Lady().privateFn()); // 编译不通过
复制代码
受保护,只能在自己及子类用。
class Lady {
protected protectedFn () {
return ‘protected‘;
};
}
class XiaoJieJie extends Lady {
protectedFn () {
return ‘xj‘;
}
}
console.log(new Lady().protectedFn()); // 编译不通过
复制代码
类型 | public | protected | private |
---|---|---|---|
自己(父类) | ? | ? | ? |
继承(子类) | ? | × | × |
外部(new Class().fun() ) |
? | ? | × |
附:JAVA异同表
类型 | public | protected | private |
---|---|---|---|
自己(父类) | ? | ? | ? |
继承(子类) | ? | ? | × |
外部(new Class().fun() ) |
? | × | × |
class People {
constructor (public name: string,public sex:string) {
this.name = name;
this.sex = sex;
}
}
class Teacher extends People {
constructor (public age:number){
super(‘小明‘,‘女‘);
}
}
var people = new People(‘小红‘,‘女‘);
console.log(people.name)
var teacher = new Teacher(19);
console.log(teacher,teacher.age);
复制代码
ES6实现对比:
class People {
constructor (name,sex) {
this.name = name;
this.sex = sex;
}
}
class Teacher extends People {
constructor (name,sex,age){
super(name,sex);
this.age = age;
}
}
var people = new People(‘小红‘,‘女‘);
console.log(people.name)
var teacher = new Teacher(‘小明‘,‘女‘,19);
console.log(teacher,teacher.age);
复制代码
JAVA实现对比:
class People {
public String name;
public int sex;
public People (String name){System.out.println(name)};
public People (int sex ){};
public People (Stirng name, int sex){}
public People (){}
}
public class Teacher extends People {
...
}
复制代码
class XiaoJieJie {
constructor(private _age:number,private _name: string){
}
get age(){
return this._age-2;
};
set age(age:number){
this._age = age;
}
get name(){
return this._name+‘人‘;
};
set name(name:string){
this._name = name;
}
}
const xj = new XiaoJieJie(18,‘小明‘);
console.log(xj,xj.age); // XiaoJieJie { _age: 18, _name: ‘小明‘ } 16
复制代码
编译如果无法通过需要加上es5:
tsc demo.ts -t es5
node demo.js
复制代码
java中
class Stutent1{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void showStu1(){
System.out.println("学生叫做"+name+",年龄"+age);
}
}
复制代码
不用new新建就可以创建
class Girl {
static sayLove(){
return ‘你好‘;
}
}
console.log(Girl.sayLove());
复制代码
不用new Girl()就可以调用方法了。
class XiaoJieJie {
constructor(private readonly _age:number,private _name: string){
}
get age(){
return this._age-2;
}
set age(age:number){ // 报错
this._age = age;
}
get name(){
return this._name+‘人‘;
};
set name(name:string){
this._name = name;
}
}
const xj = new XiaoJieJie(18,‘小明‘);
xj.name = ‘小红‘;
xj.age = 33; // 报错
console.log(xj,xj.age);
复制代码
特点:
class Girl {
skill(){
conso.log(‘skill)
}
}
class Girl1 extends Girl {
skill(){
console.log(‘cooking‘);
}
}
class Girl2 extends Girl {
skill(){
console.log(‘make cloths‘)
}
}
class Girl3 extends Girl {
skill(){
console.log(‘do gardening‘)
}
}
console.log(new Girl1().skill())
console.log(new Girl2().skill())
console.log(new Girl3().skill())
console.log(new Girl().skill())
复制代码
特点:
abstract class Girl {
abstract skill()
}
class Girl1 extends Girl {
skill(){
console.log(‘cooking‘);
}
}
class Girl2 extends Girl {
skill(){
console.log(‘make cloths‘)
}
}
class Girl3 extends Girl {
skill(){
console.log(‘do gardening‘)
}
}
console.log(new Girl1().skill())
console.log(new Girl2().skill())
console.log(new Girl3().skill())
复制代码
区别:写了abstract
关键字就是抽象类,如果不加,就是重写(Override)
,也视为多态。
多态是同一个行为具有多个不同表现形式或形态的能力。 比如打印机有打印方法,彩色打印机类打印方法是彩色,黑色打印机类打印方法是黑色。
java中多态的实现方式
abstract class Animal {
abstract void eat();
}
class Cat extends Animal {
public void eat() {
System.out.println("吃鱼");
}
public void work() {
System.out.println("抓老鼠");
}
}
class Dog extends Animal {
public void eat() {
System.out.println("吃骨头");
}
public void work() {
System.out.println("看家");
}
}
复制代码
其它:写了abstract
关键字就是抽象类,如果不加,就是重写(Override)
,也视为多态。
该配置文件通过tsc --init
命令行来生成。
include
属性是用来指定要编译的文件
{
"include":["demo.ts"],
"compilerOptions": {
//any something
//........
}
}
标签:his 自定义 定义 标签 web 你好 只读 types data-
原文地址:https://www.cnblogs.com/heibaozi/p/13823104.html