728x90
반응형

프로그래밍 166

[자바] GUI 프로그램 기본 구조

자바 AWT(Abstract Window Toolkit)를 이용하여 메인 프레임 윈도우를 생성 프레임 윈도우를 생성하기 위해서는 Frame 클래스 객체를 생성한다. Frame frame = new Frame(); import java.awt.Frame; public class Main { public static void main(String[] args) { Frame frame = new Frame(); frame.setTitle("액자"); frame.setBounds(100, 100, 600, 400); frame.setVisible(true); System.out.println("return from main!!!"); } } AWT를 이요한 자바 GUI 프로그램 환경에서 main() 함수는 프..

[자바] 추상 클래스(abstract), 밴딩머신 예제

추상 클래스는 인터페이스와 마찬가지로 하위 클래스를 구현 되어야 하는 기능을 선언하여 상속 받는 하위 클래스가 구현하도록 하지만 인터페이스보다 느슨한 추상화를 제공한다. 인터페이슨느 상수와 추상 메서드만을 포함하지만 추상 클래스는 구현된 메서드와 멤버변수를 포함할 수 있기 때문이다. 1. Main 클래스 public class Main { public static void main(String[] args) { // VendingMachineControl vmc = new CoffeeMachine(); // vmc.on(); // vmc.insertMoney(); // vmc = new CarMachine() // useVM(new vmc); // vmc = new CarMachine(); useVM(n..

[자바] 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, 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..

[자바] 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 ..

[자바] 상속 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();..

728x90
반응형