06_클래스와 객체 (생성자)

필드 == 멤버변수 == 인스턴스변수 다 같은말

전역변수

필드 == 멤버변수 == 인스턴스변수 다 같은말

new 연산자 통해 객체를 생성

객체소멸시 같이 소멸

클래스 변수 == static 변수

프로그램 실행시 무조건 메모리영역

프로그램 종료시 소멸

생성자

public 클래스명(매개변수,매개변수,매개변수,…){

}

생성자 사용 목적

객체를 생성해주기위해

생성자명은 클래스명과 반드시 동일

리턴값 존재 X

매개변수 생성자를 명시적으로 작성하면 JVM에서 자동으로 만들어주지 않음!!!

(기본생성자는 무조건 쓰는게 맞음 안써도 될때 써도되니까..)

매개변수 생성자

객체 생성과 동시에 전달값을 매개변수로 받아서 필드에 초기화할 목적임.

public User(String Id, String Pwd, String userName) {
    this.Id = Id;
    this.Pwd = Pwd;
    this.userName = userName;
}

public User(String Id, String Pwd, String userName, int age, char gender){

    this(Id,Pwd,userName);	// this 묶으면 편하게 쓸수 있음!!
    this.age = age;          // 단, this 묶음은 맨 앞줄에!!!!
    this.gender = gender;
}

문제 풀이

package chap06;

public class Question01 {
    public static void main(String[] args) {
        TV myTV = new TV("LG", 2017, 32); //LG에서 만든 2017년 32인치
        myTV.show();
    }
}




package chap06;

public class TV {

    private String brand;
    private int year;
    private int inch;

    public TV(){}

    public TV(String brand,int year,int inch){
        this.brand = brand;
        this.year = year;
        this.inch = inch;
    }

    public void show(){
        System.out.println(brand + "에서 만든" + year +"년형 " + inch + "인치 TV" );
    }

}
package chap06;

import java.util.Scanner;

public class Question02 {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);

        System.out.print("수학, 과학, 영어 순으로 3개의 정수 입력 >> ");
        int math = sc.nextInt();
        int science = sc.nextInt();
        int english = sc.nextInt();
        Grade me = new Grade(math, science, english);
        System.out.println("평균은 "+me.average()); // average()는 평균을 계산하여 리턴

        sc.close();
    }
}



package chap06;

public class Grade {

    private int math;
    private int science;
    private int english;

    public Grade() {
    }

    public Grade(int math, int science, int english) {
        this.math = math;
        this.science = science;
        this.english = english;

    }

    public int average() {

        return (math + science + english) / 3;
    }
}
package chap06;

public class Question03 {
    public static void main(String[] args) {

        //- main() 메소드에서는 1978년, 스웨덴 국적의 ABBA가 부른 "Dancing Queen"을
        //song 객체로 생성하고 show()를 이용하여 노래의 정보를 다음과 같이 출력하라.
        //1978년 스웨덴국적의 ABBA가 부른 Dancing Queen
        Song artist1 = new Song("Dancing Queen", "ABBA", 1978, "스웨덴");
        artist1.show();

    }
}



package chap06;

public class Song {
    private String title;
    private String artist;
    private int year;
    private String country;

    public Song() {
    }

    public Song(String title, String artist, int year, String country) {
        this.title = title;
        this.artist =artist;
        this.year = year;
        this.country = country;
    }

    public void show(){

        System.out.println(year + "년 " + country + "국적의 " + artist + "가 부른 " + title);
    }

}
package chap06;

import java.util.Scanner;

public class Question05 {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        Circle c[] = new Circle[3]; // 3개의 Circle 배열 선언

        for (int i = 0; i < c.length; i++) {
            System.out.print("x, y, radius >>");
            double x = sc.nextDouble();  // x값 읽기.
            double y = sc.nextDouble(); // y값 읽기.
            int z = sc.nextInt(); // radius값 읽기.
            c[i] = new Circle(x, y, z); // Circle 객체 생성
        }
        for (int i = 0; i < c.length; i++)
            c[i].show(); // 모든 Circle 객체 출력
        sc.close();
    }
}


package chap06;

public class Circle {


    private double x, y;
    private int radius;

    public Circle(double x, double y, int radius) {
        this.x = x; //x, y, radius 초기화
        this.y = y;
        this.radius = radius;
    }

    public void show() {
        System.out.println("(" + x + y + ")" + radius);
    }
}