#오늘 주제 : 패키지 package


#패키지를 만들때 이름은 전세계에 하나 뿐인 이름으로 만들어야한다.


예시

프로젝트 이름 :com.lg.it.weapon


workspace폴더가보면 com폴더\lg폴더\it폴더 안에 weapon 폴더가 생성된다.



#############


패키지


클래스


클래스, 멤버변수, 메서드(생성자 포함)



접근지정자 4개


1.public 

프로젝트 내에서 어디서든 접근이 가능


2.protected

같은 패키지 내에서 가능하고

다른 패키지에서는 상속했을때만 가능


3.default ( 앞에 아무것도 안쓰면 default )

같은 패키지 내에서만 접근이 가능

다른 패키지에서이 접근은 제한


4.private

같은 클래스내에서만 접근이 가능



################

<상속>


부모(조상) 클래스 (슈퍼클래스)를 상속받는다.


공통적인 요소를 모아서 새로운 클래스를 만들고 상속.


서로 다른 클래스를 관계를 맺어주는 것.★


멤버변수, 멤버메서드 상속가능.


생성자는 상속 받을 수 없다.


####


A class B class

기사    무기

A is a B //기사가 무기냐?

A has a B //기사가 무기를 가지고 있다.



A가 B를 가지고 있을때

class A{

B b;


}


A가 B일때

class A extends B{



}


####


부모로 부터 받은 메서드를 자기 클래스에 맞게 재정의 하는 것  : 오버라이딩 


overRiding


메서드의 선언부는 같아야 함.

{ 내용만 바꾸자 }


public void parent(){ }

                    ㄴ여기까지는 같아야한다.


public void parent(){ //내용  }  : 오버라이딩 ( 내용만 변경)



public void parent(int a){  } : 오버로딩 (매개변수이용)


#####


쓰지않으면 


public void parent(){  } 


이 부분은 안보이지만 자동으로 생성되어 있다.  


################


아무 것도 쓰지 않고


public void parent(int a){  } : 먼저쓰면 오버로딩 왜냐면 public void parent(){  } 가 안보이지만 생성되어있음.


##########################################


로그인 후 성적입력

##

내가한거


#메인클래스

package loglogin;


public class LogMain {

//메인클래스

public static void main(String[] args) {

Teach t = new Teach();

t.start(); //Teach클래스의 start메소드 실행

}


}



#학생정보클래스
package loglogin;

public class StuData {
//국,영,수
//5명
//모든 학생의 총점, 평균 한번에
public int id, kor, eng, math;
public int total;
public int mean;

public int getTotal() {
return total;
}

public void setTotal() {
this.total = kor+eng+math;
}

public int getMean() {
return mean;
}

public void setMean() {
this.mean = total/3;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public int getKor() {
return kor;
}

public void setKor(int kor) {
this.kor = kor;
}

public int getEng() {
return eng;
}

public void setEng(int eng) {
this.eng = eng;
}

public int getMath() {
return math;
}

public void setMath(int math) {
this.math = math;
}

}


#로그인클래스
package loglogin;

import java.util.Scanner;

public class Login {
//로그인클래스
public int id = 1234;
public int pw = 1234;
Scanner sc;
public void log(){
sc = new Scanner(System.in);
while(true){
System.out.println("ID를 입력하세요.");
int inputid = sc.nextInt();
System.out.println("PW를 입력하세요.");
int inputpw = sc.nextInt();
if(id==inputid && pw==inputpw){
System.out.println("로그인 성공");
Teach t = new Teach();
t.menu(); //로그인 성공하면 Teach클래스의 menu메소드 실행
}else{
System.out.println("로그인 실패, 다시 입력하세요.");
}
}
}
}


#선생님 클래스
package loglogin;

import java.util.Scanner;

public class Teach {
Scanner sc;
StuData []sar;
public void start(){
Login l = new Login();
l.log(); //Login클래스의 log메소드 실행
}

public void menu(){
sc = new Scanner(System.in);
System.out.println("1.성적입력 2.성적확인 3.종료");
int select = sc.nextInt();
if(select==1){
//성적입력메소드
input();
}else if(select==2){
//성적확인메소드
getInfo();
}else{
//종료메소드
endprg();
}
}
public void input(){
//성적입력메소드
StuData s;
sar =new StuData[5];
for(int i=0;i<sar.length;i++){
s = new StuData();
System.out.println("번호입력");
s.id=sc.nextInt(); //입력방식 둘다 가능
System.out.println("국어점수입력");
s.kor=sc.nextInt();
System.out.println("영어점수입력");
s.setEng(sc.nextInt());
System.out.println("수학점수입력");
s.setMath(sc.nextInt());
s.setTotal();
s.setMean();
sar[i]=s;
}
menu();
return;
}

public void getInfo(){
//성적확인메소드
for(int k=0;k<sar.length;k++){
System.out.println((k+1)+"번 학생 총점:"+sar[k].getTotal()+"평균:"+sar[k].getMean());
}
}
public void endprg(){
//종료메소드
System.out.println("종료하겠습니다.");
}
}



###

같은 클래스의 메소드 불러올때 클래스이름의 데이터타입으로 된 변수선언 부분을 삭제하니
스캐너 오류가 사라짐 >> 이 부분 공부


##################################################

선생님이 한거

#메인
public class Smain {

public static void main(String[] args) {
Login l = new Login();
Teacher t = new Teacher();
boolean check =l.menu();
t.start(check);

}

}


#로그인
import java.util.Scanner;

public class Login {
int id;
int password;
Scanner sc;
boolean check;
public Login(){
id=1234;
password= 1234;
sc= new Scanner(System.in);
check = true;
}
public boolean menu(){
while(check){
System.out.println("1. 로그인");
System.out.println("2. 종료");
int select = sc.nextInt();
if(select == 1){
loginCheck();
}else {
System.out.println("프로그램 종료 ");
break;
}
}
return !check;
}
public void loginCheck(){
System.out.println("ID를 입력");
int yId = sc.nextInt();
System.out.println("비번입력");
int pw = sc.nextInt();
if(yId==id && pw==password){
System.out.println("로그인 성공");
check = false;
}else {
System.out.println("아이디나 비번 확인 해라");
}
}

}


#티처
public class Teacher {
public void start(boolean check){
if(check){
System.out.println("1. 학생정보 입력");
System.out.println("2. 학생정보 출력");
}
}

}



############################################################


#은행예제 연습


#메인클래스

package bankbank;


public class BankMain {

public static void main(String[] args) {

Log l=new Log();

l.log();


}


}



#로그인클래스
package bankbank;

import java.util.Scanner;

public class Log {
int id;
int pw;
Scanner sc;
public void log(){
id=1234;
pw=1234;
sc = new Scanner(System.in);
System.out.println("1.로그인 2.종료");
int select = sc.nextInt();
if(select==1){
while(true){
Account a = new Account();
System.out.println("1. ID 입력");
int inputid = sc.nextInt();
System.out.println("2. PW 입력");
int inputpw = sc.nextInt();
if(inputid==id && inputpw==pw){
System.out.println("로그인 성공");
a.menu();
}else{
System.out.println("로그인 실패.다시입력하세요.");
}
}
}else{
System.out.println("프로그램을 종료합니다.");
//종료
}
}
}


#계좌 클래스
package bankbank;

import java.util.Scanner;

public class Account {
int balance=100000;
int deposit;
int withdrawal;
Scanner sc;
public void menu(){
sc=new Scanner(System.in);
while(true){
System.out.println("1.입금 2.출금 3.계좌이체 4.종료");
int select = sc.nextInt();
if(select==1){
//입금 메서드
godeposit();
}else if(select==2){
//출금 메서드
gowithdrawal();
}else if(select==3){
//계좌이체
trans();
}else{
//종료
}
}
}
public void godeposit(){
System.out.println("현재 잔액"+balance+"원 입니다.");
System.out.println("입금액을 입력해주세요.");
deposit = sc.nextInt();
System.out.println("입금 후 잔액은"+(balance+deposit)+"원입니다.");
}
public void gowithdrawal(){
System.out.println("현재 잔액"+balance+"원 입니다.");
System.out.println("출금액을 입력해주세요.");
withdrawal = sc.nextInt();
if(withdrawal<=balance){
System.out.println("출금 후 잔액은"+(balance-withdrawal)+"원입니다.");
}else{
System.out.println("잔액이 부족합니다.");
}
}
public void trans(){
System.out.println("이체하실 계좌번호를 입력하세요.");
int tracc = sc.nextInt();
System.out.println("이체 금액을 입력하세요.");
int trmoney = sc.nextInt();
if(trmoney<=balance){
System.out.println("이체 계좌번호"+tracc);
System.out.println("출금 후 잔액:"+(balance-trmoney));
}else{
System.out.println("잔액이 부족합니다.");
}
}

}










블로그 이미지

테시리

,