=== Car Info ===
name: a
type: suv
이동!
정지!
생성자 출력!
=== Car Info ===
name: b
type: sedan
짐을 주세요!
빵빵!
// Practice
// 다음 클래스 직접 만든 후 객체 생성
// 클래스명: Animal
// 특성: 이름, 무게, 분류
// 기능: 먹기, 잠자기, 걷기, 뛰기
class Animal {
String name;
Double weight;
String classification;
Animal(String name, Double weight, String classification) {
this.name = name;
this.weight = weight;
this.classification = classification;
}
public void printInfo() {
System.out.println("name = " + name);
System.out.println("weight = " + weight);
System.out.println("classification = " + classification);
}
public void eat() {
System.out.println("냠냠!");
}
public void sleep() {
System.out.println("쿨쿨!");
}
public void walk() {
System.out.println("걷기!");
}
public void run() {
System.out.println("뛰기!");
}
}
public class Practice {
public static void main(String[] args) {
// Test code
Animal animal1 = new Animal("강아지", 5.0, "포유류");
Animal animal2 = new Animal("구피", 0.01, "어류");
animal1.printInfo();
animal2.printInfo();
}
}
name = 강아지
weight = 5.0
classification = 포유류
name = 구피
weight = 0.01
classification = 어류
import car.Car2;
class Car {
String name;
String type;
Car(String name, String type) {
this.name = name;
this.type = type;
}
public void printCarInfo() {
System.out.println("=== Car Info ===");
System.out.println("name: " + name);
System.out.println("type: " + type);
}
public void printCarInfo(String date) {
printCarInfo();
System.out.println("date: " + date);
}
public void printCarInfo(int number) {
printCarInfo();
System.out.println("number: " + number);
}
public void printCarInfo(String date, int number) {
printCarInfo();
System.out.println("date: " + date);
System.out.println("number: " + number);
}
}
class Car3 {
static String name = "None";
String type;
Car3(String name, String type) {
this.name = name;
this.type = type;
}
public void printCarInfo() {
System.out.println("=== Car Info ===");
System.out.println("name: " + name);
System.out.println("type: " + type);
}
public static void getName() {
System.out.println("Car name: " + name);
// System.out.println("Car type: " + type);
}
}
public class Main {
public static void main(String[] args) {
Car myCar1 = new Car("a", "sedan");
myCar1.printCarInfo();
// 1. 오버로딩
myCar1.printCarInfo("2022");
myCar1.printCarInfo(1);
myCar1.printCarInfo("2022", 1);
// 2. 접근 제어자
System.out.println("=== 접근 제어자 ===");
Car2 myCar2 = new Car2("a", "b", "c", "d");
System.out.println("name1: " + myCar2.name1);
System.out.println("name2: " + myCar2.name2);
System.out.println("name3: " + myCar2.name3);
System.out.println("name4: " + myCar2.name4);
// 3. Static
System.out.println("=== Static ===");
Car3.getName();
Car3 myCar3_1 = new Car3("a", "sedan");
Car3 myCar3_2 = new Car3("b", "suv");
Car3 myCar3_3 = new Car3("c", "rv");
myCar3_1.printCarInfo();
myCar3_2.printCarInfo();
myCar3_3.printCarInfo();
Car3.getName();
}
}
=== Car Info ===
name: a
type: sedan
=== Car Info ===
name: a
type: sedan
date: 2022
=== Car Info ===
name: a
type: sedan
number: 1
=== Car Info ===
name: a
type: sedan
date: 2022
number: 1
=== 접근 제어자 ===
name1: a
name2: b
name3: c
name4: d
=== Static ===
Car name: None
=== Car Info ===
name: c
type: sedan
=== Car Info ===
name: c
type: suv
=== Car Info ===
name: c
type: rv
Car name: c
// Practice
// 오버로딩
class Calculator {
public int sum(int a, int b) {
return a + b;
}
public double sum(double a, double b) {
return a + b;
}
public double sum(String a, String b) {
return Double.parseDouble(a) + Double.parseDouble(b);
}
public int sum(int a, int b, int c) {
return a + b + c;
}
}
public class Practice {
public static void main(String[] args) {
// Test code
Calculator c = new Calculator();
System.out.println(c.sum(1, 2));
System.out.println(c.sum(1.0, 2.0));
System.out.println(c.sum("1", "2"));
System.out.println(c.sum(1, 2, 3));
}
}
Leave a comment