본문 바로가기

Backend/JAVA41

JAVA 인터페이스 interface 인터페이스 - interface라는 키워드로 선언이 가능하다. - class 키워드 대신 interface 키워드가 붙는다. - 추상메서드롸 상수를 가질 수 있다. - 인터페이스를 상속 받을 시에는 implements 키워드로 상속 받는다. - 인터페이스를 상속받는 클래스는 다중 상속이 가능하다. 다중 상속을 남발하면 좋지는 않지만 대형 프로젝트에는 필요하다 package kr.or.ksmart; interface SampleInterface{ public void show(); } interface SampleInterface2{ public void print(); } class Sample implements SampleInterface, SampleInterface2{ @Override publi.. 2020. 4. 29.
JAVA instanceof 연산자 두 변수가 같은지 비교 할 수 있는 연산자 : 클래스 명만 같은지 비교 package kr.or.ksmart; class MemberDto{ private String name; private String age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } @Override public int hashCode() { final int prime = 31; //주소값 int result = 1; result = prime.. 2020. 4. 29.
JAVA equals( ) 메서드 public boolean equals(Object obj) Indicates whether some other object is "equal to" this one. The equals method implements an equivalence relation on non-null object references: ◦ It is reflexive : for any non-null reference value x, x.equals(x) should return true. ◦ It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.. 2020. 4. 29.
JAVA - hashCode( ) hashCode 메서드 public int hashCode() Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by HashMap. The general contract of hashCode is: ◦Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used .. 2020. 4. 29.
JAVA 추상 클래스 추상화 공통적인것을 모아서 추상적으로 만든다. ① 추상 클래스 ② 인터페이스 추상 클래스 - 클래스명 앞에 abstract 키워드가 붙는다. - abstract 키워드가 붙은 추상 메서드를 구현 할 수 있다. - 추상 클래스는 일반적인 메서드 및 필드도 구현이 가능하다. - 약간 유동성이 있다(유도리 있는 친구네). 인터페이스는 일반 메서드를 가질 수 없다. - 추상적인 클래스는 직접 인스턴스화를 할 수 없다. - 추상 메서드는 몸체가 없다. ('{ }' 구현부가 생략 되어야 한다.) - 추상메서드는 서브 클래스에서 필히 구현이 되어야한다.(오버라이딩) package kr.or.ksmart; abstract class Sample3{ public abstract void print(); } } publi.. 2020. 4. 27.
JAVA override && final 오버라이딩 - 슈퍼클래스의 메서드와 동일한 메서드 명과 인수로 재구현 하는 방식 - 슈퍼클래스의 메서드를 재구현 할 때 @Override 어노테이션이 붙는다. package kr.or.ksmart; class Car{ private int num; private int gas; public int getNum() { return num; } public void setNum(int num) { this.num = num; } public int getGas() { return gas; } public void setGas(int gas) { this.gas = gas; } } class Bus extends Car{ @Override public void setGas(int gas) { super.set.. 2020. 4. 27.
728x90
반응형