关键字 this 在代码中的作用是什么?是否有没有此关键字的替代方法?
what does the keyword this does in the code and are there any alternate methods without this keyword?
在class同学这里函数中使用的关键字this指的是什么?
return 究竟是什么?
它也只用于 java 或 c/c++ 吗?
如果用其他语言有什么区别吗?
class Student
{
private String name;
private String section;
public static Comparator BY_NAME = new ByName();
public static Comparator BY_SECTION = new BySection();
public void setName(String name) {
this.name = name;
}
public void setSection(String section) {
this.section = section;
}
public String getName()
{
return this.name;
}
public String getSection()
{
return this.section;
}
private static class ByName implements Comparator
{
public int compare(Object s1, Object s2)
{
return ((Student)s1).name.compareTo(((Student)s2).name);
}
}
private static class BySection implements Comparator
{
public int compare(Object s1, Object s2)
{
return ((Student)s1).section.compareTo(((Student)s2).section);
}
}
}
在 Java 和 c++ 中 "this" 指的是对象的变量而不是 class 的变量。
在class同学这里函数中使用的关键字this指的是什么? return 究竟是什么? 它也只用于 java 或 c/c++ 吗? 如果用其他语言有什么区别吗?
class Student
{
private String name;
private String section;
public static Comparator BY_NAME = new ByName();
public static Comparator BY_SECTION = new BySection();
public void setName(String name) {
this.name = name;
}
public void setSection(String section) {
this.section = section;
}
public String getName()
{
return this.name;
}
public String getSection()
{
return this.section;
}
private static class ByName implements Comparator
{
public int compare(Object s1, Object s2)
{
return ((Student)s1).name.compareTo(((Student)s2).name);
}
}
private static class BySection implements Comparator
{
public int compare(Object s1, Object s2)
{
return ((Student)s1).section.compareTo(((Student)s2).section);
}
}
}
在 Java 和 c++ 中 "this" 指的是对象的变量而不是 class 的变量。