public- ок понятно
private - отображается и используется в том методе, где создано. Значит только методы, находящиеся в одном классе будут видеть и смогут использовать метод private
THIS- если конструктор или любой другой метод использует переменную или имя с Identical to Instance variables.any time you have the same variable in a method java looks through local variables and prints them. if a method contains the local variable the same name as the field name, it will always look for a local. to refer to field variable we use THIS. перед именем переменной
------------------------------------------------------What "this" really is in Java is an object for which you call a method. In this here example "this" is actually tunaObject, because you are using tunaObject to call a method named setTime. It has nothing to do with local variables.
Example1:
public void setTime(int h, int m, int s)
{
hour=h; // same as this.hour=h;
minute=m; // same as this.minute=m;
second=s; // same as this.second=s;
}
Example2:
public void setTime(int hour, int minute, int second)
{
this.hour=hour;
this.minute=minute;
this.second=second;
}
in Example2 you have to use "this" because your formal arguments are named the same as attributes of the tuna class. It wouldn't make any sense to write hour=hour. The only way to access your attributes in these situations is to use "this".
КАРОЧИ- картинкой нагляднее и проще!
private - отображается и используется в том методе, где создано. Значит только методы, находящиеся в одном классе будут видеть и смогут использовать метод private
THIS- если конструктор или любой другой метод использует переменную или имя с Identical to Instance variables.any time you have the same variable in a method java looks through local variables and prints them. if a method contains the local variable the same name as the field name, it will always look for a local. to refer to field variable we use THIS. перед именем переменной
------------------------------------------------------What "this" really is in Java is an object for which you call a method. In this here example "this" is actually tunaObject, because you are using tunaObject to call a method named setTime. It has nothing to do with local variables.
Example1:
public void setTime(int h, int m, int s)
{
hour=h; // same as this.hour=h;
minute=m; // same as this.minute=m;
second=s; // same as this.second=s;
}
Example2:
public void setTime(int hour, int minute, int second)
{
this.hour=hour;
this.minute=minute;
this.second=second;
}
in Example2 you have to use "this" because your formal arguments are named the same as attributes of the tuna class. It wouldn't make any sense to write hour=hour. The only way to access your attributes in these situations is to use "this".
КАРОЧИ- картинкой нагляднее и проще!
Комментариев нет:
Отправить комментарий