class (클래스)

2020. 7. 7. 17:28JAVA

<classes: syntax>

: 클래스는 instance 가 어떤식으로 행동하는지 그리고 어떤 정보를 나타내는지

  묘사하는 지시사항이다.

 

<classes: constructor(생성자)>

:클래스 내에서 정의되는 메소드 이고 클래스와 이름을 공유한다.

 리턴타입을 정의하지 않는다.

 인스턴스 변수 초기화 혹은 인스턴스 생성시 수행되어야할 작업을위해 사용한다.

 

 

ex)

/*클래스*/

public class car{

   public car(){

       public static void main(String[] args){

           //인스턴스화

           car ferrari = new car ();

         }

     }

}

 

- car ferrari = new car ();   

변수선언과함계 타입을 car으로 이름을 ferrari라고 함.

new 키워드를 사용하여 새로운 인스턴스 만들것을 지시함.

new를 생략하면 오류가 발생한다.

 

<classes: constructor parameter>

 

ex)

 

public class Store {

           //instance field

           String productType;

 

          //constructor method

     public Store(String product){

             productType = product;

 }

public stataic void main(String[] args){

 

}

 

위와  생성자 메소드에 파라미터를 넣어주어 인스턴스 필드의 프로덕트 타입의 값을 지정해줄수있다.

파라미터를 이용하면 데이터의 타입과 이름을 구체화 할수있다.

 

<Classes: Assigning Values to Instance Fields>

 

 

public class Store {

           //instance field

           String productType;

 

          //constructor method

     public Store(String product){

             productType = product;

 }

public stataic void main(String[] args){

      Store lemonadestand = new Store("lemonade");

     System.out.println(lemonadeStand.productType););

}

}

 

출력된 화면 

lemonade

 

위와 같이 스토어의 인스턴스를 만들어 레모네이드 스탠드변수에 부여해준다음 레몬에이드 파라미터를 사용해주고 출력해주면

다음과 같은 출력화면을 볼수있다.

 

 

public class Store {

           //instance field

           String productType;

           int inventoryCount;;

           double inventoryPrice;;

 

          //constructor method

     public Store(String product, int count, double price){

             productType = product;

             inventoryCount= count;;

             inventoryPrice=price;;

 }

public stataic void main(String[] args){

      Store lemonadestand = new Store("lemonade");

     System.out.println(lemonadeStand.productType);

   Store cookieShop=new Store("cookies",12, 3.75););

)

;}

}

 

 

'JAVA' 카테고리의 다른 글

2.문자와 숫자  (0) 2020.07.09
1.[java]컴파일  (0) 2020.07.09
자바 코드의 구조 (초초초간단한 요약)  (0) 2020.07.06
[JSP]execute/executeQuery()/executeUpdate()  (0) 2020.07.06
[JSP] 태그 (Tag) - 스크립트 태그  (0) 2020.07.05