Skip to content

Instantly share code, notes, and snippets.

@sunmeat
Last active August 6, 2024 08:47
Show Gist options
  • Save sunmeat/fcdbda50040ab881eec5 to your computer and use it in GitHub Desktop.
Save sunmeat/fcdbda50040ab881eec5 to your computer and use it in GitHub Desktop.
this example
package com.alex.encapsulation;
class Person {
Person ref = this;
String name;
String surname;
int age;
Person(String name, String surname, int age) {
// name = name; // "мантрический" код???
this.name = name; // применение this # 2 (после делегирования конструкторов)
this.surname = surname;
this.age = age;
}
void print() {
// System.out.println(name + " " + surname + ", " + age + " лет");
System.out.println(this); // System.out.println(ref);
// this = new Person(); // cannot assign a value to final variable this!
}
}
class Program {
public static void main(String[] args) {
Person p = new Person("Иван", "Иванов", 35);
System.out.println(p);
p.print();
// Person.print(p);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment