class BirthDay {
int day;
int month;
int year;
// 태어난 연도를 지정하는 메서드
public void setYear(int year) {
// bDay.year = year;과 같음
this.year = year;
}
// this 출력 메서드
public void printThis() {
// System.out.println(bDay);와 같음
System.out.println("this : " + this);
}
}
public class ThisExample {
public static void main(String[] args) {
BirthDay bDay = new BirthDay();
// 태어난 연도를 2021로 지정
bDay.setYear(2021);
// 참조 변수출력
System.out.println("bDay : " + bDay);
// this 출력 메서드 호출
bDay.printThis();
}
}
class Person {
String name;
int age;
Person(){
// this를 사용해 Person(String, int) 생성자 호출
this("이름 없음", 1);
}
Person(String name, int age){
this.name = name;
this.age = age;
}
}
public class CallAnotherConst {
public static void main(String[] args) {
Person noName = new Person();
System.out.println("noName.name : " + noName.name);
System.out.println("noName.age : " + noName.age);
}
}
class Person {
String name;
int age;
Person(){
// this를 사용해 Person(String, int) 생성자 호출
this("이름 없음", 1);
}
Person(String name, int age){
this.name = name;
this.age = age;
}
// 반환형은 클래스형
Person returnItSelf() {
// this 반환
return this;
}
}
public class CallAnotherConst {
public static void main(String[] args) {
Person noName = new Person();
System.out.println("noName.name : " + noName.name);
System.out.println("noName.age : " + noName.age);
System.out.println("==============================");
// this 값을 클래스 변수에 대입
Person p = noName.returnItSelf();
// noName.returnItSelf()의 반환 값 출력
System.out.println("p : " + p);
// 참조 변수 출력
System.out.println("noName : " + noName);
}
}
public class Student {
// 학생 이름
public String studentName;
// 학년
public int grade;
// 학생이 가지고 있는 돈
public int money;
// 학생이름과 가진 돈을 매개변수로 받는 생성자
public Student(String sutendtName, int money) {
this.studentName = studentName;
this.money = money;
}
// 학생이 버스를 타면 1,000원을 지불하는 기능을 구현한 메서드
public void takeBus(Bus bus) {
bus.take(1000);
this.money -= 1000;
}
// 학생이 지하철을 타면 1,500원을 지불하는 기능을 구현한 메서드
public void takeSubway(Subway subway) {
subway.take(1500);
this.money -= 1500;
}
// 학생의 현재 정보를 출력하는 메서드
public void showInfo() {
System.out.println(studentName + "님의 남은 돈은 " + money + "원 입니다.");
}
}
public class Bus {
// 버스 번호
int busNumber;
// 승객 수
int passengerCount;
// 버스 수입
int money;
// 버스 번호를 매개변수로 받는 생성자
public Bus(int busNumber) {
this.busNumber = busNumber;
}
// 승객이 버스에 탄 경우를 구현한 메서드
public void take(int money) {
// 버스 수입 증가
this.money += money;
// 승객 수 증가
passengerCount++;
}
// 버스 정보를 출력하는 메서드
public void showInfo() {
System.out.println("버스 " + busNumber + "번의 승객은 " + passengerCount + "명이고, 수입은" + money + "원 입니다.");
}
}
public class Subway {
// 지하철 노선
String lineNumber;
// 승객 수
int passengerCount;
// 수입액
int money;
// 지하철 노선 번호를 매개변수로 받는 생성자
public Subway(String lineNumber) {
this.lineNumber = lineNumber;
}
// 승객이 지하철에 탄 경우를 구현한 메서드
public void take(int money) {
// 지하철 수입 증가
this.money += money;
// 승객 수 증가
passengerCount++;
}
// 지하철 정보를 출력하는 메서드
public void showInfo() {
System.out.println(lineNumber + "번 승객은 " + passengerCount + "명이고, 수입은" + money + "원 입니다.");
}
}
public class TakeTrans {
public static void main(String[] args) {
// 학생 두 명 생성
Student studentRuin = new Student("Ruin", 5000);
Student studentArk = new Student("Ark", 10000);
// 노선 버스가 8번인 버스 생성
Bus bus8 = new Bus(8);
// Ruin이 8번 버스를 탐
studentRuin.takeBus(bus8);
// Ruin 정보 출력
studentRuin.showInfo();
// 버스 정보 출력
bus8.showInfo();
System.out.println("===============================================");
// 노선 번호가 3호선인 지하철 생성
Subway subwayBlue = new Subway("3호선");
// Ark가 3호선을 탐
studentArk.takeSubway(subwayBlue);
// Ark 정보 출력
studentArk.showInfo();
// 지하철 정보 출력
subwayBlue.showInfo();
}
}
public class Student {
// static 변수는 인스턴스 생성과 상관없이 먼저 생성됨
public static int serialNum = 1000;
public int studentID;
public String studentName;
public int grade;
public String address;
public String getStudentName() {
return studentName;
}
public void setStudentName(String name) {
studentName = name;
}
}
public class StudentTest1 {
public static void main(String[] args) {
Student studentYou = new Student();
studentYou.setStudentName("유중혁");
// serialNum 변수의 초깃값 출력
System.out.println("studentYou.serialNum : " + studentYou.serialNum);
// static 변수 값 증가
studentYou.serialNum++;
Student studentLee = new Student();
studentLee.setStudentName("이지혜");
// 증가된 값 출력
System.out.println("studentLee.serialNum : " + studentLee.serialNum);
System.out.println("studentYou.serialNum : " + studentYou.serialNum);
}
}
public class Student {
public static int serialNum = 1000;
public int studentID;
public String studentName;
public int grade;
public String address;
// 생성자
public Student() {
// 학생이 생성될 때마다 serialNum 증가
serialNum++;
// 증가된 값을 학번 인스턴스 변수에 부여
studentID = serialNum;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String name) {
studentName = name;
}
}
public class StudentTest2 {
public static void main(String[] args) {
Student studentKim = new Student();
studentKim.setStudentName("김독자");
System.out.println("studentYou.serialNum : " + studentKim.serialNum);
System.out.println(studentKim.studentName + " 학번 :" + studentKim.studentID);
System.out.println("============================");
Student studentLee = new Student();
studentLee.setStudentName("이상아");
System.out.println("studentYou.serialNum : " + studentLee.serialNum);
System.out.println(studentLee.studentName + " 학번 :" + studentLee.studentID);
}
}
public class StudentTest1 {
public static void main(String[] args) {
Student studentKim = new Student();
studentKim.setStudentName("김독자");
// serialNum 변수를 직접 클래스 이름으로 참조
System.out.println("Student.serialNum : " + Student.serialNum);
System.out.println(studentKim.studentName + " 학번 :" + studentKim.studentID);
System.out.println("============================");
Student studentLee = new Student();
studentLee.setStudentName("이상아");
// serialNum 변수를 직접 클래스 이름으로 참조
System.out.println("Student.serialNum : " + Student.serialNum);
System.out.println(studentLee.studentName + " 학번 :" + studentLee.studentID);
}
}
public class Student {
// private 변수로 변경
private static int serialNum = 1000;
public int studentID;
public String studentName;
public int grade;
public String address;
// 생성자
public Student() {
// 학생이 생성될 때마다 serialNum 증가
serialNum++;
// 증가된 값을 학번 인스턴스 변수에 부여
studentID = serialNum;
}
// serialNum의 get( ) 메서드
public static int getSerialNum() {
int i = 10;
return serialNum;
}
// serialNum의 set( ) 메서드
public static void setSerialNum() {
Student.serialNum = serialNum;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String name) {
studentName = name;
}
}
public class StudentTest2 {
public static void main(String[] args) {
Student studentKim = new Student();
studentKim.setStudentName("김독자");
// serialNum 값을 가져오기 위해 get( ) 메서드를 클래스 이름으로 직접 호출
System.out.println("Student.getSerialNum : " + Student.getSerialNum());
System.out.println(studentKim.studentName + " 학번 :" + studentKim.studentID);
System.out.println("============================");
Student studentLee = new Student();
studentLee.setStudentName("이상아");
// serialNum 값을 가져오기 위해 get( ) 메서드를 클래스 이름으로 직접 호출
System.out.println("Student.getSerialNum : " + Student.getSerialNum());
System.out.println(studentLee.studentName + " 학번 :" + studentLee.studentID);
}
}
public class StudentTest1 {
public static void main(String[] args) {
System.out.println("Student.getSerialNum : " + Student.getSerialNum());
}
}
1) 지역 변수(로컬 변수, local variable) : 함수나 메서드 안에서만 사용
2) 멤버 변수(인스턴스 변수, insatance variable) : 클래스 안에서 사용
3) static 변수(클래스 변수, class variable) : 여러 인스턴스에서 공통으로 사용
public class Company {
// 단계 1 : private 생성자 생성
private Company() { }
}
public class Company {
// 단계 2 : 유일하게 생성한 인스턴스
private static Company instance = new Company();
// 단계 1 : private 생성자 생성
private Company() { }
}
public class Company {
// 단계 2 : 유일하게 생성한 인스턴스
private static Company instance = new Company();
// 단계 1 : private 생성자 생성
private Company() { }
// 단계 3 : 인스턴스를 외부에서 참조할 수 있도록 public get( ) 메서드 구현
public static Company getInstance() {
if(instance == null) {
instance = new Company();
}
// 유일하게 생성한 인스턴스 반환
return instance;
}
}
public class CompanyTest {
public static void main(String[] args) {
// 클래스 이름으로 getInstance( ) 호출하여 참조 변수에 대입
Company myCompany1 = Company.getInstance();
Company myCompany2 = Company.getInstance();
// 두 변수가 같은 주소인지 확인
System.out.println(myCompany1 == myCompany2);
}
}