GitHub

https://github.com/Backcoder-June

BackCoder 기록 그리고 숙달

Back to the Java

System 클래스 / Class 클래스

Backcoder 2022. 7. 4. 13:02

< System 클래스 >

 

=> 자바 프로그램 실행 / 컴퓨터 시스템 정보 제공 

System 내 컴퓨터 
out 내 모니터 
println 출력        

System 내 컴퓨터 
in 내 키보드 
read 입력   


System.out. + OutputStream포함 메소드명  (ex. print~ ) 
System.in. + InputStream포함 메소드명  (ex. read ) 

 


<  System 클래스 메소드들  > ( 컴퓨터 정보 읽어올때 유용 ) 


System.exit(0);     => 시스템 종료 ( if ( A == B ) { System.exit(0); }  // ( 정상적인 종료 방법이다. ) 
System.currentTimeMilis( );     => 1/1000초 시각 ( long타입 )  => 시작, 끝에서 측정해서 시간 측정 / 효율체크 
System.getProperty("os.name");    => os 종류 보여줌 (windows, mac, linux )
System.getProperty("java.version");     => java 정보 
System.getProperty("user.dir");     => 자바저장디렉토리 
System.getProperty("file.separator");      => 파일분리자 ( OS 마다 표현 다르다 주의 " /  \  . 등등") 
System.getEnv( );  =>     컴퓨터의 환경변수 값 출력 


< Class 클래스 메소드 >  =>  Class.forName   

 

- 클래스 내부 정보( 변수 메소드 생성자 modifier...) 정보 제공해주는 클래스  (대문자 Class)

 

=> Class. forName

class A{ } => A.java => 컴파일 

=> A.class 파일생성 전달 
class B{ A a1 = new A( );  } 

 

받은 A 변수나 메소드 등등 정보를 알고싶음  

 

class c = Class.forName( "A" ) 
c.getMethods 
c.getCons 
c.getFields 
... 
//

 

< forName   =>  동적 객체 생성하기 (정해지지 않은) >

 

args 로 객체 만들고싶다  A 객체
args[0]  // 문자로 받음 "A" 

A a1 = new A( );  이렇게 만들고 싶어서 
args[0] a1 = new args[0];   
// 이건 안된다.   -   A 자리는 클래스타입. args[0]은 String타입. 


//입력된 문자열(args[0] - A ) 를 해석해서 이걸 실제 클래스타입 으로 변환해준다.  

 

-------------------------------------------------------------------------------------
try{ 
     class c = Class.forname(args[0]);   //클래스타입으로 변환 
     Object o = c.newInstance( ); }      //해당클래스 객체 생성  

-------------------------------------------------------------------------------------


// 이  두줄이 동적 객체 생성하는 방법 

     sout( o.getClass( ).getName( ) ); 
catch( Exception e ) { e.printStackTrace( ); } 


// args[0]입력값이 내 클래스중에 존재할 땐, 그걸로 객체 만들어 지고, 
   없을 땐, catch 로 오류 잡고