본문 바로가기
JAVA

221222_상속

by 경 훈 2022. 12. 22.

final : field : 값을 변경할 수 없다

final : method : 재정의가 안됨

final : class : 상속이 안됨

 

예외처리 : Exception

: 예외가 있다.

 

1.throws

2.try{ 예외처리 필요부분 } catch( 예외처리 클래스명 e)

{

   syso ....("에러메세지");

}

 

기본적인 :

에러가 나면 모든 프로그램이 중지

 

 

 

package day14_상속복습;
//Hap 과 Gop 클래스의 공통점 a,b
class Common {
	int a,b;
}
//메소드명을 일치하기 위해서 인터페이스
interface comMethod {
	public void out();
}
class Hap extends Common implements comMethod{

	@Override
	public void out() {
		System.out.println(a+"+"+b+"="+(a+b));
	}
}
//곱의 결과
class Gop extends Common implements comMethod{

	@Override
	public void out() {
		System.out.println(a+"*"+b+"="+(a*b));
	}
}
//메인클래스
public class MainClassTest {
	public static void main(String[] args) {
		Hap ha = new Hap();
		ha.a=23;
		ha.b=66;
		ha.out();
		Gop go = new Gop();
		go.a=23;
		go.b=66;
		go.out();
	}
}

 

package day14_상속복습;
//Hap 과 Gop 클래스의 공통점 a,b
abstract class Common {
	int a,b;
	public abstract void out();
}

class Hap extends Common{

	@Override
	public void out() {
		System.out.println(a+"+"+b+"="+(a+b));
	}
}
//곱의 결과
class Gop extends Common{

	@Override
	public void out() {
		System.out.println(a+"*"+b+"="+(a*b));
	}
}
//메인클래스
public class MainClassTest {
	public static void main(String[] args) {
		Hap hh = new Hap();
		Gop gg = new Gop();
		hh.a=12;
		hh.b=34;
		hh.out();
		gg.a=20;
		gg.b=44;
		gg.out();
	}
}

package day14_상속예제;

public class MyDTO extends Mysample{
	//이름,나이,거주지를 입력받아 출력하는 프로그램을 생성
	
}

 

package day14_상속예제;

public class Mysample {
	String name;//이름
	int age;//나이
	String address;//주소
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public Mysample() {}
	public Mysample(String name, int age, String address) {
		super();
		this.name = name;
		this.age = age;
		this.address = address;
	}
}

 

package day14_상속예제;

import java.util.Scanner;

public class Input {
	public String munja(String str , Scanner sc) {
		System.out.print(str+"를 입력");
		return sc.next();
	}
	public int number(String str, Scanner sc) {
		System.out.print(str+"를 입력");
		return sc.nextInt();
	}
}

 

package day14_상속예제;

public interface Myout {
	public void out();
}

 

package day14_상속예제;

import java.util.ArrayList;
import java.util.Scanner;

public class MyDAO implements Myout{
	ArrayList<MyDTO> list = new ArrayList<MyDTO>();
	public void insert() {
		Input in = new Input();
		MyDTO dto = new MyDTO();
		Scanner sc = new Scanner(System.in);
		dto.setName(in.munja("이름", sc));
		dto.setAge(in.number("나이", sc));
		dto.setAddress(in.munja("주소", sc));
		list.add(dto);
	}
	@Override
	public void out() {
		System.out.println("이름\t나이\t주소");
		System.out.println("=====================");
		for(MyDTO aa:list) {
			System.out.print(aa.getName()+"\t");
			System.out.print(aa.getAge()+"\t");
			System.out.println(aa.getAddress());
		}
	}
}

 

package day14_상속예제;

import java.util.Scanner;

public class Mymenu {

	public static void main(String[] args) {
		Scanner sc =new Scanner(System.in);
		MyDAO dao = new MyDAO();
		while(true) {
			System.out.println("1.입력");
			System.out.println("2.출력");
			System.out.println("0.종료");
			System.out.print("작업할 번호를 입력");
			int q = sc.nextInt();
			switch (q) {
			case 0:System.exit(0);;
			case 1:dao.insert();break;
			case 2:dao.out();break;
			}
		}
	}
}

package day14_상속예제;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class MyDAO implements Myout{
	ArrayList<MyDTO> list = new ArrayList<MyDTO>();
	public void insert() throws IOException {
		FileReader fr = new FileReader("c:/data/1222.txt");
		BufferedReader br = new BufferedReader(fr);
		while(true) {
			MyDTO dto = new MyDTO();
			String str = br.readLine();
			if(str==null)break;
			dto.setName(str.substring(0, 3));
			dto.setAge(Integer.parseInt(str.substring(3, 6)));
			dto.setAddress(str.substring(6));
			list.add(dto);
		}
	}
	@Override
	public void out() {
		System.out.println("이름\t나이\t주소");
		System.out.println("=====================");
		for(MyDTO aa:list) {
			System.out.print(aa.getName()+"\t");
			System.out.print(aa.getAge()+"\t");
			System.out.println(aa.getAddress());
		}
	}
}

 

package day14_상속예제;

import java.io.IOException;
import java.util.Scanner;

public class Mymenu {

	public static void main(String[] args) throws IOException {
		Scanner sc =new Scanner(System.in);
		MyDAO dao = new MyDAO();
		while(true) {
			System.out.println("1.입력");
			System.out.println("2.출력");
			System.out.println("0.종료");
			System.out.print("작업할 번호를 입력");
			int q = sc.nextInt();
			switch (q) {
			case 0:System.exit(0);;
			case 1:dao.insert();break;
			case 2:dao.out();break;
			}
		}
	}
}

package day14_Exception;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class ExceptionTest {

	public static void main(String[] args) throws IOException {
		//두수를 입력받아 몫과 나머지를 구한..자료5 파일처리
		FileReader fr = new FileReader("c:/data/12222.txt");
		BufferedReader br = new BufferedReader(fr);
		while(true) {
			try {
				String str = br.readLine();
				if(str==null)break;
				int a = Integer.parseInt(str.substring(0, 2));
				int b = Integer.parseInt(str.substring(2, 4));
				int mok = a/b;
				int na = a%b;
				System.out.println(a+"/"+b+" 몫 : "+mok+" 나머지 : "+na);
			}
			catch(NumberFormatException e) {
				System.out.println("오류내용 : 문자가 들어감");
			}
			catch(ArithmeticException e) {
				System.out.println("오류내용 : 0으로 나눔");
			}
			catch(Exception e) {
				System.out.println("오류내용 : 알수없음");
			}
		}
	}
}

 

package day14_상속예제;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class MyDAO implements Myout{
	ArrayList<MyDTO> list = new ArrayList<MyDTO>();
	public void insert() throws FileNotFoundException  {
		FileReader fr = new FileReader("c:/data/1222.txt");
		BufferedReader br = new BufferedReader(fr);
		while(true) {
			MyDTO dto = new MyDTO();
			String str;
			try {
				str = br.readLine();
				if(str==null)break;
				dto.setName(str.substring(0, 3));
				dto.setAge(Integer.parseInt(str.substring(3, 6)));
				dto.setAddress(str.substring(6));
				list.add(dto);
			} 
			catch (IOException e) {
				e.printStackTrace();
			}
			catch (NumberFormatException e) {
				System.out.println("숫자 대신 문자가 들어감");
			}
			catch (Exception e) {
				System.out.println("알수없는 에러 발생");
			}
		}
	}
	@Override
	public void out() {
		System.out.println("이름\t나이\t주소");
		System.out.println("=====================");
		for(MyDTO aa:list) {
			System.out.print(aa.getName()+"\t");
			System.out.print(aa.getAge()+"\t");
			System.out.println(aa.getAddress());
		}
	}
}

'JAVA' 카테고리의 다른 글

221226_배열  (0) 2022.12.26
221223_상속  (0) 2022.12.23
221221_Interface,상속  (0) 2022.12.21
221220_Interface  (0) 2022.12.20
221219_Class  (0) 2022.12.19

댓글