728x90
메소드 체이닝
-메소드를 고리마냥 줄줄이 엵어서 메서드를 계속해서 사용하게끔
class People {
private int height;
private int weight;
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
}
class ChainingPeople{
private int height;
private int weight;
public int getHeight() {
return height;
}
public ChainingPeople setHeight(int height) {
this.height = height;
return this;
}
public int getWeight() {
return weight;
}
public ChainingPeople setWeight(int weight) {
this.weight = weight;
return this;
}
}
People과 ChainingPeople의 set 메소드 비교
class main{
People p= new People();
p.setHeight(180);
p.setWeight(70);
ChainingPeople c= new ChainingPeople();
c.setHeight(170).setWeight(70); //set메소가 void가 아닌 해당객체를 리턴
}
참조형 매개변수 & 기본형 매개변수
class Data{
int x;
static void change(int x) {
x=1000;
System.out.println(x);
}
static void change(Data d) {
d.x=1000;
System.out.println(d.x);
}
}
public class Pra04 {
public static void main(String[] args) {
Data d= new Data();
d.x=20;
Data.change(d.x); //1000
System.out.println(d.x); //20
Data.change(d); //1000
System.out.println(d.x); //1000
}
}
메소드의 매개변수에 따라 Data의 멤버변수의 값이 바뀔 수 있음
JVM 메모리
Method Area Call Stack Heap
-클래스 데이터 -lv -인스턴스(iv)
(call stack)
main - lv |
final
final class Test{ //조상이 될 수 없음(생성자에 private붙이면
// (상속불가) class final붙여야함)
final int MAX=10; //값 변경 불가
final int getMax() { //오버라이딩 불가
final int LV=MAX; //값 변경불가
return LV;
}
}
public class Pra04 {
public static void main(String[] args) {
}
}
- 생성자를 이용한 final멤버변수 초기화
=> 생성자에서 final멤버변수 초기화시 --> 각인스턴스마다 final이 붙은 멤버변수가 다른 값을 갖도록 하게함
class Test{
final int NUMBER;
final String KIND;
Test(String a, int b){
KIND=a;
NUMBER=b;
}
}
public class Pra04 {
public static void main(String[] args) {
Test t = new Test("안녕",120);
t.KIND="잘가"; //에러
}
}
접근제어자
클래스 | public, default |
메서드 | public, protected, default, private |
멤버변수 | |
지역변수 | X |
'개발 이론 > Java' 카테고리의 다른 글
Java 기본 헷갈리는 문법(3) (0) | 2023.03.23 |
---|---|
Java 기본 헷갈리는 문법(1) (0) | 2023.03.22 |