CS Study/Java

6. 패키지 / 접근 제한자 / 상속

Ryannn 2022. 4. 7. 14:30

1. 직원의 연봉 입력하기 - 객체 활용(인스턴스 변수, 인스턴스 메소드)

import java.util.Scanner;

public class payment {
	
	private String name;
	private int pay;
	
	private static int number = 0;
	
	
	public payment(String m,int s) {
		name = m;
		pay = s;
		++number;
	}
	public int getCount() {
		return number;
	}
}

public class 과제 {
	public static void main(String[] args) {
		final int max_number = 3;
		payment [] payment = new payment[max_number];
		Scanner scan = new Scanner(System.in);
		for (int i=0;i<payment.length;i++) {
			System.out.println("이름 입력>>");
			String m = scan.next();
			if (m.equals("끝")) {
				break;
			}
			System.out.println("연봉>>");
			int s = scan.nextInt();
			payment[i] = new payment(m,s);
			int num = payment[i].getCount();
			System.out.println("현재 직원 수 :"+num);

		}
		scan.close();
	}

}

2. 책의 제목, 저자 출력 - 패키지 활용

public class Book {
	String title;
	String author;
	
	void show() {
		System.out.println(title+":"+author);
	}
	public Book() {
		this("","");
		
	}
	public Book(String title) {
		this(title,"작자미상");
	}
	public Book(String title, String author) {
		this.title = title;
		this.author = author;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Book emptyBook = new Book();
		emptyBook.show();
		Book loveStory = new Book("춘향전");
		loveStory.show();
		Book littlePrince = new Book("어린왕자","생텍쥐페리");
		littlePrince.show();

	}

}

3. protected 접근 제한자 활용

public class StaticSample {

		// TODO Auto-generated method stub
	public int n;
	static public int m;
	protected void g() {
		m= 20;
	}
	protected void h() {
		m=30;
	}
	static protected void f() {
		m=5;
	}

	

}

public class StaticSampleTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		StaticSample S1, S2;
		S1 = new StaticSample();
		S1.n = 5;
		S1.g();
		StaticSample.m = 50;
		System.out.println(StaticSample.m);
		S2 = new StaticSample();
		S2.n = 8;
		S2.h();
		StaticSample.f();
		System.out.println(StaticSample.m);

	}

}

4. private 접근 제한자 활용

import java.util.Scanner;
class Currency {
	private static double rate;
	public static double toDollar(double won) {
		return won/rate;
	}
	public static double toKWR(double dollar) {
		return dollar*rate;
	}
	public static void setRate(double r) {
		rate = r;
	}
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("환율(1달러)>> ");
		double rate = scanner.nextDouble();
		Currency.setRate(rate);
		System.out.println("원화 입력>> ");
		int won = scanner.nextInt();
		System.out.println("$"+ Currency.toDollar(won));
		System.out.println("달러 입력>> ");
		int dol = scanner.nextInt();
		System.out.println(Currency.toKWR(dol)+"원");
		
	}
}

5. 점의 좌표, 색 출력하기 - 상속 활용

class ColorPoint extends Point {
	private String color; // 점의색
	public void setColor(String color) {
		this.color = color; 
	}
	public void showColorPoint() {
		System.out.print(color);
		showPoint(); 
	}
}

class Point {
	private int x, y; // 점의 x, y 좌표
	public void set(int x, int y) {
		this.x = x;
		this.y = y; 
	}
	public void showPoint() { // 점의좌표출력
		System.out.println("("+x+","+y+")"); 
	}
}


public class Test {
	public static void main(String[] args) {
		Point p = new Point(); // Point 객체 생성
		p.set(1, 2); // Point 클래스의 set() 호출
		p.showPoint();
		ColorPoint cp = new ColorPoint(); // ColorPoint 객체
		cp.set(3, 4); // Point의 set() 호출
		cp.setColor("red"); // ColorPoint의 setColor() 호출
		cp.showColorPoint(); // 컬러와 좌표 출력 
	} 
}

6. 상속의 관계 파악

class Shape {
	public Shape(String msg) {
		System.out.println("Shape() : " + msg);
	}
}
public class Rectangle extends Shape {
	public Rectangle(String msg){
		super("from Rectangle!"); // 명시적인 호출 (생성자 첫 줄!)
		System.out.println("Rectangle() : " + msg);
	}
}
public class BankTest {
	public static void main(String[] args) {
		Rectangle r = new Rectangle("from main!");
	}
}