# 학습
클래스는 객체지향 프로그래밍에서 객체를 생성하기 위해 상태와 행동을 정의하는 일종의 설계도이다. 여기서 객체란 어플리케이션의 기능을 구현하기 위해 서로 협력하는 개별적인 실체로써 물리적일수도있고 개념적일수도있다
class Class { // 클래스
String constructor;
String instanceVar; // 인스턴스 변수
static String classVar; // 클래스 변수
static { // 클래스 초기화 블록 (클래스 변수 초기화에 사용된다.)
classVar = "Class Variable";
}
{ // 인스턴스 초기화 블록(인스턴스 변수 초기화에 사용된다) -> 클래스에서 인스턴스 초기화 블록이 생성자 초기화보다 먼저 진행된다.
instanceVar = "Instance Variable";
}
Class() { // 생성자
constructor = "Constructor";
}
void instanceMethod() { // 인스턴스 메서드
System.out.println(instanceVar);
}
static void classMethod() { // 클래스 메서드
System.out.println(classVar);
}
}
class Calculator{
int left,right;
public Calculator (int left,int rigt){
this.left = left;
this.right = right;
}
}
package week5;
public class Example2 {
public static String appName = "MyExample";
public static int add(int x, int y){
return x+y;
}
public static int min(int x,int y){
return x-y;
}
public static void main(String[] args) {
System.out.println(Example2.add(1,2));
System.out.println(Example2.min(1,2));
}
}
package week5;
public class Person {
public static String name = "YYH";
public void printName(){
System.out.println(this.name);
}
}
package week5;
public class Example3 {
public static int a;
public int b;
public static int c1(){
a=5; // ok
b=5; // x
}
public int c2(){
a=5; // ok
b=5; // ok
}
}
package week5;
public class Example3 {
private int x =1;
private int y= 2;
Example3(){}
Example3(int x,int y){
setX(x);
setY(y);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
package week5;
public class Example4 {
public static void main(String[] args) {
Example3 e1= new Example3();
Example3 e2= new Example3(3,4);
System.out.println(e1.getX());
System.out.println(e1.getY());
System.out.println(e2.getX());
System.out.println(e2.getY());
e1.setX(3);
e1.setY(4);
System.out.println(e1.getX());
System.out.println(e1.getY());
}
}
-> new 키워드는 새 객체에 메모리를 할당하고 해당 메모리에 대한 참조값을 반환하며 클래스를 인스턴스화한다. 일반적으로 객체가 메모리에 할당되면 인스턴스라고 부른다.
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
접근제어자 및 기타 제어자: 해당 메소드에 접근할수있는 범위를 명시하거나 위에서 업근했듯이 부가적인 의미도 부여할수있다.
반환타입: 메소드가 모든 작업을 수행한뒤에 반환할 타입을 명시한다.
메소드 이름: 메소드명은 동사여야하고, lowerCamelCase를 따르며 뜻이 명확해야한다.
위의 메서드는 getter/setter 메소드이다.
매개변수 리스트: 메소드에서 사용할 매개변수들을 명시한다.
메소드 시그니처: 컴파일러는 메소드 시그니처를 보고 오버로딩을 구별한다.
Point() {} // 기본 생성자
Point(int x,int y){
setX(x);
setY(y);
}
생성자를 명시하지 않으면 컴파일러가 자동으로 기본 생성자를 생성한다. 하지만 기본 생성자가 아닌 다른 형태의 생성자만 명시했다면 기본 생성자는 컴파일시에 생성되지 않는다.
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
this 키워드는 인스턴스 자신을 가르킨다. 위 코드에서 this를 사용함으로써 지역변수 x,y와 구분할수있다. 클래스 메소드에서는 this를 사용할수없다. 왜냐하면 인스턴스가 생성되지 않기때문이다.
this()는 해당 클래스 생성자를 호출할수있다. 그렇기 때문에 생성자를 재사용하는데 사용된다.(생성자 체이닝)
public class Point {
int x;
int y;
int z;
Point(int x, int y) {
this.x = x;
this.y = y;
}
Point(int x, int y, int z) {
this(x, y);
this.z = z;
}
}
package week5;
public class Node {
private int value;
private Node left;
private Node right;
Node(int value){
this.value = value;
this.left=null;
this.right=null;
}
public Node addLeftNode(int value) {
Node newNode = new Node(value);
this.left = newNode;
return newNode;
}
public Node addRightNode(int value){
Node newNode = new Node(value);
this.right = newNode;
return newNode;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
}
package week5;
import java.util.*;
public class BinaryTree {
public List<Integer> bfsList = new ArrayList<>();
public List<Integer> dfsList = new ArrayList<>();
public void bfs(Node node) {
Queue<Node> queue = new LinkedList<>();
queue.offer(node);
while (!queue.isEmpty()) {
Node n = queue.poll();
bfsList.add(n.getValue());
if (n.getLeft() != null) {
queue.offer(n.getLeft());
}
if (n.getRight() != null) {
queue.offer(n.getRight());
}
}
}
public void dfs(Node node) {
if (node == null) return;
if (node.getLeft() != null) {
dfs(node.getLeft());
}
dfsList.add(node.getValue());
if (node.getRight() != null) {
dfs(node.getRight());
}
}
}
package week5;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
import java.util.*;
class BinaryTreeTest {
static BinaryTree tree;
static Node root;
@BeforeAll
static void createBinaryTree() {
tree = new BinaryTree();
root = new Node(1);
Node temp = root.addLeftNode(2);
temp.addRightNode(6);
temp = temp.addLeftNode(3);
temp.addLeftNode(4);
temp.addRightNode(5);
temp = root.addRightNode(7);
temp.addLeftNode(8);
temp.addRightNode(9);
}
@Test
void bfs() {
tree.bfs(root);
List<Integer> answer = Arrays.asList(1, 2, 7, 3, 6, 8, 9, 4, 5);
assertArrayEquals(answer.toArray(), tree.bfsList.toArray());
}
@Test
void dfs() {
tree.dfs(root);
List<Integer> answer = Arrays.asList(4, 3, 5, 2, 6, 1, 8, 7, 9);
assertArrayEquals(answer.toArray(), tree.dfsList.toArray());
}
}