public class Test {

//변수선언 부분

//기본데이터타입 : char, int, long, float, double, boolean

//레퍼런스데이터 타입 : String str = "suji"; ,그외 프로그래머가 만든 클래스

//명사적 개념 

//멤버변수

//메소드 선언 부분

//접근지정자 리턴타입 메소드명(매개변수){}

//동사적 개념

//멤버메소드

//class는 설계도(실체화되지 않은 것), 하나의 레퍼런스 데이터 타입

//생성자(constructor, 메소드와같음), 일반멤버메소드와의 차이는 

//객체 즉 인스턴스를 만들기 위해 딱 한번 호출되는 특수한 메소드

//생성자가하는일:멤버변수를 초기화 or 객체가 만들어질때 해야 되는 일을 기술

//class 내에 생성자가 없으면 컴파일러가 자동으로 하나 만들어 줌

//public Test(){} -> 기본생성자, 빈생성자(매개변수x), 디폴트 생성

//접근지정자, 클래스명과 동일한 이름,(매개변수){}

//빈생성자는 멤버변수를 각 데이터에 맞게 기본값으로 초기화

int age; //0으로 초기화

String name; //null으로 초기화

public void prn(){

System.out.println("age"+age);

System.out.println("name"+name);

}

}


###


메인메소드를 만든다

 //age와 name은 아직 하드에 파일로만 존재.

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

public class TestMain {


public static void main(String[] args) {

Test t;


}


}



##########

다른 클래스에 메인메소드를 만들고 메인 메소드 안에서 Test를 호출해주면

변수 t는 stack에 저장된다.


이때 t는 주소값을 갖는다. 주소값을 갖는 변수는 4바이트.


왜그럴까?


#######

new로 heap에 저장후 t를 출력해보자

#######

public class TestMain {


public static void main(String[] args) {

Test t = new Test();

System.out.println(t);


}


}


#######

결과창


java_0722.Test@659e0bfd


#######
659e0bfd <<이 부분이 주소값, 4바이트를 차지한다.

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

public class TestMain {

public static void main(String[] args) {
Test t ;
}

}

#######
여기서 t는 stack에 저장

#######

new : heap 영역에 만들어라


#######

public class TestMain {

public static void main(String[] args) {
Test t = new Test();
t.age=20;
}

}

#######

Test t = new Test();  // new 생성자 를 통해서 heap에 저장

t는 주소값을 갖는다.  heap에서 저장된 주소값을 t에 저장하고 stack에서 사용?
t > 시작점 주소값 


주소값을 외우기 힘드므로 변수명 t에 주소값을 저장해서 사용한다.

#######

public class TestMain {

public static void main(String[] args) {
Test t = new Test();
t.age=20;
Test t2 = new Test();
Test [] ar = new Test[2];
ar[0]=t;
ar[1]=t2;
}

}


###
t 시작점 주소값
t2 시작점 주소값
###
Tset타입의 배열을 담을 수 있는 ar  > 4바이트를 마련하고 시작점 주소값을 ar이라고 선언.

그리고 new  > Test타입을 heap 에다가 저장

###


heap에 t의 시작주소값을 a라하고 t2의 시작주소값을 b라고 하자.

heap에 ar의 시작 주소값이 저장되고



stack에 a의 주소값이 저장되고 b의 주소값이 저장된다.

stack에 ar의 시작주소값이 저장된다.


ar[0]=t;
ar[1]=t2;

여기서 stack에 저장된 t의 시작주소값 a를 heap에 저장된 ar 배열의 첫번째 칸에
stack에 저장된 t2의 시작주소값 b를 heap에 저장된 ar배열의 두번째 칸에 저장


###


클래스를 만드는이유


(필요하면 꺼내쓰고 필요없으면 안쓰기 위해서)


ex) 프로토스 스카우트 하늘의제왕 ㅋㅋ



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

은행 프로그램


#메인클래스

import java.util.Scanner;


public class BankMain {


public static void main(String[] args) {

Scanner sc= new Scanner(System.in);

System.out.println("이름을 입력하세요.");

String name = sc.next();  //iu

System.out.println("입금액을 입력하세요.");

int balance = sc.nextInt(); //1000;

Account suji = new Account();

Account iu = new Account();

iu.name = name; //입력받은 값 사용

iu.balance = balance; //입력받은 값 사용

iu.id = "J102-58-96";

suji.name="suji"; //수지는 직접입력

suji.balance = 20; //수지는 직접입력

suji.id = "S103-55-99";

Deposit d = new Deposit();

d.input(iu); //메소드호출,인자값에 아이유입력

}


}



###

iu 는 stack에 저장되어 있고 그 값은 heap에 저장?





#계좌 클래스

public class Account {

String id;

String name;

int balance;

}


#이체 클래스

public class Deposit {

public void input(Account t){//메소드선언, 메소드정의

System.out.println(t.balance);

}

}



#####

매개변수 자리에 Account 타입의 t를 입력

인자값에 iu를 입력하였으므로

public void input(Account t){

System.out.println(t.balance);

}

이 부분은 매개변수에 아이유의 값을 입력한 것과 같다.

Account t  = iu ;




######

반지름구하기


메인클래스 , 메소드


public static void main(String[] args) {

double n =d.cal(3);

System.out.println(n);

}


계산클래스


public double cal(int r){

double result = r*r*3.14;

System.out.println(result);

return result;

}



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

public static void main(String[] args) {

int r= 10;

d.cal(r);


}



#계산

public double cal(int r){

double result = r*r*3.14;

System.out.println(result);

}




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


온라인게임 


#다른 패키지에 작성

디폴트패키지:메인클래스

웨폰패키지:각 무기 클래스 (검, 활, 지팡이...)

pc패키지: 각 유저 클래스 (기사 궁수 마법사..)


##메인 클래스(디폴트 패키지)

import Weapon.Sword;

import pc.Knight;


public class TestMain {

public static void main (String [] args){

String name = "iu";

Knight iu = new Knight(name);

System.out.println(iu.getHp()); //기사 hp부르기

//iu.setName("iu");

System.out.println(iu.getName()); //기사 이름 부르기

System.out.println(iu.getS().getDamage()); //단검데미지

System.out.println(iu.getS().getName()); //단검 이름 불러오기

}

}



##기사클래스(pc패키지)
package pc;


import Weapon.Sword;

public class Knight {
private String name;
private int str ; //힘
private int def ; //방어력
private int hp ; //체력
private Sword s ; //데이터타입을 만들어줌

public Knight(){
str=10;
def=15;
hp=100;
s = new Sword();
}
public Knight(String name){
this.name = name;
str=10;
def=15;
hp=100;
s = new Sword();
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getStr() {
return str;
}

public void setStr(int str) {
this.str = str;
}

public int getDef() {
return def;
}

public void setDef(int def) {
this.def = def;
}

public int getHp() {
return hp;
}

public void setHp(int hp) {
this.hp = hp;
}

public Sword getS() {
return s;
}

public void setS(Sword s) {
this.s = s;
}
}



##무기클래스 (weapon 패키지)

package Weapon;


public class Sword {

private String name;

private int damage;

private int lv;

public void totalDamage(){

int sum = damage + lv;

System.out.println(name+"의 공격력:"+sum);

}

public Sword(){

name="단검";

damage=10;

lv=0;

}


public String getName() {

return name;

}


public void setName(String name) {

this.name = name;

}


public int getDamage() {

return damage;

}


public void setDamage(int damage) {

this.damage = damage;

}


public int getLv() {

return lv;

}


public void setLv(int lv) {

this.lv = lv;

}

}




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


상속 : 기존의 클래스를 재사용해서 새로운 클래스를 작성하는 것.


###


package Weapon;


public class Sword extends Weapon{

//int lv;

//int damage;

//String name;

public Sword(){

name="단검";

damage=10;

lv=0;

}


//public void totalDamage(){

//

//}

}


###

package Weapon;

public class Weapon {
String name;
int damage;
int lv;
public void totalDamage(){
int sum = damage + lv;
System.out.println(name+"의 공격력:"+sum);
}
}



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


다른 클래스의 멤버(멤버변수 , 멤버메소드)를 사용하는 방법


1. 상속받기


2. 해당 클래스명(데이터타입) 변수명; 선언해주기(ex Sword s; //포함관계)

public class Knight extends Select {

String name;

int str ; //힘

int def ; //방어력

int hp ; //체력

Sword s ; //포함관계


//is s -> 상속

//has a -> 포함


}



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


Object 클래스  :  모든 클래스의 최고 조상






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

폰 프로그램



#메인클래스

public class Pmain {


public static void main(String[] args) {

Phone p = new Phone();

p.prn();

Phone p2 = new Phone("LG","white",10);

p2.prn();

}


}




#폰클래스
public class Phone {
String brand;
String color;
int price;
public Phone(){
brand="apple";
color="gold";
price=80;
}
public Phone(String brand, String color,int price){
this.brand=brand;
this.color=color;
this.price=price;
}
public void prn(){
System.out.println("brand"+brand);
System.out.println("color"+color);
System.out.println("price"+price);
}
}




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



메인클래스

public class Pmain {


public static void main(String[] args) {

Phone p2 = new Phone("LG","white",10);

p2.prn();

}


}


폰클래스

public class Phone {

String brand;

String color;

int price;


public Phone(String brand, String color,int price){

this.brand=brand;

this.color=color;

this.price=price;

}

public void prn(){

System.out.println("brand"+brand);

System.out.println("color"+color);

System.out.println("price"+price);

}

}



#카메라폰 

public class CameraPhone extends Phone{

int pixel;

public CameraPhone(){

super("lg","red",10);  //부모의 빈 생성자 호출

}

}




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


!!!overriding

//overriding!! : 상속관계에서만 가능, 자기클래스에 맞게 내용만 바꿔줌

선언부까지 무조건 같아야함/




//overload와 차이


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

메인클래스

public class Pmain {


public static void main(String[] args) {

Phone p2 = new Phone("LG","white",10);

p2.prn();

CameraPhone p3 =new CameraPhone();

p3.prn();

}


}


##

폰클래스


public class Phone {

String brand;

String color;

int price;


public Phone(String brand, String color,int price){

this.brand=brand;

this.color=color;

this.price=price;

}

public void prn(){

System.out.println("brand"+brand);

System.out.println("color"+color);

System.out.println("price"+price);

}

}




#카메라폰 클래스

public class CameraPhone extends Phone{
int pixel;
public CameraPhone(){
super("lg","red",10);  //부모의 빈 생성자 호출
brand="samsung";
color="white";
price=30;
pixel=300;
}
//overriding!! : 상속관계에서만 가능, 자기클래스에 맞게 내용만 바꿔줌
//overload와 차이
public void prn(){
System.out.println("pixel:"+pixel);
}


}



###오버로딩과 오버라이딩 차이점
오버로딩:기존에 없는 새로운 메소드를 정의하는 것(new)
오버라이딩:상속받은 메소드의 내용을 변경하는 것 (change)














블로그 이미지

테시리

,