int[] arr = new int[3]
// 자료형[][] 참조변수 = new 자료형[행의 개수][열의 개수]
int[][] arr = new int[2][3]
arr = {
{11, 22, 33},
{44, 55, 66}
};
//참조변수[행 번호][열 번호]
//배열의 시작은 항상 '0' !!
int i = arr[0][2];
// i = 33
public static void main(String[] args) {
int[][] arr = new int[3][4];
int num = 1;
// 배열에 값을 저장
for(int i = 0; i < 3; i++) { // 행
for(int j = 0; j < 4; j++) { // 열
arr[i][j] = num;
num++;
}
}
// 배열에 저장된 값을 출력
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 4; j++) {
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
}
Car car = new Car()
생성자 함수 : Car()
class Pizza {
int size;
String type;
public Pizza() { // 생성자
size = 12;
type = "슈퍼슈프림";
}
public Pizza(int size, String type) { // 생성자
this.size = size;
this.type = type;
}
}
public class PizzaTest {
public static void main(String[] args) {
Pizza obj1 = new Pizza();
System.out.println("("+obj1.type+ " , "+obj1.size+",)");
Pizza obj2 = new Pizza(24, "포테이토");
System.out.println("("+obj2.type+ " , "+obj2.size+",)");
}
}
class Pizza {
int size;
String type;
}
public class PizzaTest {
public static void main(String[] args) {
Pizza obj1 = new Pizza(); // 컴파일러가 자동으로 만들어준 기본생성자가 있기 때문에 가능
}
}
class Pizza {
int size;
String type;
public Pizza() {} // 기본 생성자
public Pizza(int size, String type) { // 생성자
this.size = size;
this.type = type;
}
}
public class PizzaTest {
public static void main(String[] args) {
Pizza obj1 = new Pizza();
Pizza obj2 = new Pizza(24, "포테이토");
}
}
public class Circle {
int radius;
public Circle(int radius) {
this.radius = radius;
}
double getArea() {
return 3.14 * radius * radius
}
}
class Car{
String color;
String gearType;
int door;
// 기본 옵션
Car() {
this("black", "auto", 4);
// this.color = "black";
// this.gearType = "auto";
// this.door = 4;
}
// 색상 옵션 추가
Car(String color) {
this(color, "auto", 4);
// this.color = color;
// this.gearType = "auto";
// this.door = 4;
}
// 색상, 문 옵션 추가
Car(String color, int door) {
this(color, "auto", door);
}
// 풀옵션
Car(String color, String gearType, int door) {
this.color = color;
this.gearType = gearType;
this.door = door;
}
}
public static void main(String[] args) {
Tv myTv = new Tv("LG", 2017, 32); //LG에서 만든 2017년 32인치
myTv.show();
}
LG에서 만든 2017년형 32인치 TV
class Tv{
private String brand;
private int year;
private int inch;
public Tv(String brand, int year, int inch) {
this.brand = brand;
this.year = year;
this.inch = inch;
}
void show() {
System.out.println(brand + "에서 만든 " + year + "년형 " + inch + "인치 TV");
}
}
public class TelevisionEx {
public static void main(String[] args) {
Tv myTv = new Tv("LG", 2017, 32);
myTv.show();
}
}
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();
}
import java.util.Scanner;
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 double average() {
return (double)(math+science+english) / 3;
}
}
public class GradeClassTest {
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();
}
}
생성자 2개: 기본 생성자와 매개변수로 모든 필드를 초기화하는 생성자
노래 정보를 출력하는 show() 메소드
main() 메소드에서는 1978년, 스웨덴 국적의 ABBA가 부른 "Dancing Queen"을song 객체로 생성하고 show()를 이용하여 노래의 정보를 다음과 같이 출력하라.1978년 스웨덴국적의 ABBA가 부른 Dancing Queen
class Song{
private String title, artist, country;
private int year;
public Song() {
this("title", "artist", 0000, "country");
}
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);
}
}
public class SongPlayer {
public static void main(String[] args) {
Song song = new Song("Dancing Queen","ABBA",1978,"스웨덴");
song.show();
}
}
String
클래스의 실제 이름(full name)은 java.lang.String
이다.java.lang
패키지에 속한 String
클래스라는 의미package 패키지명;
import 패키지명.클래스명;
import 패키지명.*; //패키지 아래 모든 클래스 사용
private | 같은 클래스 내에서만 접근이 가능 |
---|---|
(default) | 같은 패키지 내에서만 접근이 가능 |
protected | 같은 패키지 내에서, 그리고 다른 패키지의 자손클래스에서 접근이 가능 |
public | 접근 제한이 전혀 없다. |
제어자 | 같은 클래스 | 같은 패키지 | 자손 클래스 | 전체 |
---|---|---|---|---|
public | ⭕ | ⭕ | ⭕ | ⭕ |
protected | ⭕ | ⭕ | ⭕ | |
(default) | ⭕ | ⭕ | ||
private | ⭕ |
예시
private
으로 하여 외부에서 접근하지 못하도록 하고, 메서드는 public
으로 하여 직접 접근은 막고 메서드를 통한 간접 접근 허용class Circle {
private double rad = 0;
final double PI = 3.14;
public Circle(double rad) {
setRad(rad);
}
public double getRad() {
return rad;
}
public void setRad(double rad) {
if(rad<0) {
this.rad=0;
return;
}
this.rad = rad;
}
public double getArea() {
return PI*rad*rad;
}
}
public class CircleTest {
public static void main(String[] args) {
Circle c = new Circle(1.5);
System.out.println(c.getArea());
c.setRad(2.5);
System.out.println(c.getArea());
c.setRad(-3.3);
System.out.println(c.getArea());
// c.rad = -4.5; // 컴파일 오류로 이어짐
// System.out.println(c.getArea());
}
}