
Override
๐ ๋ฎ์ด์ด๋ค -> ์์ / ์ธํฐํ์ด์ค์ธ ๊ฒฝ์ฐ์๋ง ๊ฐ๋ฅํ๋ค.
overload
๐ ํ ํด๋์ค ๋ด์์ ์ด๋ฆ์ด ๋๊ฐ์ ๋ฉ์๋๊ฐ ๋งค๊ฐ๋ณ์์ ๊ฐ์๊ฐ ๋ค๋ฅธ ๋ฉ์๋.
์์ฑ์ ์ค๋ฒ๋ก๋ฉ
๐ ์์ฑ์๊ฐ name๊ณผ age๊ฐ ์์๋ ๋๊ฐ์ ์์ฑ์๋ฅผ ๋ง๋ค์ด์ name์ ์์ฑ์๋ง ์ด๊ธฐํ ํ๋๊ฒ.
๐ ํด๋์ค์์ ์ฌ์ฉ : ์์์ด ๊ธ์ง๋จ -> ์ปดํ์ผ๋ฌ๊ฐ ์ฐ์ ์ ์ผ๋ก ์ฐพ์ผ๋ ์ฑ๋ฅ ํฅ์
๐ ๋ณ์์์ ์ฌ์ฉ : ๋ณ์์์ ๋ด์ฉ๋ฌผ์ ๋ค๋ฅธ๊ณณ์์ ๋ณ๊ฒฝ์ด ๋ถ๊ฐ๋ฅ
๐ ๋ฉ์๋์์ ์ฌ์ฉ : ํ์ํด๋์ค์์ override ๋ถ๊ฐ๋ฅ
LifeCycle Method
๐ ๊ฐ์ฒด๊ฐ ์์ฑ๋๊ณ ์ฌ์ฉ๋๋ฉฐ ์๋ฉธํ๋ ๊ณผ์
๐ heap์์ต์ ํ ๋น, ํธ์ถ๋์ด ์ฌ์ฉ, GC์ ์ํด ์ ๊ฑฐ
์๋ฃ๊ตฌ์กฐ Method
๐ equals Method : ๊ฐ์ฒด์ ๋๋ฑ์ฑ ๋น๊ต
๐ hashCode Method : ํด์์ฝ๋๋ฅผ ๋ฐํํ์ฌ ํด์๊ธฐ๋ฐ ์ปฌ๋ ์
์์ ๊ฐ์ฒด ์ ์ฅ ๋ฐ ๊ด๋ฆฌ
๐ toString Method : ๊ฐ์ฒด์ ์ ๋ณด๋ฅผ ๋ฌธ์์ด๋ก ์ถ๋ ฅ
๐ ํ ํด๋์ค์ abstract ๋ฉ์๋๊ฐ ์์ผ๋ฉด ํด๋น ํด๋์ค๋ abstract class๋ก ๋ง๋ค์ด์ค์ผํจ.
๐ ํ์ ํด๋์ค์์๋ ํด๋น ๋ฉ์๋ ์์์ ๊ฐ์ ํ๋ค.
๐ ์ถ์ ๋ฉ์๋๋ ํ์ํด๋์ค์์ ์ค๋ฒ๋ผ์ด๋ ํด์ผํ๋ private์ ์ธ์ ์ค๋ฅ๋ฐ์.
๐ ์ถ์ํด๋์ค๋ ์ธ์คํด์ค,๊ฐ์ฒด๋ฅผ ์ง์ ์ ์ผ๋ก ์์ฑํ ์ ์์.
- CommonUI
public abstract class CommonUI {
private Scanner scanner;
public CommonUI(Scanner scanner) {
this.scanner = scanner;
}
public abstract void execute();
public void print(String msg){
System.out.println(msg);
}
protected String inputStr(String msg){
print(msg);
return scanner.nextLine();
}
protected int inputInt(String msg){
return Integer.parseInt(inputStr(msg));
}
}
๐ execute๋ผ๋ ์ถ์๋ฉ์๋๋ฅผ ์์ฑํ์ฌ CommonUI๋ ์ถ์ํด๋์ค๋ก ๋ณ๊ฒฝ๋จ
๐ execute ์ถ์๋ฉ์๋๋ฅผ ํ์ ํด๋์ค์์ ๋ฐ๋์ overrideํ์ฌ ๋คํ์ฑ์ ์ด๋ฃธ.
๐ print, inputStr, inputInt ๋ฉ์๋๋ฅผ ์์ฑํ ๋ค๋ฅธ ํด๋์ค์์ ์์ํ์ฌ ์ฝ๋์ ์๋ฅผ ์ค์.
- CreateUI
public class CreateUI extends CommonUI{
public CreateUI(Scanner scanner) {
super(scanner);
}
@Override
public void execute() {
print("Welcome to Pizza Creation UI");
}
}
๐ CommonUI๋ฅผ extendsํ๊ณ , Scanner๋ฅผ super, execute๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉํ๋ค.
- TotalUI
public class TotalUI extends CommonUI{
private CommonUI[] uiArr;
public TotalUI(Scanner scanner, CommonUI[] uiArr) {
super(scanner);
this.uiArr = uiArr;
}
@Override
public void execute() {
int oper = inputInt("0:create \t 1:read \t 2:update \t 3:delete \t -1:EXIT");
if(oper == -1){
return;
}
uiArr[oper].execute();
execute();//recursive
}
}
๐ public Static final
๐ ์ค์ ์ฝ๋์์๋ public, static, final์ ๋ํ ์ฝ๋๊ฐ ์์ง๋ง, interface๋ฅผ ์ฌ์ฉํ๊ฒ ๋๋ฉด ์๋ฌต์ ์ผ๋ก interfaceํ๋์ ์๋ ๋ณ์๋ public, static, final์ ํด๋น๋๋ค.
๐ Interface๋ ์ค๊ณ๋ฅผ ์ํจ์ผ๋ก ๋ค๋ฅธ ํด๋์ค์์ ๊ผญ ์ฌ์ฉํด์ผํ๋ ๋ณ์๊ฐ ์์ผ๋ฏ๋ก public, ํด๋์ค๋ณ์์ฒ๋ผ ์ฌ์ฉ์ด ๋๋ฏ๋ก static, ์ ํด์ง ๋ณ์๋ ๋ณํ์ง ์๊ณ ์์๋ก ์ฌ์ฉ๋์ด์ผ ํ๋ฏ๋ก final์ด๋ค.
- CalcSalary
public interface CalcSalary {
String getName();
double calcMonth();
}
- ContractWorker
public class ContractWorker implements CalcSalary {
private double yearPay;
private String name;
public ContractWorker(String name, double yearPay) {
this.yearPay = yearPay;
this.name = name;
}
@Override
public String getName() {
return name;
}
@Override
public double calcMonth() {
return (yearPay/12) * 0.65; //์๊ธ๊ณ์ฐ
}
}
- freelenser
public class Freelancer implements CalcSalary{
private String name;
private int month;
public Freelancer(String name, int month) {
this.name = name;
this.month = month;
}
@Override
public String getName() {
return name;
}
@Override
public double calcMonth() {
return month*0.967; //์ธ๊ธ์ ์ธ
}
}
- Parttimer
public class PartTimer implements CalcSalary{
private int hourPay;
private int time;
private String name;
public PartTimer(String name, int hourPay, int time) {
this.hourPay = hourPay;
this.time = time;
this.name = name;
}
@Override
public String getName() {
return name;
}
@Override
public double calcMonth() {
return hourPay * time; //์๊ธ๊ณ์ฐ
}
}
- Main
public class SalaryMain {
public static void main(String[] args) throws FileNotFoundException {
File file = new File("list.txt"); //ํ์ผ์ด๊ธฐ ๊ฐ์ฒด
Scanner scanner = new Scanner(file);
java.util.List<CalcSalary> empList = new ArrayList<CalcSalary>();
while (scanner.hasNextLine()) { //๋ค์์ ์ฝ์ ๋ฌธ์์ด์ด ์๋์ง
String line = scanner.nextLine(); //๋ค์ ํ ์ค ์ฝ๊ธฐ
System.out.println(line);
//split - ๋ฐฐ์ด๋ก
String[] arr = line.split(","); //ํด๋น ์ค์ , ์ผ๋ก ๊ตฌ๋ถํ์ฌ ๋ฐฐ์ด์ ๋ฃ๊ธฐ
if(arr[0].equals("A")){ //์ด ์ง์ ํํธํ์ด๋จธ
empList.add(new PartTimer( arr[1], Integer.parseInt(arr[2]), Integer.parseInt(arr[3])));
}else if(arr[0].equals("C")){
empList.add(new ContractWorker( arr[1], Integer.parseInt(arr[2])));
}else if(arr[0].equals("F")){
empList.add(new Freelancer(arr[1], Integer.parseInt(arr[2])));
}
}//while true
scanner.close(); //ํ์ผ ์ฐ๊ฒฐ ๋
System.out.println("--------------------------------");
empList.forEach( emp -> System.out.println( emp.getName()+": " + emp.calcMonth()) );
//list.txt
//A, Messi,9980,95
//A, Ronaldo,9980,60
//C, Neymar,50000000
//C, Neymar1,50000000
//C, Neymar2,50000000
//F, SON,5600000
}
}
- tomcat์๋ฒ ์ฐ๊ฒฐํ๊ธฐ
1. https://tomcat.apache.org/ ์์ tomcat10 download ํ๊ธฐ
2. 10.1.26 zip file ์ฐพ๊ธฐ ์ฌ์ด๊ณณ์ ์์ถํ๊ธฐ
3. ์ธํ
๋ฆฌ์ ์ด new project โ jakarta ee โ template (web application) โ gradle โ application server โ new โtomcat server โ tomcat home( tomcatํด๋ bin ๋ฐ๋ก ์ ๊น์ง) โ servlet ํ์ธํ creat โ tomcat ์ ํ config โ deployment โ run
4. ์์ tomcat๋ค์ ์ ํ โ deployment โ ๊ธฐ์กด์์๋๊ฑฐ -๋ก ์ง์ฐ๊ณ โ newํด์ exploded ์์ฑ โ application context๋ / ๋ง ๋จ๊ฒจ๋์ ํธํ๊ฒ ํ๋ค.
5. tomcat์์ serverํญ ๋๋ฅด๊ณ update action์ update classes and resources ๋๊ฐ๋ค ๋ณ๊ฒฝํด์ค.
๐ Scanner ์ด์ฉํด์ file๋ฐ์ดํฐ๋ฅผ ๋ผ์ธ๋จ์๋ก ์ฝ์ ์ ์๋ค.
ํด๋น ๋ด์ฉ์ ๋ชจ๋ ์๋ฌธ์๋ก ๋ณ๊ฒฝํด์, ๊ฐ ์ํ๋ฒณ์ด ๋ช๋ฒ์ฉ ๋์ค๋์ง ์๊ณ ๋ฆฌ์ฆ๋ฌธ์ .
- CharCounter
public class CharCounter {
private int count;
public int getCount(){
return count;
}
public void inc(){
count++;
}
}
- main
public class ReadingFile2 {
public static void main(String[] args) throws FileNotFoundException {
CharCounter charcounter = new CharCounter();
File file = new File("Alpha.txt"); //ํด๋น ํ์ผ์ด๊ธฐ
Map<Character, Integer> map = new HashMap<>(); //map๊ฐ์ฒด ์์ฑ, key = ๋ฌธ์ value = ์ ์
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) { //๋ค์ ๋ฌธ์์ด์ด ์๋์ง ํ์
ํ๋ฉด์ while
String line = scanner.nextLine().toLowerCase(); //ํด๋น ์ค์ ๋ฌธ์๋ฅผ ์๋ฌธ์๋ก ๋ณ๊ฒฝ
for (char c : line.toCharArray()) { // ๋ฌธ์์ด line์ ๋ฌธ์ ๋ฐฐ์ด๋ก ๋ณํ
int ascii = (int) c; //์์คํค์ฝ๋๋ก ๋ณํํ๊ธฐ
if (ascii < 97 || ascii > 122) { //a= 97 ,z = 122
continue;
}
map.put(c, map.getOrDefault(c, 0) + 1); //๋ง์ฝ ๊ฐ์ด ์์ผ๋ฉด ์ ์ฅํ๊ณ ์๋๋ฉด 0
charcounter.inc();
}
}
scanner.close();
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}