728x90
반응형
클래스 제작
클래스를 직접 만들어보자
package Manpackage;
public class ManClass {
//instance 변수, private 선언을 해주었기에 다른 class에서 변수에 접근 및 사용이 불가능
//정보보호를 위한 '은닉화'
private int age;
private int height;
private int weight;
private String phoneNum;
public static void main(String[] args) {
// TODO Auto-generated method stub
}
//생성자
public ManClass() {
}
//parameter를 포함한 생성자
public ManClass(int age, int height, int weight,String phoneNum) {
//python의 self.~ 라 보자
this.age = age;
this.height = height;
this.weight = weight;
this.phoneNum = phoneNum;
}
//은닉화로 인해 접근이 불가능하면 어떻게 사용하나?
//getter와 setter 메소드를 이용해 접근
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
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;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
//메소드 생성
public double calculateBMI() {
double result = weight/(height+height);
return result;
}
}
생성한 class로 객체 생성해보기
package Manpackage;
public class MainClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
ManClass mc = new ManClass();
ManClass mc1 = new ManClass(15,160,50,"010-1111-1118");
double d = mc1.calculateBMI();
}
}
출처 : 신입 SW인력을 위한 실전 자바 (by 블스 강사님)
728x90
'Programming > Java' 카테고리의 다른 글
7. Static (0) | 2021.01.03 |
---|---|
6. 패키지 및 접근제한 (0) | 2021.01.03 |
4. 객체지향 프로그래밍 (0) | 2021.01.03 |
3. 배열 (0) | 2020.12.30 |
2. 변수와 연산자 (0) | 2020.12.30 |