在Java中,静态方法和变量可以通过对象引用访问,就像在下面的程序中一样,它工作得很好:
//StaticDemo.java
class StaticVerifier{
private int a,b;
public StaticVerifier(int a,int b){
this.a = a;
this.b = b;
System.out.println("Values are "+this.a+" "+this.b);
}
public static void takeAway(){
System.out.println("This is a static method...");
}
}
public class StaticDemo{
public static void main(String[] args){
StaticVerifier sv = new StaticVerifier(3,4);
sv.takeAway();
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试在C#中转换相同的代码时,它不允许对象访问静态方法并给出编译时错误.请参阅下面的代码和相关错误:
//StaticDemo.cs
using System;
public class StaticVerifier{
private int a,b;
public StaticVerifier(int a,int b){
this.a = a;
this.b = b;
Console.WriteLine("Values are "+this.a+" "+this.b);
}
public static void takeAway(){
Console.WriteLine("This is …Run Code Online (Sandbox Code Playgroud) HttpResponseclass(在@ angular / common / http中)是@ angular / http(不推荐使用)的类Response的替换。看文档并没有太多关于如何使用它的想法!此外,我试图替换旧的角度代码,但由于此类是通用的,因此需要类型,例如。赋予它类型将产生错误,如:HttpResponse<T>
Property 'json' does not exist on type 'HttpResponse<any>'
谁能帮我知道如何在Angular中使用HttpResponse类吗?
更新
这是我编写的代码段,即函数“ get”:
get(path: string, params: HttpParams = new HttpParams()): Observable<any> {
return this.http.get(`${environment.api_url}${path}`, { headers: this.setHeaders(), search: params })
.catch(this.formatErrors)
.map((res: HttpResponse<any>) => res.json());
Run Code Online (Sandbox Code Playgroud)