为什么有花括号来访问方法。
问题描述:
考虑一个班级
Consider a class
class Person(){
private int age;
public int getAge(){
return age;
}
}
要访问方法getAge(),我们可以直接将其称为
To access the method getAge() we can directly call it as
new Person().getAge();
我尝试了什么:
但是在使用Typetoken类时为什么我应该像这样调用方法getType() 。
What I have tried:
but while using Typetoken Class Why am i supposed to call the method getType() like this.
new TypeToken<List<T>>(){}.getType();
我为什么要戴上花括号。是什么原因。
Why should i put flower braces. what is the reason.
答
花括号是为新实例提供默认值,在这种情况下无关紧要,因为没有初始化数据它可以省略。
它们经常出现在列表,数组等中:
The curly braces there are to provide a default value for the new instance, and in this case are irrelevant as there is no initialization data in it so they can be omitted.
They are often seen in lists, arrays, and so forth:
List<string> names = new List<string>() { "John", "Mike", "Susan" };