为什么不允许constructor在TypeScript中使用单独的定义?
要有两个constructors,我需要像这样写我的代码.
constructor(id: number)
constructor(id: number, name?: string, surname?: string, email?: string) {
this.id = id;
this.name = name;
this.surname = surname;
this.email = email;
}
Run Code Online (Sandbox Code Playgroud)
因此,我需要?在第一个构造函数中不需要的参数之后.
为什么我不能这样写呢?
constructor(id: number) {
this.id = id;
}
constructor(id: number, name: string, surname: string, email: string) {
this.id = id;
this.name = name;
this.surname = surname;
this.email = email;
}
Run Code Online (Sandbox Code Playgroud)
因此,对于两个构造函数,所有参数都是必需的.
而且,如果我需要一个空的构造函数,事情变得更加怪异,因为我需要用a标记每个参数?.
constructor()
constructor(id?: number, name?: string, surname?: string, email?: string) {
this.id …Run Code Online (Sandbox Code Playgroud) 我有这个简单的组件
import {Component} from 'angular2/core';
import {RouterLink, RouteParams} from 'angular2/router';
import {Http, Response, Headers} from 'angular2/http';
import {User} from '../../models/user';
import 'rxjs/add/operator/map';
@Component({
template: `
<h1>{{user.name}} {{user.surname}}</h1>
`,
directives: [RouterLink],
})
export class UserComponent {
user: User;
constructor(private routeParams: RouteParams,
public http: Http) {
this.user = new User();
this.http.get('http://localhost:3000/user/' + this.routeParams.get('id'))
.map((res: Response) => res.json())
.subscribe((user: User) => this.user = user);
console.log(this.user);
}
}
Run Code Online (Sandbox Code Playgroud)
为什么不subscribe将响应转换为完整User对象.当我记录user变量时,我的控制台说User {_id: undefined, name: undefined, surname: undefined, email: undefined} …
我目前正在使用Android Studio 1.2开发Android应用程序.
我想在我的Android应用程序中使用外部Java项目作为依赖项.这个Java项目是一个Maven项目.
如何将此项目作为依赖项添加到我的Android应用程序中,因此我可以使用我的Android应用程序参考Java/Maven项目的类?
Android应用程序是使用Grandle构建的.