Ms'Note

Object-7 본문

IT/└▶Example Coding.JAVA

Object-7

Jelly_B 2020. 8. 8. 23:16

파라미터를 갖는 생성자의 이용

class Article {
    int seq;
    String subject;
    String writer;
    
    // 파라미터가 존재하는 생성자
    // --> 파라미터에 의해서 멤버변수의 값을 초기화 한다.
    
    public Article(int seq, String subject, String writer) {
        this.seq = seq;
        this.subject = subject;
        this.writer = writer;
    }
    
    public void print() {
        System.out.println(this.seq);
        System.out.println(this.subject);
        System.out.println(this.writer);
    }
}

public class Main07 {
    public static void main(String[] args) {
        Article article1 = new Article(1, "자바연습 첫번째 입니다.", "자바학생");
        article1.print();
        System.out.println("----------");
        Article article2 = new Article(2, "자바는 객체지향 언어입니다.", "자바강사");
        article2.print();
    }
}

 

 

출력 결과

1
자바연습 첫번째 입니다.
자바학생
----------
2
자바는 객체지향 언어입니다.
자바강사

'IT > └▶Example Coding.JAVA' 카테고리의 다른 글

Hiding-2  (0) 2020.08.11
Hiding-1  (0) 2020.08.09
Object-6  (0) 2020.08.08
Object-5  (0) 2020.08.08
Object-4  (0) 2020.08.08
Comments