728x90
반응형

Java 44

[자바] 인터페이스의 구현(interface)

인터페이스를 구현한 클래스는 인터페이스에 선언된 모든 추상 메서드를 구현해야 한다. 만약 구현되지 않은 추상 메서드가 있다면 자바 컴파일러는 에러를 출력한다. 인터페이스를 구현하기 위해서는 implement 지시자를 사용한다. 1. 인터페이스 import java.util.LinkedList; interface Weapon { // 클래스 아님, 동작 상호작용임, public void attack(); } // class Weapon { // 다형성 // public void attack() { // } //} // 위처럼 쓰고 아래 class들에 implements 빼고 extends 넣으면 똑같이 동작하지만 동작의 개념이 달라짐 // abstract class Weapon { // abstract p..

[자바] Vector 클래스

Vector 클래스는 java.util 패키지에 정의되어 있다. Vector는 배열과 달리 여러종류의 데어터형 데이터를 요소로 가질 수 있다. 데이터가 가득차면 자동으로 크기가 늘어나며 프로그래머가 임의로 크기를 조절할 수 있다. import java.util.Vector; public class HumanTest { public static void main(String args[]) { Human h = new Human(); Korean k = new Korean(); American a = new American(); Vector v = new Vector(); // h.speak(); // k.speak(); // a.speak(); // 다형성 // h.speak(); // h = k; // ..

[자바] extends 문 활용한 예제 (Game)

1. 메인 클래스 public class Main { public static void main(String[] args) { Worrior worrior = new Worrior(); Sword sword = new Sword(); worrior.take(sword); // Weapon weapon=sword worrior.attack(); Cane cane = new Cane(); worrior.take(cane); // Weapon weapon=cane worrior.attack(); } } 2. 워리어 클래스 class Worrior { Weapon weapon; void take(Weapon weapon) { this.weapon=weapon; } void attack() { weapon.use..

[자바] 상속 extends와 private 함수를 사용한 예제 1

public class Main { public static void main(String[] args) { Korean k = new Korean("공유"); System.out.println(k.getName()); k.setNationality("한국"); System.out.println(k.getNationality()); Korean k2 = new Korean("공유", "한국"); System.out.println(k2.getName()); System.out.println(k2.getNationality()); } } public class Human { private String nationality; Human(String nationality) { this.nationality=na..

[자바] 노트패드++ 예제 (자전거 클래스)

삼천리 자전거 회사에서 자전거 관리 프로그램을 요청했습니다. 1. 자전거 클래스를 적당한 영어로 정의하시오. 2. 여러분들이 관심있는 자전거 속성 2가지를 추출하여 private로 추가합니다. 3. 2가지 속성에 대한 getter, setter 함수를 클래스에 추가합니다. 4. 생성자 함수를 추가하시오. 5. 자전거 객체를 하나 생성한 후, 속성 정보를 출력하시오. public class Samcheonri { public static void main(String args[]) { // Bicycle BicycleParts = new Bicycle(); Bicycle BicycleParts = new Bicycle("iron","plastic"); // BicycleParts.setChain("iron..

[자바] 노트패드++ String 클래스

String 클래스는 문자열을 처리하기 위해 자바가 제공하는 클래스로 다음과 같은 방법으로 객체를 생성할 수 있다. public class Ex5 { public static void main(String agrs[]) { String firstStr = "문자열"; String secondStr = "문자열"; String thirdStr = new String("문자열"); if(firstStr == secondStr) System.out.println("firstStr과 secondStr은 같은 문자열 객체입니다."); else System.out.println("firstStr과 secondStr은 다른 문자열 객체입니다."); if(firstStr == thirdStr) System.out.pr..

[자바] 노트패드++ 다차원 배열

다차원 배열 : 배열의 요소가 또 다른 배열인 형태의 배열 public class Ex3 { public static void main(String args[]) { int srcNum[][] = {{1,2,3},{4,5,6},{7,8,9}}; int destNum[][] = new int[3][3]; System.out.println("원본 배열"); for(int row=0; row < srcNum.length; row++) { for(int col=0; col < srcNum[row].length; col++){ System.out.print(srcNum[row][col]+" "); destNum[col][row] = srcNum[row][col]; } System.out.print("\n"); p..

728x90
반응형