Тёмный

Java Programming Tutorial - 20 - Using "this" in Java 

A J
Подписаться 3,1 тыс.
Просмотров 3,5 тыс.
50% 1

============Main Class=================================
public class MainClass {
public static void main(String[] args){
Person person1 = new Person("Andrew");
Person person2 = new Person();
person1.setName("Alex");
}
}
============Person Class===============================
public class Person {
//instance variable
private String name;
//constructor to set name to argument given in parameter
public Person(){
this("default name");
}
//constructor to set name to argument given in parameter
public Person(String name){
this.name = name;
System.out.println("Person created: " + name);
}
//setter method to set name variable of object, displays name change
public void setName(String name){
this.name = name;
System.out.println("Changed name to " + getName(this));
}
//getter method to return object name, takes object in parameter
public String getName(Person person){
return name;
}
}

Опубликовано:

 

30 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 2   
@crassbmt
@crassbmt 5 лет назад
why is the print statement in the constructor?
@UniProgrammer
@UniProgrammer 5 лет назад
It is there just to show that the constructor was being called. Using output is helpful when learning and trying to understand code, and also debugging programs.
Далее
Java Programming Tutorial - 21 - Intro to Arrays
13:33
Просмотров 1,5 тыс.
I Took An iPhone 16 From A POSTER! 😱📱 #shorts
00:18
Coding Was HARD Until I Learned These 5 Things...
8:34
Java Classes & Objects
11:36
Просмотров 333 тыс.
Naming Things in Code
7:25
Просмотров 2,1 млн
5 JavaScript Concepts You HAVE TO KNOW
9:38
Просмотров 1,4 млн
Java constructors 👷
10:37
Просмотров 182 тыс.
Exception Handling in Java Tutorial
13:20
Просмотров 390 тыс.
Why You Shouldn't Nest Your Code
8:30
Просмотров 2,7 млн