Do it 자바 프로그래밍 입문 연습문제 6장
·
Programming Language/자바(JAVA)
Q5 public class Kim { String name; String coffee; int money; public Kim(String name, String coffee, int money) { this.name = name; this.coffee = coffee; this.money = money; } public void takecoffee(Star st) { st.take(4000,this.coffee); this.money -= 4000; } public void showInfo() { System.out.println("이름 : " + name + " / 커피 : " + coffee + " / 남은 돈 : " + money); } } public class Star { String cof..
Do it! 자바 프로그래밍 입문 / 6장 클래스와 객체 2
·
Programming Language/자바(JAVA)
6.1 this 예약어 this : 인스턴스(객체) 스스로를 가리키는 예약어 즉, this == 참조 변수가 가리키는 힙에 저장된 인스턴스의 주소 class BirthDay { String name; int day, month, year; public void printThis() { System.out.println(this); // this == bDay(인스턴스가 저장된 힙 주소) } BirthDay() { // this == bDay == Birthday. 그냥 특정 인스턴스의 Birthday라고 생각하면 됨 this("이름 없음", 0, 0, 0); // 디폴트 생성자에서 바로 아래 생성자를 호출 } BirthDay(String name, int day, int month, int year) {..
Do it! 자바 프로그래밍 입문 / 5.5 생성자 ~ 5.7 정보 은닉
·
Programming Language/자바(JAVA)
public class Student_Test { public static void main(String[] args) { Student st = new Student(); st.setName("토자맨"); System.out.println(st.getName()); } } 5.5 생성자 package chapter5; public class Person { String name; float height; public static void main(String[] args) { Person cho = new Person(); } } new Person() 의 Person()이 (디폴트)생성자이다. 생성자는 클래스 객체를 생성할 때 멤버 변수, 상수를 초기화 하는 역할을 한다. 디폴트 생성자는 생성자를 하..