[1] java 파일 생성 [폴더에 Hello.java 만들기]
[2] 컴파일(javac) 하면 [cmd에 javac Hello.java 실행]
클래스가 생성된다 [폴더에 Hello.class가 생김]
: OS 위에 있는 JVM에 java 파일을 메모리에 할당한다
[3] java 실행 [cmd에 java Hello]
: JVM에서 java 파일 실행, main 함수를 찾고 호출
BankAccount.java
public class BankAccount {
int balance = 0;
public int deposit(int amount) {
balance += amount;
return balance;
}
public int withdraw(int amount) {
balance -= amount;
return balance;
}
public int checkMyBalance() {
System.out.println("My Balance : " + balance);
return balance;
}
}
BankAccountTest.java
public class BankAccountTest {
public static void main(String[] args) {
BankAccount yoon = new BankAccount();
yoon.deposit(5000);
yoon.checkMyBalance();
yoon.withdraw(1000);
yoon.checkMyBalance();
}
}
public class BankAccountTest {
public static void main(String[] args) {
BankAccount yoon = new BankAccount();
yoon.deposit(5000);
yoon.checkMyBalance();
yoon.withdraw(1000);
yoon.checkMyBalance();
BankAccount park = new BankAccount();
park.deposit(4000);
park.checkMyBalance();
park.withdraw(5000);
park.checkMyBalance();
}
}
[Console]
My Balance : 5000
My Balance : 4000 (yoon 계좌)
My Balance : 4000
My Balance : -1000 (park 계좌)
(클래스 안에 static을 추가하면 변수값이 다같이 쌓이게 되므로, 메인 함수에만 static을 넣으면 된다.)
클래스는 구현 하려는 기능을 모아둔 설계도 라고 볼 수 있다.
객체는 클래스의 기능을 활용하기 위해 메모리를 할당해 실제 사용하는 것을 의미한다.
구현하고자 하는 중복되는 기능이 2개 이상일 때 그것을 하나의 클래스로 정의하고, 그 클래스를 통해 객체를 생성(메모리에 할당)한다.
class Baby {
int age; // instance 변수
void setAge(int num) { // 나이를 설정하는 함수
age = num;
}
int getAge() { // 나이를 리턴하는 함수
return age;
}
void showAdult(){ // 어른인지 판별하는 함수
if(age >= 20) {
System.out.println("술을 마실 수 있습니다.");
} else {
System.out.println("술을 마실 수 없습니다.");
}
return;
}
}
/*********************************/
class BabyTest {
public static void main(String[] args) {
Baby baby = new Baby();
System.out.println(baby); // Baby@4926097b
/* class 'Baby'의 주소 출력
* 해당 주소는 JVM의 코드이므로 가상의 주소이며, 모두 다른 값이 나온다*/
System.out.println(baby.age);
/* 값을 주지 않으면 0으로 초기화된다 */
baby.age = 10;
System.out.println(baby.age);
baby.setAge(20);
System.out.println(baby.age);
// 변수에 값을 넣어서 직접 접근하기
System.out.println(baby.getAge());
// 함수로 접근하기
/*********************************/
Baby baby2 = new Baby();
baby2.setAge(10);
System.out.println(baby2.getAge());
System.out.println(baby2); // Baby@762efe5d
baby2.showAdult();
baby2.setAge(20);
baby2.showAdult();
baby.showAdult();
/*********************************/
}
}
[Console]
Baby@4926097b
0
10
20
20
10
Baby@762efe5d
술을 마실 수 없습니다.
술을 마실 수 있습니다.
술을 마실 수 있습니다.
class Circle {
final double PI = 3.14;
int r;
void setRadius(int num) { // 반지름 설정하는 함수
r = num;
}
/*
* int setRadius(int num) { r = num; return r; }
*/
double getArea() { // 원의 넓이를 구하는 함수
return r * r * PI;
}
}
public class CircleTest {
public static void main(String[] args) {
Circle circle = new Circle();
circle.setRadius(10);
double area = circle.getArea();
System.out.println(area);
}
}
[Console]
314.0
class Rectangle {
double w;
double h;
void setWidth(double width) {
w = width;
}
void setHeight(double height) {
h = height;
}
double getArea() {
return w * h;
}
}
public class RectangleTest {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle();
rectangle.setWidth(10);
rectangle.setHeight(10);
double area = rectangle.getArea();
System.out.println(area);
}
}
[Console]
100.0
class Sum {
int a; // 변수 선언
void getNumber(int num) {
// 여기까지 더하고자 하는 수를 얻어오는 함수
a = num;
}
int getSum() { // 1부터 원하는 수까지 더하는 함수
int sum = 0;
for (int i = 1; i <= a; i++) {
sum += i;
}
return sum;
}
}
public class SumTest {
public static void main(String[] args) {
Sum memory = new Sum();
memory.getNumber(10); // 10까지 더하고 싶다
int number = memory.getSum(); // 1+2+...+10
System.out.println(number);
}
}
[Console]
55
class StarPrint {
int num; // 층수 구하기
void printTriangle(int num) { // 원하는 층수의 별 찍기
for (int i = 1; i <= num; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
public class StarPrintTest {
public static void main(String[] args) {
StarPrint strPrint = new StarPrint();
strPrint.printTriangle(5);
System.out.println();
}
}