본문 바로가기

Java

Class 클래스와 다형성 Class 클래스 //2011-3-2 Class 클래스 class ClassSCB { public static void main(String[] args) { ClassSCB A = new ClassSCB(); Class aClass = A.getClass(); System.out.println(aClass.getName()); return; } } Object 클래스의 getClass() 메소드 - 객체의 클래스를 돌려준다. 객체의 클래스는 프로그램 실행중 Class 클래스의 객체로 나타난다. Class 클래스 프로그램 실행중 각 객체의 클래스나 interface를 나타내기 위한 클래스 객체가 속한 클래스의 이름을 출력하기 위한 getName() 메소드 Person 클래스에서의 사용예 ClassSCB .. 더보기
Object 클래스 Object 클래스의 public 메소드 String toString() ☞ 객체를 나타내는 String 객체를 돌려준다. Class getClass() ☞ 객체가 속한 클래스를 돌려준다. boolean equals (Object arg) ☞ 객체가 arg와 같으면 true를 돌려준다. int hashCode() ☞ 객체의 hash code를 돌려준다. void notify() ☞ 쓰레드를 깨운다 void notifyAll() ☞ 연관된 모든 쓰레드를 깨운다 void wait() ☞ 쓰레드의 실행을 일시 중지시킨다. toString() 메소드 toString()은 모든 객체를 String 객체로 바꾸는 메소드 toString() 메소드는 모든 클래스에서 재정의될 수 있으며, 많은 클래스에서 객체를 표현하.. 더보기
매소드 재정의 (Method Overriding) 메소드의 재정의 // Person 클래스에서 정의한 print() 메소드를 서브클래스에서 재정의한 프로그램 class Person { protected String name; public Person(String aName) { name = aName; } protected void print() { System.out.println(" "); System.out.println(" Name : " + name); System.out.println(); } } class Professor extends Person { private String major; public Professor(String aName, String aMajor) { super(aName); major = aMajor; } prot.. 더보기
상속 (Inheritance) 상속의 개념 // Vehiecle 클래스로부터 서브클래스 Car를 정의한 프로그램 public class CarInformationClass { public static void main(String[] args) { Car aCar = new Car("Car", "Red", 5); aCar.printType(); aCar.printColor(); aCar.printNumOfPerson(); } } class Vehiecle { private String type; protected String color; public Vehiecle(String aType, String aColor) { type=aType; color=aColor; } /*protected*/ void printColor() { Sy.. 더보기
Random 클래스 Random 클래스의 메소드 int nextInt() int형의 전 범위에서 균등하게 분포된 난수를 발생시킨다. long nextLong() long형의 전 범위에서 균등하게 분포된 난수를 발생시킨다. double nextDouble() 0.0에서 1.0 범위내에서 균등하게 분포된 난수를 발생시킨다. float nextFloat() 0.0f에서 1.0f 범위에서 균등하게 분포된 난수를 발생시킨다. void setSeed (long seedNumber) seedNumber를 종자값으로 재설정한다. // 1에서 15사이의 난수 10개를 발생시키는 프로그램 import java.util.Random; class RandomNumberGenerator { public static void main (String.. 더보기