Java 실무 기초
객체지향언어
class Phone {
String model;
String color;
int price;
}
public class Main {
public static void main(String[] args) {
Phone galaxy = new Phone();
galaxy.model = "Galaxy10";
galaxy.color = "Black";
galaxy.price = 100;
Phone iphone =new Phone();
iphone.model = "iPhoneX";
iphone.color = "Black";
iphone.price = 200;
System.out.println("철수는 이번에 " + galaxy.model + galaxy.color + " + 색상을 " + galaxy.price + "만원에 샀다.");
System.out.println("영희는 이번에 " + iphone.model + iphone.color + " + 색상을 " + iphone.price + "만원에 샀다.");
}
}
int [] heights = new int[]{10, 20, 30, 40, 50}; // 키가 저장되어 있는 배열
intHeight(heights);
sortHeight(heights); // heights 오름차순으로 정렬
printHeight(heights); // heights에 있는 것을 하나하나 꺼내서 출력
// 이러한 함수를 쓰지 않는다면 int[]에 초기화하는 로직{}을 적어줘야함
// 더하기 함수
int add(int x, int y) { // 맨 앞 int는 함수의 결과값이 전달되는 타입을 말함(return type)
// 함수 이름 뒤에 ()오는 두 개(int x, int y)는 parameter라고 함
return x + y
} // return은 표현(x+y)도 가능하고 특정 값을 리턴할 수도, int result = x + y;같이 이 안에서 선언된 변수 자체를 넘겨줄 수도 있음
// 빼기 함수
int minus(int x, int y) { // 위의 ()안의 값이랑 별개임
return x - y
}
parameter: 내가 원하는 만큼 여러 개를 선언해서 쓸 수 있음class Calculation {
int add(int x, int y) {
return x + y;
}
int subtract(int x, int y) {
return x - y;
}
}
public class Main {
public static void main(String[] args) {
Calculation calculation = new Calculation();
int addResult = calculation.add(x: 1, y: 2); // 오류
int subtractResult = calculation.subtract(x: 5, y: 3); // 오류
System.out.println(addResult);
System.out.println(subtractResult);
}
}
// 밑에처럼 숫자만 입력해야 오류 안 뜸...
int addResult = calculation.add(1, 2);
int subtractResult = calculation.subtract(5, 3);
class Phone {
String model;
String color;
int price;
// Alt+Insert 누르면 아래 코드 쉽게 생성 가능
Phone (String model, String color, int price) { // 위의 모델 컬러 프라이스랑은 다른 값
this.model = model;
this.color = color;
this.price = price;
}
}
public class Main {
public static void main(String[] args) {
Phone galaxy = new Phone("galaxy10", "black", 100);
Phone iphone =new Phone("iphoneX", "black", 200);
System.out.println("철수는 이번에 " + galaxy.model + galaxy.color + " + 색상을 " + galaxy.price + "만원에 샀다.");
System.out.println("영희는 이번에 " + iphone.model + iphone.color + " + 색상을 " + iphone.price + "만원에 샀다.");
}
}
class DefaultValueTest {
byte byteDefaultValue;
int intDefaultValue;
short shortDefaultValue;
long longDefaultValue;
float floatDefaultValue;
double doubleDefaultValue;
boolean booleanDefaultValue;
String referenceDefaultValue;
}
public class Main {
public static void main(String[] args) {
DefaultValueTest defaultValueTest = new DefaultValueTest();
System.out.println("byte default: " + defaultValueTest.byteDefaultValue);
System.out.println("short default: " + defaultValueTest.shortDefaultValue);
System.out.println("int default: " + defaultValueTest.intDefaultValue);
System.out.println("long default: " + defaultValueTest.longDefaultValue);
System.out.println("float default: " + defaultValueTest.floatDefaultValue);
System.out.println("double default: " + defaultValueTest.doubleDefaultValue);
System.out.println("boolean default: " + defaultValueTest.booleanDefaultValue);
System.out.println("reference default: " + defaultValueTest.referenceDefaultValue);
}
}
class Animal {
String name;
public void cry() {
System.out.println(name + "is crying.");
}
}
class Dog extends Animal {
Dog(String name) {
this.name = name;
}
public void swim() {
System.out.println(name + "is swimming.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog("코코");
dog.cry();
dog.swim();
// 상속을 받는 선언부에 부모 type을 쓸 수도 있음
// Animal dog2 = dog; // 위의 Dog를 그대로 할당해줌
Animal dog2 = new Dog("미미"); // 이렇게 해도 됨 // Animal type의 dog2라는 변수인데, 실제는 이 dog로 생성한 객체
dog2.cry(); // Animal에 있는 cry는 호출할 수 있지만, dog에 있는 swim은 호출할 수 없음
// dog2.swim(); // 에러. 변수를 선언하는 type에는 Animal로 되어 있기 때문에 Animal에 있는 기능밖에 수행을 못함
}
}
// 상속을 받을 때, 여러 class를 상속 받을 수 없음. 오직 하나의 class만!
public class Main {
public static void main(String[] args) {
}
int add(int x, int y, int z) {
return x+y+z;
}
// long add(int a, int b, int c) {
// return a+b+c;
// }
// return type만 다른 경우는 overloading이라고 하지 않음
// 에러 이유. 이름과 매개변수의 개수와 타입이 모두 같음
// x,y,z ,, a,b,c는 중요하지 않음
// type의 순서, 개수만 봄
// long add(int a, int b) {
// return a+b;
// } // overloading에 해당
// int add(int a, int b) {
// return a+b;
// } // 이름만 같고 매개변수의 개수는 다르기 때문에 overloading에 해당됨
long add(int a, int b, long c) {
return a+b+c;
} // 개수는 같은데 type이 달라서 overloading에 해당됨
}
class Animal {
String name;
String color;
public Animal(String name) {
this.name = name;
}
public void cry() {
System.out.println(name + "is crying.");
}
}
class Dog extends Animal {
public Dog(String name) {
super(name);
}
@Override // 명확하게 나타내기 위해 어노테이션을 붙여줌
public void cry() {
System.out.println(name + "is barking");
} // Animal 과 똑같은데 cry 할 때 동작이 다름. crying 보다 barking 이라 하고 싶음
// dog 로 만들었을 때, dog 가 위의 부모 class 에 있던 것을 override 한 함수일 경우에는 이것만 수행함
}
public class Main {
public static void main(String[] args) {
Animal dog = new Dog("코코");
dog.cry();
}
}
알고리즘