본문 바로가기

콩's EDUCATION/콩's JAVA_RUN

오버라이딩 (overriding) 오버라이딩 class Person{ String name; int age; String title = "사람"; void eat(){ System.out.println("사람은 밥을 먹는다."); } void sleep(){ System.out.println("사람은 잠을 잔다."); } } class Employee extends Person{ // Person 에서 상속받은 title String title = "회사원"; void sleep(){ System.out.println("사람은 잠을 잔다."); System.out.println("회사원은 밤에만 잠을 잔다."); } } class Student extends Person{} public class InheritanceTest { publ.. 더보기
패키지 클래스 패키지 임포트 예제 edu1 패키지 생성 package test.edu1; public class A { public A(String name){ System.out.println(name+"에 의해 A객체를 생성합니다"); } } // A a1 = new A ("b") edu2 패키지 생성 package test.edu2; import test.edu1.A; //import java.util.Date; /** 자동 import * 1. 사용자 클래스 : 현재 클래스와 같은 패키지 * 2. api 클래스 : java.lang 패키지 */ public class B { public static void main(String[] args) { A a1 = new A("B클래스"); } } B클래스에 의해 A.. 더보기
상속 상속 class Person{ String name; int age; } class Employee extends Person{} class Student extends Person{} public class InheritanceTest { public static void main(String[] args) { Person p = new Person(); Employee e = new Employee(); Student s = new Student(); p.name = "이순신"; e.name = "강감찬"; s.name = "장보고"; p.age = 10; e.age = 40; s.age = 50; System.out.println(p.name+"은 나이가 "+p.age+"이다."); System.o.. 더보기
Java 문제 2 Book, BookMgr, BookStore Bookclass Book { private String title; private int price; Book(String title, int price) { super(); this.title = title; this.price = price; } Book() { super(); } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } } BookStore pu.. 더보기
생성자(Constructor) 생성자 (Constructor) class Employee{ int number; //사원 String name; // 이름 String title; // 직급 String dept; // 부서 //Employee(){super();} Employee(int number, String name, String title, String dept){ this.number = number; this.name = name; this.title = title; this.dept = dept; } } public class ConstructorTest { public static void main(String[] args) { // 명령행 매개변수 4개 전달 // Employee 객체 생성하면서 멤버변수값 초기화 E.. 더보기
오버로딩 overloading 오버로딩 예제 오버로딩; 하나의 클래스 내부에 같은 이름의 메소드 여러개 정의, 매개변수 리스트(갯수, 타입, 순서) 중 하나 이상 다르게 정의, 메소드를 중복 재정의한다. class Adding { int add(int i, int j) { return i + j; } String add(String i, String j) { return i + j; } double add(double i, double j) { return i + j; } } public class OverloadingTest { public static void main(String[] args) { Adding a = new Adding(); System.out.println(a.add(3, 4)); System.out.print.. 더보기
팩토리얼 재귀 팩토리얼 재귀 class facto{ int fact(int number){ if(number == 1) return 1; if(number == 2) return 2; return number * fact(number-1); } } public class FactorialTest { public static void main(String[] args) { // 명령형 매개변수 5 == 5! int input = Integer.parseInt(args[0]); if(input==0){ System.out.println("잘못 입력하셨습니다."); return; } facto f = new facto(); System.out.println(input+"!="+f.fact(input)); } } 5!=120 더보기
팩토리얼 팩토리얼 factorial 기본 public class FactorialTest { public static void main(String[] args) { // 명령형 매개변수 5 == 5! int input = Integer.parseInt(args[0]); if(input==0){ System.out.println("잘못 입력하셨습니다."); return; } int result = 1; for(int i=1;i 더보기
CallByReference CallByReference 예제 class A{ int x = 10; } class Test{ void addInt(int i){ System.out.println(i++); } void addA(A a1){ System.out.println(a1.x++); } } public class CallByReferenceTest { public static void main(String[] args) { Test t = new Test(); A a2 = new A(); t.addA(a2); System.out.println(a2.x); } } 10 11 더보기
CallByValue CallByValue 예제 class Test{ void addInt(int i){ System.out.println(i++); } } public class CallByValueTest { public static void main(String[] args) { Test t = new Test(); int j = 10; t.addInt(j); System.out.println("t.addInt(j) 호출 : "+j); } } 10 t.addInt(j) 호출 : 10 더보기