[ 작업 환경 ]

 

Windows10

jdk-15

 

 

 

 

 

1. Book2.java 작성

 

public class Book2 {

    public String title;
    protected int pages;

    public Book2(String mytitle) {
        title = mytitle;
    }

    public Book2(int numPages) {
        pages = numPages;
    }

}

 

 

 

 

 

2. NoteBook.java 작성

 

public class NoteBook extends Book2 {
    
    private String memo;

    public NoteBook(String mytitle) {
        super(mytitle);
    }

    public NoteBook(String mytitle, String memo) {
        super(mytitle);
        this.memo = memo;
    }

    public NoteBook(int numPages, String memo) {
        super(numPages);
        this.memo = memo;
    }

    public void setMemo(String mymemo) {
        this.memo = mymemo;
    }

    public String getMemo() {
        return this.memo;
    }
}

 

 

 

 

 

3. MyTester2.java 작성

 

public class MyTester2 {
    public static void main(String[] args) {
        
        NoteBook mybook = new NoteBook(30, "Empty");

        System.out.println("title : " + mybook.title);
        System.out.println("pages : " + mybook.pages);
        System.out.println("memo : " + mybook.getmemo());
        System.out.println();

        mybook.title = "English";

        System.out.println("title : " + mybook.title);
        System.out.println("pages : " + mybook.pages);
        System.out.println("memo : " + mybook.getmemo());
        System.out.println();

        mybook.setmemo("This is an example memo.");

        System.out.println("title : " + mybook.title);
        System.out.println("pages : " + mybook.pages);
        System.out.println("memo : " + mybook.getmemo());

    }
}

 

 

 

 

 

4. 실행 결과 예시

 

javac Book2.java
javac NoteBook.java
javac MyTester2.java
java MyTester2

 

 

 

 

+ Recent posts