้ŸณใŒๆตใ‚Œใชใ„ๅ ดๅˆใ€ๅ†็”Ÿใ‚’ไธ€ๆ™‚ๅœๆญขใ—ใฆใ‚‚ใ†ไธ€ๅบฆๅ†็”Ÿใ—ใฆใฟใฆไธ‹ใ•ใ„ใ€‚
ใƒ„ใƒผใƒซใ€€
็”ปๅƒ
Bro Code
16401ๅ›žๅ†็”Ÿ
Learn GETTER and SETTERS in 10 minutes! ๐Ÿ”

#java #javatutorial #javacourse

public class Main {
public static void main(String[] args) {

// They help protect object data and add rules for accessing or modifying them.
// GETTERS = Methods that make a field READABLE.
// SETTERS = Methods that make a field WRITEABLE.

Car car = new Car("Charger", "Yellow", 10000);

car.setColor("Blue");
car.setPrice(5000);

System.out.println(car.getColor() + " " + car.getModel() + " " + car.getPrice());
}
}

public class Car {

private String model;
private String color;
private int price;

Car(String model, String color, int price){
this.model = model;
this.color = color;
this.price = price;
}

String getModel(){
return this.model;
}
String getColor(){
return this.color;
}
String getPrice(){
return "$" + this.price;
}

void setColor(String color){
this.color = color;
}
void setPrice(int price){
this.price = price;
}
}

ใ‚ณใƒกใƒณใƒˆ