JAVA STUDY Chapter 12  콘솔 입력과 출력 Chapter 14  클래스의 상속  1 :  상속의 기본 2011.10.02 이진아
Chapter12 콘솔 입력과 출력
콘솔 출력 메소드 System.out.println System.out.print System.out.printf System.out.println(“ 안녕하세요” ); System.out.println(“ 이진아입니다” ); 안녕하세요 이진아입니다
콘솔 출력 메소드 System.out.println System.out.print System.out.printf System.out.print(“ 안녕하세요” ); System.out.print(“ 이진아입니다” ); 안녕하세요이진아입니다
콘솔 출력 메소드 System.out.println System.out.print System.out.printf System.out.print(“\” 안녕 \” 하세요 \n”); System.out.print(“ 이진아 \t 입니다 \\”); “ 안녕”하세요 이진아 입니다 \ 이스케이프 시퀀스 (Escape Sequence) \n, \t, \”, \\
콘솔 출력 메소드 System.out.println System.out.print System.out.printf Int age=28; String name=” 이진아” ; String nick=” 사슴” ; System.out.printf(“ 나이  : %d\n”, age); System.out.printf(“ 이름 ( 별명 ) : %s(%s)”, name, nick); 나이  : 28 이름 ( 별명 ) :  이진아 ( 사슴 ) 서식문자 ( 변환문자 ) %d, %o, %x %f, %e, %g %s, %c
콘솔 입력 메소드 Scanner kb = new Scanner(System.in); Int num = kb.nextInt(); Scanner public boolean nextBoolean() public byte nextByte() public short nextShort() public int nextInt() public long nextLong() public float nextFloat() public double nextDouble() public String nextLine()
콘솔 입력 메소드 import java.util.Scanner; class KeyboardScanning{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int num1 = sc.nextInt(); int num2 = sc.nextInt(); int sum = num1 + num2; System.out.printf(“ 입력된 정수  %d, %d 의 합은  %d”, num1, num2, sum); } } Scanner 3 4 입력된 정수  3, 4 의 합은  7 3 4 입력된 정수  3, 4 의 합은  7
콘솔 입출력을 해봅시다 오늘 발표 준비를 열심히 했습니까 ?(true/false) true 오늘 준비한 슬라이드는 몇 장인가요 ? 30 오늘  30 장의 슬라이드로 발표 열심히 하시기 바랍니다 . 오늘 발표 준비를 열심히 했습니까 ?(true/false) false 다음주에는 열심히 준비해주세요 . Scanner kb = new Scanner(System.in); System.out.println(“ 오늘 발표 준비를 열심히 했습니까 ? (true/false)”); boolean isTrue = kb.nextBoolean(); if(isTrue==true){ Sytem.out.println(“ 오늘 준비한 슬라이드는 몇 장인가요 ?”); int pages = kb.nextInt(); System.out.printf(“ 오늘  %d 장의 슬라이드로 발표 열심히 하시기 바랍니다 .”, pages); } else System.out.println(“ 다음주에는 열심히 준비해주세요 .”);
Chapter14 클래스의 상속  1 :  상속의 기본
상속의 개념 상속은 기존의 클래스를 재활용하여 새로운 클래스를 작성하기 위한 문법 ? 재활용할 수 있다는 이유만으로 상속을 사용하기에는 재활용하는 것이 새로 만드는 것보다 더 번거롭다 . 상속을 통해 연관된 일련의 클래스에 대한 공통적인 규약을 정의할 수 있다 . 패키지 단위의 재활용은 고려할 만 함 (CBD : Componet Based Dvelopment)
상속의 기본 문법 class MyName { private String name; public void tellmyName() {System.out.println(“ 내 이름은 “ +name);} } class Introduce  extends MyName { private String favorite; //  취미 private String speciality; //  특기 public void tellMyInfo() {System.out.println(“ 내 취미는 “ +favorite); System.out.println(“ 내 특기는 “ +speciality); tellMyName(); } } 상위 클래스 기초 클래스 하위 클래스 유도 클래스
상속과 생성자 class MyName { private String name; public MyName(String name){this.name = name;} public void tellMyName() {System.out.println(“ 내 이름은 “ +name);} } class Introduce  extends MyName { private String favorite; //  취미 public Introduce(String name, String favorite) { super(name); this.favorite = favorite;} public void tell MyInfo(){System.out.println(“ 내 취미는 “ +favorite);} } class BasicCrt{ public static void main(String[] args){ Introduce intro = new Introduce(“ 이진아” , “ 영화감상” ); intro.tellMyName(); intor.tellMyInfo(); } 1. 인스턴스 생성 4.Introduce 생성자 실행 3.MyName 생성자 호출 및 실행 2.Introduce 생성자 호출
상속과 생성자 class AAA { int num1; } class BBB extends AAA { int num2; } class AAA{ int num1; AAA(){} } class BBB extends AAA { int num2; BBB() {super();} } class AAA { int num1; AAA(int n) {num=n;} } class BBB extends AAA { int num2; BBB() {super(0); } } public static void main(String[], args) { BBB b1=new BBB(); //  가능 BBB b2=new BBB(1); //  불가능 } 상위클래스 생성자 자동 호출 생성자 상속 BBB() {num=n;} 하위클래스에 없는 생성자의 인자로 인스턴스 생성은 불가능
상속과 접근제어지시자 class AAA{ int num1; protected intnum2; } class BBB extends AAA{ BBB() { num1=10; //default  접근 num2=29;//protected  접근 } } class AAA { private num; AAA(int n) {num=n;} protected void ABC(int val1) { if(val1<0) return; val1+=num;  } protected int getABC() {return num;} } class BBB extends AAA { BBB() {super(0); public void DEF(int val2) {  ABC(val2);  } } protected 다른 패키지에 존재하더라도 상속관계에 있으면 접근을 허용하는 접근지시자 private  변수에 public  메소드로 간접적으로 접근 private  변수도 상속됨
상속과  static  변수 ( 메소드 ) class AAA{ public static int val=0; public void add(int num) {val+=num;} } class BBB extends AAA{ public void friendAdd(int num) { val+=num; } public void showVal() {System.out.println(val);} }  class CCC {  public static void main(String[] args) { AAA aaa=new AAA(); BBB bbb=new BBB(); aaa.add(1); bbb.friendAdd(3); BBB.val+=5; bbb.showVal(); } } AAA 의 변수  val 을 하위 클래스  BBB 의 이름으로 접근 가능 BBB.val AAA 의 변수  val 을 하위 클래스  BBB 에서 접근 가능
감사합니다

자바스터디(6기) 3

  • 1.
    JAVA STUDY Chapter12 콘솔 입력과 출력 Chapter 14 클래스의 상속 1 : 상속의 기본 2011.10.02 이진아
  • 2.
  • 3.
    콘솔 출력 메소드System.out.println System.out.print System.out.printf System.out.println(“ 안녕하세요” ); System.out.println(“ 이진아입니다” ); 안녕하세요 이진아입니다
  • 4.
    콘솔 출력 메소드System.out.println System.out.print System.out.printf System.out.print(“ 안녕하세요” ); System.out.print(“ 이진아입니다” ); 안녕하세요이진아입니다
  • 5.
    콘솔 출력 메소드System.out.println System.out.print System.out.printf System.out.print(“\” 안녕 \” 하세요 \n”); System.out.print(“ 이진아 \t 입니다 \\”); “ 안녕”하세요 이진아 입니다 \ 이스케이프 시퀀스 (Escape Sequence) \n, \t, \”, \\
  • 6.
    콘솔 출력 메소드System.out.println System.out.print System.out.printf Int age=28; String name=” 이진아” ; String nick=” 사슴” ; System.out.printf(“ 나이 : %d\n”, age); System.out.printf(“ 이름 ( 별명 ) : %s(%s)”, name, nick); 나이 : 28 이름 ( 별명 ) : 이진아 ( 사슴 ) 서식문자 ( 변환문자 ) %d, %o, %x %f, %e, %g %s, %c
  • 7.
    콘솔 입력 메소드Scanner kb = new Scanner(System.in); Int num = kb.nextInt(); Scanner public boolean nextBoolean() public byte nextByte() public short nextShort() public int nextInt() public long nextLong() public float nextFloat() public double nextDouble() public String nextLine()
  • 8.
    콘솔 입력 메소드import java.util.Scanner; class KeyboardScanning{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int num1 = sc.nextInt(); int num2 = sc.nextInt(); int sum = num1 + num2; System.out.printf(“ 입력된 정수 %d, %d 의 합은 %d”, num1, num2, sum); } } Scanner 3 4 입력된 정수 3, 4 의 합은 7 3 4 입력된 정수 3, 4 의 합은 7
  • 9.
    콘솔 입출력을 해봅시다오늘 발표 준비를 열심히 했습니까 ?(true/false) true 오늘 준비한 슬라이드는 몇 장인가요 ? 30 오늘 30 장의 슬라이드로 발표 열심히 하시기 바랍니다 . 오늘 발표 준비를 열심히 했습니까 ?(true/false) false 다음주에는 열심히 준비해주세요 . Scanner kb = new Scanner(System.in); System.out.println(“ 오늘 발표 준비를 열심히 했습니까 ? (true/false)”); boolean isTrue = kb.nextBoolean(); if(isTrue==true){ Sytem.out.println(“ 오늘 준비한 슬라이드는 몇 장인가요 ?”); int pages = kb.nextInt(); System.out.printf(“ 오늘 %d 장의 슬라이드로 발표 열심히 하시기 바랍니다 .”, pages); } else System.out.println(“ 다음주에는 열심히 준비해주세요 .”);
  • 10.
    Chapter14 클래스의 상속 1 : 상속의 기본
  • 11.
    상속의 개념 상속은기존의 클래스를 재활용하여 새로운 클래스를 작성하기 위한 문법 ? 재활용할 수 있다는 이유만으로 상속을 사용하기에는 재활용하는 것이 새로 만드는 것보다 더 번거롭다 . 상속을 통해 연관된 일련의 클래스에 대한 공통적인 규약을 정의할 수 있다 . 패키지 단위의 재활용은 고려할 만 함 (CBD : Componet Based Dvelopment)
  • 12.
    상속의 기본 문법class MyName { private String name; public void tellmyName() {System.out.println(“ 내 이름은 “ +name);} } class Introduce extends MyName { private String favorite; // 취미 private String speciality; // 특기 public void tellMyInfo() {System.out.println(“ 내 취미는 “ +favorite); System.out.println(“ 내 특기는 “ +speciality); tellMyName(); } } 상위 클래스 기초 클래스 하위 클래스 유도 클래스
  • 13.
    상속과 생성자 classMyName { private String name; public MyName(String name){this.name = name;} public void tellMyName() {System.out.println(“ 내 이름은 “ +name);} } class Introduce extends MyName { private String favorite; // 취미 public Introduce(String name, String favorite) { super(name); this.favorite = favorite;} public void tell MyInfo(){System.out.println(“ 내 취미는 “ +favorite);} } class BasicCrt{ public static void main(String[] args){ Introduce intro = new Introduce(“ 이진아” , “ 영화감상” ); intro.tellMyName(); intor.tellMyInfo(); } 1. 인스턴스 생성 4.Introduce 생성자 실행 3.MyName 생성자 호출 및 실행 2.Introduce 생성자 호출
  • 14.
    상속과 생성자 classAAA { int num1; } class BBB extends AAA { int num2; } class AAA{ int num1; AAA(){} } class BBB extends AAA { int num2; BBB() {super();} } class AAA { int num1; AAA(int n) {num=n;} } class BBB extends AAA { int num2; BBB() {super(0); } } public static void main(String[], args) { BBB b1=new BBB(); // 가능 BBB b2=new BBB(1); // 불가능 } 상위클래스 생성자 자동 호출 생성자 상속 BBB() {num=n;} 하위클래스에 없는 생성자의 인자로 인스턴스 생성은 불가능
  • 15.
    상속과 접근제어지시자 classAAA{ int num1; protected intnum2; } class BBB extends AAA{ BBB() { num1=10; //default 접근 num2=29;//protected 접근 } } class AAA { private num; AAA(int n) {num=n;} protected void ABC(int val1) { if(val1<0) return; val1+=num; } protected int getABC() {return num;} } class BBB extends AAA { BBB() {super(0); public void DEF(int val2) { ABC(val2); } } protected 다른 패키지에 존재하더라도 상속관계에 있으면 접근을 허용하는 접근지시자 private 변수에 public 메소드로 간접적으로 접근 private 변수도 상속됨
  • 16.
    상속과 static 변수 ( 메소드 ) class AAA{ public static int val=0; public void add(int num) {val+=num;} } class BBB extends AAA{ public void friendAdd(int num) { val+=num; } public void showVal() {System.out.println(val);} } class CCC { public static void main(String[] args) { AAA aaa=new AAA(); BBB bbb=new BBB(); aaa.add(1); bbb.friendAdd(3); BBB.val+=5; bbb.showVal(); } } AAA 의 변수 val 을 하위 클래스 BBB 의 이름으로 접근 가능 BBB.val AAA 의 변수 val 을 하위 클래스 BBB 에서 접근 가능
  • 17.