| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
| 29 | 30 | 31 |
Tags
- 전화번호부
- 별 찍기
- 오라클
- 환경설정
- 무결성 제약조건
- Ajax
- 자바
- 스프링
- 백준문제풀이
- 깃허브 블로그
- 오류모음
- jsp
- 파워서플라이
- 이클립스
- Oracle
- 이클립스단축기
- for문
- 오류
- 순환문
- 인터페이스
- 백준문제
- 백준
- ORA-02292
- 설정
- while
- 공부
- spring
- 티스토리 블로그
- ORA-01407
- MSI
Archives
- Today
- Total
danDevlog
Java - day6 본문
728x90
package package1;
public class Test2 {
int num1;
int num2;
public static void main(String[] args) {
Test2 t = new Test2(); // 기본 생성자의 호출
// test2 클래스를 통해서 객체 m을 선언하고 생성.
// int a=5처럼, test2 자료형의 변수 t를 가르킴.
// t 변수가 저장될 수 있는 공간을 메모리에 확보 == 객체 생성.
t.addFunc(t.num1, t.num2);
System.out.println("차감 : " + t.subtraction(1500, 1350));
t.multiplication(12, 5);
t.divide(16, 4);
t.remain(30, 8);
}
// 생성자는 메모리에 클래스 타입의 공간을 확보하는 역할.
// 기본 생성자
// 매개 변수가 있는 생성자
// 생성자 특징. 리턴 타입이 없고, 클래스명과 동일한 특별한 메소드
public Test2() {} // 기본 생성자는 명시하지 않으면, 컴파일 과정에서 자동으로 생성.
// 매개변수가 있는 생성자를 구현하면, 디폴트 생성자를 생성하지 않음.
public Test2(int a, int b) {
num1 = a;
num2 = b;
}
// static : 객체 생성 이전에 메모리에 올라감
// static 이 없는 메소드는 객체 생성후 호출하여 사용.
// void 키워드는 리턴 타입이 없다.
public void addFunc(int a, int b) {
// TODO Auto-generated method stub
System.out.println("합계 : " + (a+b));
}
// 빼기, 곱하기, 나누기,나머지 연산자 메소드 구현해서 제출
// return : 메소드를 호출한 쪽으로 결과를 돌려보냄.
public int subtraction(int a, int b) {
// return 키워드를 사용하고 돌려보내는 자료형을 메소드명 앞에 명시함.
return a-b;
}
public void multiplication(int a, int b) {
// TODO Auto-generated method stub
System.out.println("곱셈 : " + (a*b));
}
public void divide(int a, int b) {
// TODO Auto-generated method stub
System.out.println("나눗셈 : " + (a/b));
}
public void remain(int a, int b) {
// TODO Auto-generated method stub
System.out.println("나머지 : " + (a%b));
}
}
package package1;
// 전화번호 1단계
public class PhoneBook {
public static void main(String[] args) {
PhoneInfo plnfo1 = new PhoneInfo("홍길동", "111-2222", "90/08/30");
// 매개변수 3개 처리하는 생성자가 없음
PhoneInfo plnfo2 = new PhoneInfo("이순신", "222-3333");
// 매개변수 2개 처리하는 생성자가 없음
plnfo1.showPhoneInfo();
plnfo2.showPhoneInfo();
// 해당 클래스에 메소드가 존재하지 않는다.
}
}
class PhoneInfo{
private String name;
private String phoneNum;
private String birth;
public PhoneInfo() {}
public PhoneInfo(String a, String b) {
name = a;
phoneNum = b;
}
public PhoneInfo(String a, String b, String c) {
name = a;
phoneNum = b;
birth = c;
}
public void showPhoneInfo() {
// TODO Auto-generated method stub
if(birth == null) {
System.out.printf(name + "\t" + phoneNum + "\t" + "생일 정보가 없습니다");
}else {
System.out.printf(name + "\t" + phoneNum + "\t" + birth);
System.out.println();
}
}
}
import java.util.Scanner;
// 전화번호 2단계
public class PhoneBook {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(true) {
System.out.println("-----------------------");
System.out.println("1. 데이터 입력");
System.out.println("2. 프로그램 종료");
System.out.println("-----------------------");
int choice = sc.nextInt();
if(choice != 2) {
sc.nextLine(); // 엔터 소거용 코드, 안하면 입력이 씹힘
System.out.print("이름 : ");
String name2 = sc.nextLine();
System.out.print("전화번호 : ");
String phoneNum = sc.nextLine();
System.out.print("생년월일 : ");
String birth = sc.nextLine();
PhoneInfo plfo2 = new PhoneInfo(name2, phoneNum, birth);
plfo2.showPhoneInfo();
}else {
System.out.println("프로그램 종료");
break;
}
}
sc.close();
}
}
class PhoneInfo{
private String name;
private String phoneNum;
private String birth;
public PhoneInfo() {}
public PhoneInfo(String a, String b) {
name = a;
phoneNum = b;
}
public PhoneInfo(String a, String b, String c) {
name = a;
phoneNum = b;
birth = c;
}
public void showPhoneInfo() {
// TODO Auto-generated method stub
if(birth == null) {
System.out.printf(name + "\t" + phoneNum + "\t" + "생일 정보가 없습니다");
System.out.println();
}else {
System.out.println("입력된 정보 출력");
System.out.println("이름\t: " + name);
System.out.println("전화번호\t: " + phoneNum);
System.out.println("생일\t: " + birth);
}
}
}
public class PhoneBook {
private int num1 = 5;
public int num2 = 10;
// prievate의 개념
public static void main(String[] args) {
// 오버로드 혹은 오버로딩
// 메소드의 이름은 같지만, 전달값의 갯수나 타입이 다른 경우,
// println 메소드가 대표 경우,
PhoneBook pb = new PhoneBook();
pb.num1 = 10;
pb.num2 = 20;
System.out.println(pb.num1);
System.out.println(pb.num2);
PhoneInfo pi = new PhoneInfo();
// pi.num3=100;// The field PhoneInfo.num3 is not visible
pi.num4 = 200;
pi.setNum3(100);
System.out.println(pi.getNum3());
}
}
class PhoneInfo {
private int num3 = 15;
public int num4 = 25;
public void setNum3(int a) {
num3 = a;
}
public int getNum3() {
return num3;
}
}
this와 this()의 차이점
class PhoneInfo{
// this 키워드 사용하기.
private String name;
private String phoneNum;
private String birth;
public PhoneInfo() {
this("","",""); // 이때는 매개변수 3개인 생성자를 호출
}
public PhoneInfo(String name, String phoneNum) {
this.name = name;
this.phoneNum = phoneNum;
}
public PhoneInfo(String name, String phoneNum, String birth) {
this.name = name;
this.phoneNum = phoneNum;
this.birth = birth;
}
}
this는 인스턴스 자신을 가리키는 참조 변수이고 this() 는 생성자를 뜻한다.
PhoneInfo() 생성자 안에서의 this.name은 인스턴스 변수이고, name은 매개변수로 정의된 지역변수이다.
static 메서드에서는 this를 사용하지 못한다.
this()는 같은 클래스의 다른 생성자를 호출할 때 사용한다. 위 코드의 생성자들은 this()를 통해 모두 생성자를 호출하고 있는 것이다. 때문에 this("","",""); 는 매개변수가 3개인 생성자를 호출하는 의미이다.
변형하여 this("","")는 매개변수가 2개인 생성자를 호출한다.
'Java' 카테고리의 다른 글
| Java - day8(전화번호부 3단계) (1) | 2022.02.24 |
|---|---|
| Java - day7 (0) | 2022.02.23 |
| Java - day5 (0) | 2022.02.21 |
| Java - day4 (0) | 2022.02.18 |
| Java - day3 (0) | 2022.02.17 |
Comments