final

응용 SoftWare/JAVA 2016. 12. 1. 16:08

테스트 클래스

//클래스에 final 표기시 클래스를 상속할 수 없다.

//public final class FinalTest 

public class FinalTest

{

//멤버변수의 final은 변수를 상수화 시킬 때 사용

//한번 값이 대입되면 수정할 수 없다.

final String SCHOOL = "가산초등학교";

final int MAX_KOR;


public FinalTest(){

MAX_KOR=200;

}

//메소드에 final이 있을 경우 상속은 가능하나 오버라이딩 할 수 없다.

public final void output(){

System.out.println(SCHOOL+"="+MAX_KOR);

}

}

=====================================================================

메인 클래스

public class FinalMain extends FinalTest

{

public FinalMain(){

}

public void start(){

//SCHOOL = "독산초등학교";

System.out.println(SCHOOL);

System.out.println(MAX_KOR);

output();

}

public void output(){

System.out.println("["+SCHOOL+"] ["+MAX_KOR+"]"); // <<< error 임

}

public static void main(String[] args) 

{

FinalMain fm = new FinalMain();

fm.start();

}

}



'응용 SoftWare > JAVA' 카테고리의 다른 글

interface 클래스  (0) 2016.12.02
추상 클래스  (0) 2016.12.02
static  (0) 2016.12.01
private  (0) 2016.12.01
public과 protected  (0) 2016.12.01
Posted by Hyun CHO
,

static

응용 SoftWare/JAVA 2016. 12. 1. 16:07
테스트 클래스


public class StaticTest 

{

//변수선언, 객체생성

static String name = "StaticTest 조현";

int ban = 11;

public static String addr = "서울시 양천구 신정동";


static{

//클래스 내에는 실행문을 사용할 수 없지만 static 내로 지정하면 사용할 수 있다.

int a=10+200;

System.out.println("a="+a);

}

public StaticTest(){

System.out.println("Static()생성자 출력");

}



//메소드에 static

public static void output(){

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

}

}

=====================================================================

메인 클래스

class  StaticMain

{

public static void main(String[] args) 

{

StaticTest st1 = new StaticTest();

StaticTest st2 = new StaticTest();

StaticTest st3 = new StaticTest();


//Static 있는 메소드는 객체생성하지 않고 클래스명, 메소드명으로 실행할 수 있다.

StaticTest.output();



}

}



'응용 SoftWare > JAVA' 카테고리의 다른 글

추상 클래스  (0) 2016.12.02
final  (0) 2016.12.01
private  (0) 2016.12.01
public과 protected  (0) 2016.12.01
package ComFile  (0) 2016.12.01
Posted by Hyun CHO
,

private

응용 SoftWare/JAVA 2016. 12. 1. 14:55

class  SchoolInfor

{

//private 으로 선언된 변수는 같은 클래스 내에서만 접근가능

//private 으로 선언된 변수는 상속하지 않는다.

private String name = "SchoolInfor 조현";

private int kor = 90;


SchoolInfor(){}


void output(){

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

}


void setName(String name){

this.name = name;

}

void setKor(int kor){

this.kor = kor;

}

String getName(){

return name;

}

int getKor(){

return kor;

}

}


=====================================================================

메인

class SchoolMain extends SchoolInfor

{

SchoolMain(){

}

void output(){

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

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

}

public static void main(String[] args) 

{

SchoolMain sm = new SchoolMain();

sm.output();

}

}



'응용 SoftWare > JAVA' 카테고리의 다른 글

final  (0) 2016.12.01
static  (0) 2016.12.01
public과 protected  (0) 2016.12.01
package ComFile  (0) 2016.12.01
상속 관계에서 객체 형변환  (0) 2016.12.01
Posted by Hyun CHO
,