- this()
this() : 자신의 클래스 내의 생성자 호출
this - 자주사용
super - 거의안씀
this() - 거의안씀
super() - 자주사용
- this.변수 하면은 전역변수를 가리킴.
----------------------------------------------------------------------------------
예제 1
----------------------------------------------------------------------------------
/*
this : 자신의 객체
this.변수 : 전역변수
*/
class Test{
int a = 10; //전역
public void aa()
{
int a = 100; //지역
System.out.println("this.a=> " + this.a); // this.변수는 전역변수 a를 가리킴 10출력
System.out.println("a: " +a); // 지역변수 a를 가리킴 100 출력
}
}
class ThisTest_1
{
public static void main(String args[])
{
Test A = new Test();
A.aa();
}
}
----------------------------------------------------------------------------------
예제 2
----------------------------------------------------------------------------------
/*
*/
class ThisTest_2
{
int a = 100;
static int b = 500;
public static void main(String args[])
{
//a = 200; // Error!!
//this.a = 300; // Error!!
b = 1000;
System.out.println("b:" + b);
}
}
----------------------------------------------------------------------------------
static 메서드에서는 this사용 못함 자신이 2번 호출 되므로.
(메모리에 올라와 있는데 다시 올린다...?
main 메서드와 같이 static 메서드 에서는 this 키워드를 쓸 수 없다.
this키워드는 자신의 클래스의 객체(Object)를 의미한다.
static 메서드는 Object가 생성되기 전에 클래스가 메모리에 로드 될때 같이 로드된다.
static 메서드에서는 static이 아닌 변수 메소드를 호출 할 수 없다.
----------------------------------------------------------------------------------
예제 1
----------------------------------------------------------------------------------
class Cons
{
String title;
int num;
public Cons(String t, int n){ //1번 생성자
title = t;
num = n;
System.out.println(title + " ~~~~" + num);
}
public Cons(String t) //2번 생성자
{
this(t,200);
}
}
class ThisTest_3
{
public static void main(String arts[])
{
new Cons("연습1"); //2번 생성자 호출하면 다시 2번이 1번 생성자 호출
new Cons("연습2",500); //2번 생성자 호출
}
}
실행결과 : 연습1 ~~~~200
연습2 ~~~~500
----------------------------------------------------------------------------------
예제 2
----------------------------------------------------------------------------------
class PanMae
{
public PanMae(String pum) //1
{
this(pum,10);
}
public PanMae(String pum, int su) //2
{
this(pum,su,1000);
}
public PanMae(String pum, int su, int dan) //3
{
System.out.println(pum+ "~~~"+su+"~~~~"+dan);
}
}
class ThisTest_4
{
public static void main(String arts[])
{
new PanMae("사과"); //1번 -> 2번 -> 3번 생성자 호출
new PanMae("수박",3); // 2번 -> 3번 생성자 호출
new PanMae("딸기",5,2500); // 3번 생성자 호출
}
}
실행결과 : 사과~~~10~~~~1000
----------------------------------------------------------------------------------
String substring(int start) : 현재 문자열 객체에서 start위치부터 끝가지 문자열 추출
String str ="GoodMorning-kin-dong";
String s1 = str.substring(12); // kil-dong
String substring(int start, int end) : 현재 문자열 객체 start부터 end직전까지 문자열 추출
String s2 = str.substring(12,15); // kil
int indexOf(int ch) : 현재 문자열 객체에서 ch가 첫번째로 발견된 위치를 반환, 없으면 -1을 반환.
String str2 = "banana";
int a1 = str2.indexOf('a'); // 1
int indexOf(String str) : 현재 문자열 객체에서 str를 찾아서 존재하면 첫째문자 위치를 반환하고,
없으면 -1을 반환.
String str3 = "총 비용은 $45.76";
0 1 2 3 456789
int a2 = str3.indexOf("$45.76"); //6
----------------------------------------------------------------------------------
예제 (주민번호를 입력받아 성별을 판별하세요)
----------------------------------------------------------------------------------
class HomeWork
{
public static void main(String args[])
{
/*
String ju = args[0];
System.out.println("입력한 주민번호 :" +ju);
String temp = ju.substring(7,8);
if(true == temp.equals("1"))
System.out.println("남자입니다.");
else if (true == temp.equals("2"))
System.out.println("여자입니다.");
else if (true == temp.equals("3"))
System.out.println("남자 입니다");
else if (true == temp.equals("4"))
System.out.println("여자 입니다");
else
System.out.println("잘못 입력 해써");
*/
String ju = args[0];
System.out.println("입력한 주민번호 :" +ju);
char ch[] = new char[ju.length()];
ju.getChars(7,8,ch,0);
if('1' == ch[0] || '3' == ch[0])
System.out.println("남자입니다.");
else if ('2'== ch[0] || '4' == ch[0])
System.out.println("여자입니다.");
else
System.out.println("잘못 입력 해써");
}
}
------------------------------------------------------------------------------
- 객체 배열
class Sung
{
private String name;
private int kor;
private int eng;
public Sung(){}
public Sung(String name, int kor, int eng)
{
this.name = name;
this.kor = kor;
this.eng = eng;
}
public void disp() // 사용자 정의 메서드
{
System.out.println(name + " " + kor + " " + eng);
}
}
class ObjectArray
{
public static void main(String args[])
{
//일반적인 객체생성
/*
Sung s1 = new Sung("김",0,100);
Sung s2 = new Sung("이",20,40);
Sung s3 = new Sung("박",40,90);
s1.disp();
s2.disp();
s3.disp();
*/
// 객체 배열
Sung str[] ={new Sung("kim", 40,30), new Sung("lee",34,67), new Sung("park",67,86)};
for(int i=0; i<3; i++)
str[i].disp();
}
}
------------------------------------------------------------------------------
- 상속
- 이미 만들어져 있는 클래스를 상속받아 또 다른 클래스를 만들 수 있다.
super 클래스 파생 클래스
- 파생클래스에서는 super클래스의 기능 (변수, 메서드)을 사용할 수 있다.
- 클래스가 클래스를 상속받을때 extends 사용
- 인터페이슨가 인터페이스를 상속받을때 extends 사용
- 클래스가 인터페이스를 상속받을대 implements 사용
- 클래스는 다중상속이 불가능하다.
- 인터페이스는 다중상속이 가능하다.
class Test extends Frams implenment WindowsListener, KeyListener
------------------------------------------------------------------------------
예제
------------------------------------------------------------------------------
/*
상속관계일때 상위클래스 맴버 변수 사용 예
*/
class General
{
static int roomNo = 100;
}
class GeneralRoom extends General
{
public static void main(String args[])
{
System.out.println("roomNo :"+roomNo);
}
}
-------------------------------------------------------------------------------
/*
상속관계가 아닐대 맴버 변수 사용 예
*/
class General_1
{
int roomNo = 200;
}
class GeneralRoom_2
{
public static void main(String args[])
{
//System.out.println("roomNo :" +roomNo); // Error!!
General_1 A = new General_1(); // 객체 생성
System.out.println("roomNO: " +A.roomNo);
//자바 버전 알아보기
System.out.println("Java Version: " + System.getProperty("java.version"));
System.out.println("Java ClassPath " + System.getProperty("java.class.path"));
}
}
-------------------------------------------------------------------------------
/*
생성자 호출 순서
*/
class PersonSuper
{
protected String name; //보호필드 (수퍼클래스와 파생클래스에서만 사용가능)
public PersonSuper(){
System.out.println("PersonSuper defalut constructor");
}
public PersonSuper(String name)
{
this.name = name;
System.out.println("PersonSuper 인자");
}
public void disp()
{
System.out.println("이름 :" + name);
}
}
class PersonSub extends PersonSuper
{
public double left_eye;
public double right_eye;
public PersonSub()
{
super();
System.out.println("PersonSub defalut constructor");
}
public PersonSub(String name, double left, double right)
{
super(name);
System.out.println("PersonSub 인자");
left_eye = left;
right_eye = right;
}
public void disp() //Overriding
{
System.out.println("이름 :" +name+" 좌시 :" +left_eye+" 우시 : " +right_eye);
}
}
class Extends_3
{
public static void main(String args[])
{
PersonSub AAA = new PersonSub();
// 자식 클래스가 부모 클래스로 부터 상속받았을때 부모 클래스 생성자를 자동으로 호출
// PersonSub 생성자 호출하면 PersonSuper 생성자 자동호출
// 상속 해주는 클래스의 default 생성자는 상속받은 클래스가 호출될때 자동으로 호출된다.
// 전혀 무관한 클래스에서 객제생성 하므로 default 생성자는 무조건 public
System.out.println();
PersonSub A = new PersonSub("김본좌",1.0,1.0);
A.disp();
}
}
----------------------------------------------------------------------------------
실행결과 : PersonSuper defalut constructor
PersonSub defalut constructor
PersonSuper 인자
PersonSub 인자
이름 :김본좌 좌시 :1.0 우시 : 1.0