[자바] 겹치지 않는 랜덤 숫자 뽑아내기(Random) import java.util.Random; public class Main { public static void main(String[] args) { Random rand = new Random(); // int i = rand.nextInt(10); // System.out.println(i); int N = 10; int nums[] = new int[N]; for(int j=0; j 즐거운프로그래밍 2023.10.23
[자바] Vector 클래스 예제 import java.util.Vector; class MyInt { private int value; MyInt(int i) { this.value = i; } int getValue() { return value; } } public class Main { public static void main(String[] args) { Vector iv=new Vector(); for(int i=0; i 즐거운프로그래밍 2023.10.23
[자바] 인터페이스의 구현(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.. 즐거운프로그래밍 2023.10.23
[자바] 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; // .. 즐거운프로그래밍 2023.10.17
[자바] 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.. 즐거운프로그래밍 2023.10.17
[자바] extends, showInfo 문 활용한 예제 1. 메인 클래스 public class Main { public static void main(String[] args) { Yes24 yes24 = new Yes24(); Books books = new Books(); yes24.serf(books); // Goods goods=books Drone drone = new Drone(); yes24.serf(drone); // Goods goods=drone } } 2. yes24 클래스 class Yes24 { void serf(Goods goods) { goods.showInfo(); } } 3. 하위 클래스 class Goods { void showInfo() { System.out.println("상품"); } } class Books exte.. 즐거운프로그래밍 2023.10.17
[자바] extends, insert 문 활용한 예제 1. 메인 클래스 public class Main { public static void main(String[] args) { Computer myCom = new Computer(); USBDisk myDisk = new USBDisk(); myCom.insert(myDisk); // USB usb=myDisk USBWifi myWifi = new USBWifi(); myCom.insert(myWifi); // USB usb=myWifi USBFan myFan = new USBFan(); myCom.insert(myFan); // USB usb=myFan } } 2. 하위 클래스 class Computer { void insert(USB usb) { usb.connect(); } } class USB .. 즐거운프로그래밍 2023.10.17
[자바] 상속 extends와 private 함수를 사용한 예제 2 메서드 오버라이딩 규칙 상위 메서드의 이름, 인자, 반환형이 완전히 같아야 함. static, final, private으로 정의된 메서드는 오버라이딩 할 수 없다. public class Main { public static void main(String[] args) { Korean man = new Korean("정진철"); man.speak(); man.think(); } } public class Korean extends Human { private String name; public Korean(String name) { super(); this.name=name; System.out.println("한국사람"+this); } public void speak() { super.speak();.. 즐거운프로그래밍 2023.10.17
[자바] 상속 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.. 즐거운프로그래밍 2023.10.17
[자바] 클래스 메서드 오버로딩(overloading) 메서드 오버로딩 (overloading) 메서드 오버로딩은 같은 이름의 메서드를 중복해서 정의하는 것, 다형성을 지원하는 방법 중에 하나이다. public class Main { public static void main(String[] args) { Calculator calc = new Calculator(); int ires = calc.sum(10,20); long lres = calc.sum(10L, 20L); double dres = calc.sum(10.0, 20.0); System.out.println(ires); System.out.println(lres); System.out.println(dres); } } public class Calculator { public int sum(in.. 즐거운프로그래밍 2023.10.17