on
java 수업 DAY 13
java 수업 DAY 13
class Student { String name = "홍길동"; int score = 100; double height = 180.0; boolean adult = true; }
class TestStudent { public static void main(String[] args) { // 값을 미리 주면 그 값만 나온다 Student s = new Student(); Student s2 = new Student(); System.out.println(s.name); System.out.println(s2.name); } }
class Student { String name; int score; double height; boolean adult; // 생성자는 필드를 초기화 함. public Student() { // 기본 생성자 / 숨겨져 있던 생성자 // 리턴 타입이 없음 클래스 이름이랑 똑같음 // 기본 생성자는 필드를 기본값으로 초기화 name = null; score = 0; height = 0/0; adult = false; } }
class TestStudent { public static void main(String[] args) { Student s = new Student(); // 생성자(constructor) 호출 (호출 = 똑같이 생긴거 따라가는 것) Student s2 = new Student(); System.out.println(s.name); // Student()가 초기화해서 값이 나옴 = null } }
참조형 = null
정수형 = 0
실수형 = 0.0
논리형 - false
char 은 숫자 0에 해당하는 문자를 가짐 - 완전 작으만한 동그라미
기본 생성자에 초기화를 하면 그걸로 나온다
class Student { String name; int score; double height; boolean adult; public Student (String n, int a, double h, boolean isA) { name = n; age = a; height = h; adult = isA; } }
class TestStudent { public static void main(String[] args) { Student s = new Student("홍길동", 22, 180.0, true); System.out.println(s.name); System.out.println(s.age); System.out.println(s.height); System.out.println(s.adult); } }
// 책상 // 제조사 // 생산일자 // 가격 // 높이 조절 가능 여부 // ----- // 위와 같은 필드를 초기화하는 생성자를 작성 class Desk { String manu; String date; int price; boolean heightOk; public Desk (String n, String d, int p, boolean ok) { manu = n; date = d; price = p; heightOk = ok; } public void printAll() { System.out.println(manu); System.out.println(date); System.out.println(price); System.out.println(heightOk); } }
public class TestDesk { public static void main(String[] args) { Desk s = new Desk("이케아", "12년 03월 29일", 100, false); s.printAll(); // 1. Desk의 printAll System.out.println(s.manu); System.out.println(s.date); System.out.println(s.price); System.out.println(s.heightOk); } }
위 상황에서
Desk s2 = new Desk(); 는 에러가 난다 = 이럴땐 기본생성자가 필요하나는 것을 적어야 실행가능
public Desk() {}를 Desk에 적어놔야함
public Desk() {
this(); // 내 생성자 호출
}
class Desk { String manu; String date; int price; boolean heightOk; public Desk() { // 생성자 안에서는 다른 "내" 생성자 호출 가능 // 주의점 : 첫문장 이어야함. this("내맘대로", "오늘", 10000, true); } public Desk (String n, String d, int p, boolean ok) { manu = n; date = d; price = p; heightOk = ok; } public void printAll() { System.out.println(manu); System.out.println(date); System.out.println(price); System.out.println(heightOk); } }
class Dog { final String breed; // 상수의 표현이 final을 붙여서 상수의 표현으로 바꿈 String name; int age; public Dog (String breed, String n, int a) { // 파라미터 값도 이름이 있으면 더 좋음 // 다만 이름이 같으면 가로안의 String breed이고 breed = breed일때 // 가장 가까운 breed를 찾음 파라미터 값이랑 필드 값을 구분 시켜주는데 그때 this.breed // this 내꺼 this.breed = breed; name = n; age = a; } public void printAll() { System.out.println(breed); System.out.println(name); System.out.println(age); } }
class TestDog { public static void main(String[] args) { Dog d = new Dog("푸들", "초코", 3); // (d.breed = "요크셔테리어"; // 변수의 형태라 변경이 가능) d.printAll(); } }
// 비행기 // 필드 (제작사, 모델, 최대승객수) //-------- // 생성자 (모든 필드를 초기ㅘ 가능) //메소드 1.각 필드 값을 반환 받을 수 있는 메소드 // 2. 각 필드 값을 변경할 수 있는 메소드 class Airplane { String manufacturer; String model; int max; public Airplane(String manufacturer, String model, int max) { this.manufacturer = manufacturer; this.model = model; this.max = max; } public void printAll() { System.out.println(manufacturer); System.out.println(model); System.out.println(max); } // getter (접근자) public String getManufacturer() { return manufacturer; } public String getModel() { return model; } public int getMax() { return max; } // setter(설정자) public void setManufacturer(String manufacturer) { this.manufacturer = manufacturer; } public void setModel(String model) { this.model = model; } public void setMax(int max) { this.max = max; } }
class TestAirplane { public static void main(String[] args) { Airplane p = new Airplane("보잉", "B747", 450); Airplane a1 = new Airplane("에어버스", "A380", 550); p.printAll(); a1.printAll(); int pMax = p.getMax(); System.out.println(pMax); p.setManufacturer("보보이잉"); System.out.println(p.getManufacturer()); p.setModel("B747~400"); p.setMax(400); System.out.println(p.getModel()); System.out.println(p.getMax()); } }
from http://hiapprendre.tistory.com/26 by ccl(A) rewrite - 2021-12-13 23:01:32