-
생성자?Back end/Java 객체지향 2023. 11. 18. 23:05
https://cloudstudying.kr/lectures/197#tab
public class BicycleTest {
public static void main(String[] args) {
// 객체 생성 및 초기화
Bicycle b1 = new Bicycle("부릉2", 21.74, 899000);
Bicycle b2 = new Bicycle("씽씽2", 12.57, 495000);
// 객체 정보 출력
Systehttp://m.out.printf("Bicycle { %s, %.2fkg, %d원 }\n", b1.name, b1.weight, b1.price);
Systehttp://m.out.printf("Bicycle { %s, %.2fkg, %d원 }\n", b2.name, b2.weight, b2.price);
}
}
class Bicycle { // 실제 객체를 만들기 위한 설계도
// 필드
String name;
double weight;
int price;
// 생성자 - 파라미터를 통한 초기화
Bicycle(String n, double w, int p) {
name = n;
weight = w;
price = p;
}
}출력
Bicycle { 부릉2, 21.74kg, 899000원 }
Bicycle { 씽씽2, 12.57kg, 495000원 }public class GorokeTest {
public static void main(String[] args) {
/* 1. TOP 3 고로케를 객체로 만드세요. */
Goroke pizza = new Goroke("피자",1000);
Goroke yachae = new Goroke("야채",800);
Goroke pot = new Goroke("팥",500);
/* 2. 고로케 정보를 출력하세요. */
System.out.println(pizza.str());
System.out.println(yachae.str());
System.out.println(pot.str());
}
}
class Goroke {
// 필드
String name;
int price;
// 생성자
Goroke(String n, int p) {
name = n;
price = p;
}
// 메소드 - 고로케 객체의 필드 정보를 문자열로 반환!
String str() {
return String.format("Goroke { name: %s, price: %d원 }", name, price);
}
}- 출력
Goroke { name: 피자, price: 1000원 }
Goroke { name: 야채, price: 800원 }
Goroke { name: 팥, price: 500원 }public class HeroTest {
public static void main(String[] args) {
// 객체 생성
Hero ironMan = new Hero("아이언맨", 80);
Hero thanos = new Hero("타노스", 160);
Hero thor = new Hero("토르", 150);
Hero groot = new Hero("그루트", 40);
// 모든 객체 정보를 출력
System.out.println(ironMan.toStr());
System.out.println(thanos.toStr());
System.out.println(thor.toStr());
System.out.println(groot.toStr());
}
}
// Hero 클래스
class Hero {
// 필드
String name;
int hp;
// 생성자
Hero(String n, int p) { // 클래스와 생성자 이름은 같아야 함~
/* 2. 필드 값을 초기화하세요. */
this.name = n;
this.hp = p;
}
// 메소드
String toStr() {
return String.format("Hero { name: %s, hp: %d }", name, hp);
}
}- 출력
Hero { name: 아이언맨, hp: 80 }
Hero { name: 타노스, hp: 160 }
Hero { name: 토르, hp: 150 }
Hero { name: 그루트, hp: 40 }- 사라진 디폴트 생성자
public class DrinkTest {
public static void main(String[] args) {
// 디폴트 생성자로 객체 만들기
Drink d1 = new Drink(); // ERROR! 디폴트 생성자가 없기 때문에 !!
d1.name = "포카리";
d1.price = 1000;
// 다른 생성자로 객체 만들기
Drink d2 = new Drink("박카스", 800);
// 모든 객체 출력
System.out.println(d1.toStr());
System.out.println(d2.toStr());
}
}
// 드링크 객체
class Drink {
// 필드
String name;
int price;
/* 1. 디폴트 생성자를 추가하세요. */
Drink(){ }
// 생성자
Drink(String n, int p) {
name = n;
price = p;
}
// 메소드
String toStr() {
return String.format("Drink { name: %s, price: %d }", name, price);
}
}Drink { name: 포카리, price: 1000 }
Drink { name: 박카스, price: 800 }https://cloudstudying.kr/challenges/380
public class SongTest {
public static void main(String[] args) {
// 객체 생성
Song s0 = new Song("별헤는 밤", "유재하");
Song s1 = new Song("비상", "임재범");
Song s2 = new Song("비밀", "박완규");
// 객체 배열 생성
Song[] songs = { s0,s1,s2 };
// 모든 배열 속 객체 출력
for (int i = 0; i < songs.length; i++) {
/* 2. 객체 정보를 출력하세요. */
System.out.println(songs[i].toStr());
}
}
}
class Song {
// 필드
String name;
String singer;
// 생성자
Song(String n, String s) {
name = n;
singer = s;
}
// 메소드
String toStr() {
return String.format("Song { name: %s, singer: %s }", name, singer);
}
}- 출력값
Song { name: 별헤는 밤, singer: 유재하 }
Song { name: 비상, singer: 임재범 }
Song { name: 비밀, singer: 박완규 }https://cloudstudying.kr/codes/97742#code-output
public class HeroTest {
public static void main(String[] args) {
// 객체 생성
Hero thor = new Hero("토르", 150); // thor -> {"토르", 150}
Hero thanos = new Hero("타노스", 160); // thanos -> {"타노스", 160}
// 토르의 펀치 -> 타노스
thor.punch(thanos);
/* 2.코드를 추가하여 펀치를 주고 받으세요. */
thanos.punch(thor);
thanos.punch(thor);
}
}
class Hero {
// 필드
String name;
int hp;
// 생성자
Hero(String s, int i) {
name = s;
hp = i;
}
// 메소드
void punch(Hero enemy) {
// 때린 주체 객체
Systehttp://m.out.printf("[%s]의 펀치!! ", name);
/* 1. 맞은 객체에 대한 정보를 출력하세요. */
Systehttp://m.out.printf("%s의 HP: %d ->", enemy.name, enemy.hp);
enemy.hp -= 10;
Systehttp://m.out.printf("%d\n", enemy.hp);
}
}[토르]의 펀치!! 타노스의 HP: 160 -> 150
[타노스]의 펀치!! 토르의 HP: 150 -> 140
[타노스]의 펀치!! 토르의 HP: 140 -> 130출저: 홍팍 (https://www.youtube.com/@hongpark)
이 블로그 기록은 개인 공부용 기록입니다.
'Back end > Java 객체지향' 카테고리의 다른 글
인스턴스 메소드 < -> 클래스 메소드 (0) 2023.11.21 4가지 맛 붕어빵 만들기 (0) 2023.11.20 생성자[마린과 메딕] 문제 (0) 2023.11.19 메소드 스코프 문제 (0) 2023.11.18 메소드 스코프 예시 (0) 2023.11.18