본문 바로가기

전체 글388

자바를 자바 11 class Object: method equals class object는 구지 extend하지 않아도 상속되어지는 클래스를 말한다고 하였다. 저번에는 toString을 배웠으니 이번에는 equals라는 함수에 대해서 알아보자 Employee empl1 = new Employee("John", 50000); Employee empl2 = new Employee("John", 50000); System.out.println(empl1.equals(empl2)); 위와 같이 작성이 되어 있을 때 equals 라는 함수를 사용하면 두개의 클래스가 같은 객체인지(같은 메모리에 저장된 객체인지) 혹은 둘 중 하나가 null인지를 확인한다. 그렇기 때문에 위의 객체들은 인자가 같을지라도 객체가 다르기 때문에 fal.. 2020. 11. 16.
자바를 자바 10 Inheritance(계속) Creating a subclass constructor 문제 -> 객체를 생성하면 constructor이 호출됨 마치 C++에서의 initializer과 동일함 만약 constructor가 없다면 default constructor가 호출됨, 이건 superclass의 constructor을 호출하게 됨 class Employee{ private String name; private int salary; public Employee(){ name="NoName"; salary=50000; } public String getname() {return this.name;} public int getSalary() {return this.salary;} } class Manage.. 2020. 11. 16.
자바를 자바 09 Interface(나머지 부분) Interface variable 변수들은 모두 자동으로 public static final variable 이 된다. 그렇기 때문에 이 변수들은 클래스의 실채와 상관없이 사용 가능하며 final은 값이 바뀌지 않기 때문에 상수처럼 사용할 수 있게 된 것이라고 생각해 볼 수 있다. interface Motion{ int NORTH = 1; int EAST = 2; int SOUTH = 3; int WEST = 4; void move(int direction); int getX(); int getY(); } class TwoDMotion implements Motion { private int posX, posY; public TwoDMotion() { posX = 0; p.. 2020. 11. 16.
자바를 자바 08 Four Principles of Object-Oriented Programming Encapsulation(캡슐화) 여러 클래스 간의 member variable과 method들을 함부로 접근하는 것을 방지하기 위해 꼭 필요한 것만 public으로 설정하고 그 이외의 것들은 private으로 유지한다. Abstraction(추상화) "interface"기능 : 이런식으로 구현한다는 명세는 존재하는데 그 내부는 실재로 그렇게 정의되어 있지 않는 것을 이야기 함. 유사하게 abstract classes라고 존재 Inheritance(상속) 두 객체간의 "is-a" 혹은 "has-a"관계를 가지게 하는 것을 의미하며 super class (parent class) vs sub class (child clas.. 2020. 11. 16.