나는 기존 스칼라와 아크카 프로젝트 작업을 시작합니다. 저는 스칼라 클래스를 말합니다. 몇몇 필드는 그것들을 private으로 만들고 getter와 setter 메소드를 다른 방식으로 제공합니다. 왜 그런 식으로 공개적으로 만들 수 있겠습니까?스칼라 - 개인 멤버가 그때 왜
나는 내 프로젝트
에서 말하는 이유는이 방법 :
class Person() {
// Private age variable, renamed to _age
private var _age = 0
var name = ""
// Getter
def age = _age
// Setter
def age_= (value:Int):Unit = _age = value
}
그래서 공용처럼 같은 느낌을 얻을 수 있습니다 :
person.age = 99
왜이 간단한 방법은?
class Person() {
var name = ""
var age = 0
}
// Instantiate a person object
person = new Person()
// Print the object's age and name properties
println(person.age)
println(person.name)
// Set the properties to different values
person.age = 34
person.name = "Dustin Martin"
http://stackoverflow.com/questions/1568091/why-use-getters-and- 중복 setter – pamu
이것은 데이터 캡슐화라고 불리는 주요 OO 프로그래밍 원칙 중 하나입니다. https://en.wikipedia.org/wiki/Object-oriented_programming – iuriisusuk