Last active
December 19, 2021 10:14
-
-
Save sunmeat/0618d38a7d7c809c7509 to your computer and use it in GitHub Desktop.
c-tor optimization 1
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.constructors; | |
| class Person { | |
| String name = "Иван"; | |
| String surname = "Иванов"; | |
| int age = 25; | |
| Person() { // конструктор без параметров | |
| } | |
| Person(String n) { | |
| name = n; | |
| } | |
| Person(String n, String s) { | |
| name = n; | |
| surname = s; | |
| } | |
| Person(String n, String s, int a) { | |
| name = n; | |
| surname = s; | |
| age = a; | |
| } | |
| Person(String n, int a) { | |
| name = n; | |
| age = a; | |
| } | |
| void print() { | |
| System.out.println(name + " " + surname + ", " + age + " лет."); | |
| } | |
| } | |
| class Program { | |
| public static void main(String[] args) { | |
| Person people[] = new Person[5]; | |
| people[0] = new Person(); | |
| people[1] = new Person("Арья Старк"); | |
| people[2] = new Person("Гжегож", "Бженчишчикевич"); | |
| people[3] = new Person("Кандибобер", "Ибрагимов", 37); | |
| people[4] = new Person("Аристарх", 48); | |
| for (Person p : people) { | |
| p.print(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment