위 글은 단순히 복습을 위한 목적으로 작성했으며, 자세한 설명은 없습니다.
package com.step5;
class sonata
{
int speed = 100;
}
public class Review1 {
public sonata p1(sonata a)
{
a = new sonata();
a.speed = 200;
return a;
}
public static void main(String[] args) {
Review1 rv = new Review1();
sonata sn = new sonata();
sn = rv.p1(sn);
System.out.println(sn.speed);
}
}
(2)
package com.step5;
class sonata
{
int speed = 100;
}
public class Review1 {
public sonata p1()
{
sonata a = new sonata();
a.speed = 200;
return a;
}
public static void main(String[] args) {
Review1 rv = new Review1();
sonata sn = new sonata();
sn = rv.p1();
System.out.println(sn.speed);
}
}
------------------------------------------
200
package com.step5;
class D
{
int i = 10;
}
public class Review10 {
void setMethodA(D d)
{
d.i = 200;
}
public static void main(String[] args) {
Review10 rv = new Review10();
D d = new D();
System.out.println(d.i);
rv.setMethodA(d);
System.out.println(d.i); // 원본 값이 바뀜
}
}
Ball[] balls = new Ball[200]; -> 전역변수로 선언
public Ball[] getBalls()
{
for(int i = 0; i < 200; i++)
{
int userInput = sc.nextInt();
balls[i] = new Ball(userInput);
}
}
package com.week3;
class S
{
public S(){ }
public S(int i){ }
public S(String s, int i){}
public S(int i, int j)
{
System.out.println(i + " " + j);
}
public S(boolean isOk)
{
System.out.println(isOk);
}
}
public class SangSungJa01 {
public static void main(String[] args) {
S s = new S(1,2);
System.out.println(s);
s = new S(true);
System.out.println(s);
// 두개는 다른 객체다.
}
}
---------------------------------------------------
1 2
com.week3.S@4e50df2e
true
com.week3.S@1d81eb93
static안에서는 this나 super 사용불가하다.
생성자에서는 this나 super 사용가능하다.
package com.week3;
class Glogic
{
int i = 1;
public Glogic(GView gv) {
/*
* gv = null
*/
System.out.println(gv);
}
}
public class GView {
public GView()
{
Glogic gl = new Glogic(this);
}
public static void main(String[] args) {
GView gv = new GView();
System.out.println(gv);
}
}
-------------------------------------------------------
com.week3.GView@4e50df2e
com.week3.GView@4e50df2e
package com.week3;
class Glogic
{
int i = 1;
public Glogic(GView gv) {
/*
* gv = null
*/
System.out.println(gv);
System.out.println(gv.gl_1);
}
}
public class GView {
int gl_1 = 40;
public GView()
{
Glogic gl = new Glogic(this);
}
public static void main(String[] args) {
GView gv = new GView();
System.out.println(gv);
}
}
-------------------------------------------------------
com.week3.GView@4e50df2e
40
com.week3.GView@4e50df2e
package com.week3;
class Glogic
{
int i = 1;
public Glogic(GView gv) {
/*
* gv = null
*/
System.out.println(gv);
}
}
public class GView {
public GView()
{
Glogic gl = new Glogic(this);
}
public static void main(String[] args) {
GView gv = new GView();
System.out.println(gv);
gv = new GView();
System.out.println(gv);
}
}
--------------------------------------
com.step5.GView@372f7a8d
com.step5.GView@372f7a8d
com.step5.GView@2f92e0f4
com.step5.GView@2f92e0f4
package com.week3;
class Glogic
{
int j = 1;
public Glogic(GView gv) {
/*
* gv = null
*/
System.out.println("========SangSungJa=====");
System.out.println("Glogic : " + gv);
System.out.println("Glogic. j :" +gv.j);
}
}
public class GView {
int j = 10;
public GView()
{
Glogic gl = new Glogic(this);
}
public static void main(String[] args) {
GView gv = new GView();
System.out.println(gv);
gv = new GView();
gv.j = 20;
System.out.println("============main=========");
System.out.println(gv);
System.out.println(gv.j);
}
}
----------------------------------------------------------------------------------------
========SangSungJa=================================
Glogic : com.week3.GView@4e50df2e
Glogic. j :10
com.week3.GView@4e50df2e
========SangSungJa=================================
Glogic : com.week3.GView@1d81eb93
Glogic. j :10
============main=================================
com.week3.GView@1d81eb93
20
package com.step5;
class Glogic
{
int j = 1;
public Glogic(GView gv) {
/*
* gv = null
*/
System.out.println("========SangSungJa=====");
System.out.println(this.j);
System.out.println("Glogic : " + gv);
System.out.println("Glogic. j :" +gv.j);
}
}
public class GView {
int j = 10;
public GView()
{
Glogic gl = new Glogic(this);
}
public static void main(String[] args) {
GView gv = new GView();
System.out.println(gv);
gv = new GView();
gv.j = 20;
System.out.println("============main=========");
System.out.println(gv);
System.out.println(gv.j);
}
}
---------------------------------------------
========SangSungJa=====
1
Glogic : com.step5.GView@372f7a8d
Glogic. j :10
com.step5.GView@372f7a8d
========SangSungJa=====
1
Glogic : com.step5.GView@2f92e0f4
Glogic. j :10
============main=========
com.step5.GView@2f92e0f4
20
여기서 this는 List2를 의미햔다. -> 제네릭 타입 자체가 다르다.(Customer)로 해야함
package com.step5;
import java.util.List;
import java.util.Vector;
class Customer extends Thread
{
}
public class List2 {
List<Customer> list = new Vector<>();
void methodA()
{
list.add(this);
/*
* 이 코드가 왜 틀린것인가?
* 여기서 this는 List2를 의미햔다.
*
*/
}
public static void main(String[] args) {
List2 list2 = new List2();
list2.methodA();
}
}
package com.step5;
import java.util.List;
import java.util.Vector;
class Customer extends Thread
{
}
public class List2 {
List<Customer> list = new Vector<>();
void methodA()
{
Customer cu = new Customer();
list.add(cu); // list.size() = 1
System.out.println(list.size()); // 0 -> 1로 증가된 상태
Customer cu2 = new Customer();
list.add(cu2); // list.size() = 2
System.out.println(list.size());
Customer cu3 = new Customer();
list.add(cu3);
System.out.println(list.size());
/*
* 이 코드가 왜 틀린것인가?
* 여기서 this는 List2를 의미햔다.
*
*/
}
public static void main(String[] args) {
List2 list2 = new List2();
list2.methodA();
}
}
package com.step6;
public class A {
int i = 1;
void methodB() {
System.out.println("==========[[methodB]]===========");
System.out.println(this);
System.out.println(this.i);
System.out.println("==========[[methodB]]===========");
}
void methodA() {
A a = new A();
System.out.println("========================");
System.out.println(a);
System.out.println(this);// 12번
// System.out.println(this.i);//1
}
public static void main(String[] args) {
A a1 = new A(); // i = 1
a1 = new A(); // i = 1
System.out.println(a1); // 23번 주소번지
a1.i = 2; // 23번 주소번지 + a.i = 2로 바뀜 (원본이 바뀜)
//a1.methodA(); //
a1.methodA();
a1.methodB();
System.out.println("main");
}
}
--------------------------------------------------
com.step6.A@4517d9a3
========================
com.step6.A@372f7a8d
com.step6.A@4517d9a3
==========[[methodB]]===========
com.step6.A@4517d9a3
2
==========[[methodB]]===========
main
package com.week3;
class Dept
{
private int deptno;
private String name;
private String loc;
//전변은 초기화를 생략할 수 있다. + 생성자가 대신 해줌
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
Dept(){}
Dept(int deptno, String name, String loc)
{
/*
* 현재 메모리에 활성화(언제든 사용이 가능한, 준비가 되어 있는)
* 원본의 객체를 참조하고 있다.
*
*/
this.deptno = deptno;
this.name = name;
this.loc = loc;
}
}
public class DeptMain {
public static void main(String[] args) {
Dept dept = new Dept();
Dept dept1 = new Dept(10,"개발1팀","부산");
System.out.println(dept.getDeptno()); // 0
System.out.println(dept1.getDeptno()); // 10
}
}
dept1
에 deptno=10
이 들어간다. 이후에 dept1.setDeptno(20);
통해서 setMethod
를 통해서 deptno=20
이 들어간다.package com.week3;
class Dept
{
private int deptno;
private String name;
private String loc;
//전변은 초기화를 생략할 수 있다. + 생성자가 대신 해줌
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
Dept(){}
Dept(int deptno, String name, String loc)
{
/*
* 현재 메모리에 활성화(언제든 사용이 가능한, 준비가 되어 있는)
* 원본의 객체를 참조하고 있다.
*
*/
this.deptno = deptno;
this.name = name;
this.loc = loc;
}
}
public class DeptMain {
public static void main(String[] args) {
Dept dept = new Dept();
Dept dept1 = new Dept(10,"개발1팀","부산");
System.out.println(dept.getDeptno()); // 0
dept1.setDeptno(20);
System.out.println(dept1.getDeptno()); // 20
}
}
------------------------------------------------------
0
20
package com.week3;
class Dept
{
private int deptno;
private String name;
private String loc;
//전변은 초기화를 생략할 수 있다. + 생성자가 대신 해줌
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
Dept(){}
Dept(int deptno, String name, String loc)
{
/*
* 현재 메모리에 활성화(언제든 사용이 가능한, 준비가 되어 있는)
* 원본의 객체를 참조하고 있다.
*
*/
this.deptno = deptno;
this.name = name;
this.loc = loc;
}
}
public class DeptMain {
public static void main(String[] args) {
Dept dept = new Dept();
Dept dept1 = new Dept(10,"개발1팀","부산");
System.out.println(dept.getDeptno()); // 0
dept1.setDeptno(100);
dept1.setName("운영팀");
dept1.setLoc("인천광역시");
System.out.println(dept1.getDeptno() +":"+dept1.getName()+": "+dept1.getLoc());
}
}
-----------------------------------------------------
0
100:운영팀: 인천광역시
package com.week3;
class H
{
int a;
H(){} // 디폴트 생성자는 파라미터가 없으니깐 생략해도 JVM이 제공함.
Hmain hm = new Hmain(this);
}
public class Hmain {
int b;
H hm = null;
//hm를 쓰고 싶음
/*
* 1. 전역변수로 인스턴스화해서 사용한다.
* 2.
*/
public Hmain(H h)
{
this.hm = hm;
}
public Hmain() {}
void methodA()
{
System.out.println(hm); //hm를 쓰고 싶음
}
public static void main(String[] args) {
Hmain hm = new Hmain();
hm.methodA(); // null 값 나옴
}
}
-------------------------------------------------
null
package com.week3;
class H
{
int a;
H(){} // 디폴트 생성자는 파라미터가 없으니깐 생략해도 JVM이 제공함.
Hmain hm = new Hmain(this);
}
public class Hmain {
int b;
H hm = null;
//hm를 쓰고 싶음
/*
* 1. 전역변수로 인스턴스화해서 사용한다.
* 2.
*/
public Hmain(H h)
{
this.hm = hm;
b = 20;
}
public Hmain() {}
void methodA()
{
System.out.println(hm); //hm를 쓰고 싶음
}
public static void main(String[] args) {
H h = new H();
Hmain hm = new Hmain(h);
System.out.println("==============인자1====");
System.out.println(hm);
System.out.println(hm.b);
/*
* 생성자가 두 번 호출 되는데 자바에서는 메소드 오버로딩이여서
* 같은 이름이여도 다른 생성자 인식함.
*/
hm = new Hmain();
System.out.println("==============재생성====");
System.out.println(hm);
System.out.println(hm.b);
/*
* 0 하고 null이 나오는 이유는 hm을 Hmain에 생성했기 때문에
* 해당 부분은 재정의 하는 동작 자체가 없음.
* 그래서 0하고 null
*/
hm.methodA(); // null 값 나옴
// 생성자라는 곳을 거치게 되면 전역변수 값이 바뀐다.
}
}
-------------------------------------------------------------
==============인자1====
com.week3.Hmain@4e50df2e
20
==============재생성====
com.week3.Hmain@1d81eb93
0
null
package com.week3;
class K
{
public K(Kmain km)
{
System.out.println("생성자 파트"+km);
}
}
public class Kmain {
public static void main(String[] args) {
Kmain km = new Kmain();
K k = new K(km);
System.out.println("main 파트"+km);
}
}
------------------------------------------------
생성자 파트com.week3.Kmain@4e50df2e
main 파트com.week3.Kmain@4e50df2e
package com.week3;
class K
{
public K(Kmain km)
{
System.out.println("생성자 파트"+km);
}
}
public class Kmain {
public static void main(String[] args) {
Kmain km = new Kmain();
System.out.println("main 파트"+km);
km = new Kmain();
K k = new K(km);
}
}
---------------------------------------------
main 파트com.week3.Kmain@2f4d3709
생성자 파트com.week3.Kmain@1d81eb93
package com.week3;
class K
{
public K(Kmain km)
{
System.out.println("생성자 파트"+km);
}
}
public class Kmain {
public static void main(String[] args) {
Kmain km = new Kmain();
km = new Kmain();
System.out.println("main 파트"+km);
K k = new K(km);
}
}
-------------------------------------------------
main 파트com.week3.Kmain@2f4d3709
생성자 파트com.week3.Kmain@2f4d3709
package com.step4;
import javax.swing.JFrame;
class ZipcodeApp{
//선언부
JFrame jf = new JFrame();
//생성자 - 생성자도 메소드 처럼 좌중괄호 우중괄호로 영역을 가짐 - 실행문(초기화, for,if포함, 연산가능함)올 수 있다.
//생성자에서도 메소드 호출 가능하다. -> 뭐가 다르지? 아님 같은건가? - 결과, 어떤 상황일때 차이점 발견해 본다
//선처리와 후처리 필수 파악
ZipcodeApp(ThisMain tm){
System.out.println(tm.i);
for(double d=0;d<9999999999999999999999999999999999.0;d++) {
System.out.println("커피를 내리는 시늉을 하고 있다.");//지연, 대기, 순서대로.....
}
//메소드 호출 순서를 정할 수 있어야 한다.(대기상태, 지연, 데드락....)
initDisplay();
}
//화면그리기
public void initDisplay() {
System.out.println("화면그리기");
jf.setSize(400, 600);
jf.setVisible(true);
}
}
public class ThisMain {
int i = 1;
ThisMain(){
}
public static void main(String[] args) {
ThisMain tm = new ThisMain();
//insert here - initDisplay호출해 보시오.
ZipcodeApp zc = new ZipcodeApp(tm);//spot-> static 을 사용하지 않으면서 세개의 나눠진 클래스가 한 몸처럼 움직이도록 한다
}
}
package com.week4;
class A
{
JFrame2 jFrame2 = null;
public A(JFrame2 jFrame2) {
System.out.println("A(this) 호출");
}
}
class B
{
public B(JFrame2 jFrame2) {
}
}
public class JFrame2 {
A a = new A(this);
// main스레드이다. - entry point이다.
public static void main(String[] args) {
JFrame2 jf = new JFrame2();
A a1 = new A(jf);
}
/*
* 24-26-20-21-7-6-8("A(this) 호출") - 26
* 27-7-6-8("A(this) 호출")-7
*/
}
-----------------------------------------------------------
A(this) 호출
A(this) 호출
package com.week4;
class A
{
JFrame2 jf = null;
public A(JFrame2 jFrame2) {
System.out.println("A(this) 호출");
this.jf = jFrame2;
System.out.println("jf.i : "+jFrame2.i+", this.jf.i:"+this.jf.i);//1을 쥐고 있다|없다
}
}
public class JFrame2 {
int i = 2;
public JFrame2()
{
A a = new A(this);
}
// main스레드이다. - entry point이다.
public static void main(String[] args) {
JFrame2 jf = new JFrame2();
System.out.println(jf.i);
}
/*32-23-21-25-8-6-10("A(this) 호출") -11-12
* 33 (2)
*
* 몰랐던 부분 디버깅 시 -> 클래스가 아니라 생성자 먼저 들렸다가(JFrame2) -> 전역변수 들렸다가(i)
* 밑에 수행한다.
*/
}
------------------------------------------------------------------------
A(this) 호출
jf.i : 2, this.jf.i:2
2