Last active
August 6, 2024 08:47
-
-
Save sunmeat/fcdbda50040ab881eec5 to your computer and use it in GitHub Desktop.
this example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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