본문 바로가기

전체 글

static 키워드 예제1) public class Test3 {public static void main(String[] args) {System.out.println("main:"+Ex3.x);System.out.println("main:"+Ex3.y);//System.out.println("main:"+Ex3.z); //에러.//static 변수가 아닌 경우 반드시 객체 생성후에만 가능Ex3.print(); //Ex3.write(); //에러Ex3 ob = new Ex3();ob.write();}} // static 키워드가 붙은 변수나 메소드는 클래스가 메모리에 로딩됨과 동시에 메모리에 할당되므로 객체를 생성하지 않아도 클래스의 이름으로 바로 접근 가능(Ex3.x)// 클래스 앞에 static 키워드는 내부클래스인 .. 더보기
상속 super 슈퍼클래스=상위클래스=부모클래스서브클래스=하위클래스=자식클래스 예제1) public class Test1 {public static void main(String[] args) { Demo1 ob=new Demo1(); ob.write();}} class Ex1{protected int x;public Ex1(int x) {this.x=x;}} /*class Demo1 extends Ex1 { =>디폴트 생성자를 호출한 셈이다. 그런데 상위클래스에는 디폴트생성자는 없고 인자가있는 생성자만 있다.public void write(){System.out.println(x);}}*/ class Demo1 extends Ex1 {public Demo1() {super(10); System.out.println(".. 더보기
업캐스팅 다운캐스팅 예제1) public class Test9 {public static void main(String[] args) {// super 생성자(먼저 메모리할당) -> sub 생성자Demo9 ob1=new Demo9(); // new에 의하여 객체생성하면서 생성자 실행System.out.println(ob1.b); // 자기것(30) -> 자기것은 Demo9(하위클래스). ob1.exam(); // 자기것 메소드 실행 Ex9 ob2=ob1; // 업캐스팅 => 변수는 상위클래스것을 쓰고 메서드는 하위에 없으면 상위 것을 쓴다.(재정의가 되어있는 메소드에 한해서) //Ex9 ob4=new Demo9(); // 이것은 객체생성과 동시에 업캐스팅한것이다. // 상위 클래스 객체는 하위 클래스 객체를 가르킬수 있다... 더보기
재정의(override), 중복정의(overloading) 예제) public class Test8 {public static void main(String[] args) {Rect aa=new Rect();Circle bb=new Circle();aa.calc(10, 20);aa.write("사각형 넓이 :");bb.result(10);bb.write("원 넓이 :");}} class Ex8 {protected double area;public void write(String title) {System.out.println(title+area);}} class Rect extends Ex8 {private int w, h;public void calc(int w, int h) {this.w=w;this.h=h;area=(double)this.w*this.h;}.. 더보기
자바의 상속 예제) public class Test7 {public static void main(String[] args) {Demo7 ob=new Demo7();ob.set();ob.write();ob.print();}} class Ex7 {private int a;protected int b;public int c, d;public void print() {System.out.println("a:"+a+",b:"+b+",c"+c+",d:"+d);}} // 자바는 단일 상속만 지원한다.class Demo7 extends Ex7 {public int d;public void set() {// a=10; // supper 클래스의 접근제어자가 private는 접근불가b=20;c=30;d=40;}public void .. 더보기
Scanner 클래스 예제1) import java.util.Scanner; /* * Scanner * 단락문자의 패턴을 사용하여 입력을 토근에 따라 * 분할하며 기본 토근은 공백이다. * 예외처리를 명시하지 않아도 된다. */public class Test5 {public static void main(String[] args) {String name;int k, e;Scanner sc=new Scanner(System.in); /*System.out.print("이름?");name=sc.next();System.out.print("국어?");k=sc.nextInt();System.out.print("영어?");e=sc.nextInt();*//*// 홍길동 80 60System.out.print("이름 국어 영어(공백으로 .. 더보기
SimpleDateFormat 예제1) import java.text.SimpleDateFormat;import java.util.Date; public class Test3 {public static void main(String[] args) throws Exception {Date dd=new Date();System.out.println(dd);/*// 년, 월, 일(추천되지 않음)int y=dd.getYear()+1900;int m=dd.getMonth()+1;int d=dd.getDate();System.out.println(y+"-"+m+"-"+d);*//* * SimpleDateFormat : Date 객체를 텍스트로 변환하거나 * 텍스트를 Date 객체로 변환 * yyyy-MM-dd HH:mm:ss * h:mm a -.. 더보기
Wrapper 클래스 예제) public class Test1 {public static void main(String[] args) {int a=10;Integer ii;// 기본자료형 -> Wrapper 클래스로 변환ii=new Integer(a);// JDK 5.0부터는 다음과 같이 가능ii=a; // autoboxing(컴파일시 ii=new Integer(a); 로 변환됨)// Wrapper -> 기본자료형으로 변환int b=ii.intValue();// JDK 5.0 부터는 다음과 같이 사용 가능int c=ii; // auto-unboxing(컴파일시 c=ii.intValue(); 로변환)System.out.println(a);System.out.println(b);System.out.println(c);System.. 더보기