JAVA/클래스 ( 기본 )
-
생성자 + 캡슐화의 개념JAVA/클래스 ( 기본 ) 2023. 5. 3. 12:32
생성자란 생성자란 클래스를 통해 인스턴스가 생성될 때, 실행되는 것 목적 인스턴스 생성시 멤버변수 초기화의 용도로 쓰임 생성자도 일종의 메소드와 비슷한데, 따로 호출해주는 것이 아니라 인스턴스가 생성될 때 실행된다. 생성자는 기존의 메소드와는 특이하게 클래스의 이름을 그대로 따라간다. public class Circle { // 속성 -> 변수(필드, 멤버변수) public double radius; // 생성자 public Circle(){ System.out.println("Circle() 생성자 호출"); System.out.println("반지름 값: " + radius); } // 생성자 오버로딩 가능 public Circle(double radius){ System.out.println("Ci..
-
클래스란?JAVA/클래스 ( 기본 ) 2023. 5. 3. 12:12
클래스 vs 인스턴스 우리가 만들고자 하는 대상의 '상태' 와 '기능' 을 함께 가지고 있는 '데이터 타입' 클래스의 사용 클래스를 사용하기 위해서는, 해당 클래스의 인스턴스를 생성해야 한다. 구문 클래스이름 변수이름 = new 생성자(); 생성자(): 클래스의 이름과 동일, 클래스의 멤버변수들을 초기화 클래스 생성 구성요소 상태(속성, 필드) 기능(동작) 클래스는 내가 구현하고자 하는 것이 가지고 있는 상태와 속성을 정의해주고, 이에 따른 기능을 구현하는 방식으로 진행 TV 클래스를 만들어보자 public class MyTV { //상태 private boolean isPowerOn; // 전원 on/off private int channel; // 현재 채널 private int volume; // ..