각각의 변수는 주소 값이 있는데 이것을 reference변수다...static은 reference변수(주소값)가 없어서 this 사용 불가능
this는 자기 자신의 reference변수(주소값)
명시적생성자...3개의 인자값을 수정과 동시에 생성하는 생성자
이것만 저장하면 오류가 뜨기 때문에 디폴트 생성자를 만들어줘서 오류를 없앰
public MyDate(int y, int m, int d) {
year=y;
month=m;
day=d;
}
이것만 저장하면 오류가 뜨기 때문에 디폴트 생성자를 만들어줘서 오류를 없앰
디폴트 생성자 숨어있다..명시적 생성자 만들면 생성
자동완성 할때 Class 변수 앞에만 쓰고 자동완성 constructor로 하면 자동 완성 가능
디폴트 생성자
public MyDate() {
}
package day0628;
//각각의 변수는 주소 값이 있는데 이것을 reference변수다...static은 reference변수(주소값)가 없어서 this 사용 불가능
//this는 자기 자신의 reference변수(주소값)
class MyDate{
//instance 변수
private int year;
private int month;
private int day;
//디폴트 생성자 숨어있다..명시적 생성자 만들면 생성
//자동완성 할때 Class 변수 앞에만 쓰고 자동완성 constructor로 하면 자동 완성 가능
public MyDate() {
}
//명시적생성자...3개의 인자값을 수정과 동시에 생성하는 생성자
//이것만 저장하면 오류가 뜨기 때문에 위 디폴트 생성자를 만들어줘서 오류를 없앰
public MyDate(int y, int m, int d) {
year=y;
month=m;
day=d;
}
//화면 오른쪽 클릭->source->generate getter and setter ->get set할 인자 선택 ->generate
//getter,setter
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
}
public class AutoSetGet_01 {
public static void main(String[] args) {
//객체생성
//이 형식을 더 많이 쓴다
MyDate date=new MyDate();
date.setYear(2023);
date.setMonth(6);
date.setDay(22);
System.out.println(date.getYear()+"-"+date.getMonth()+"-"+date.getDay());
//명시적생성
MyDate date1=new MyDate(2020, 8, 10);
System.out.println(date1.getYear()+"-"+date1.getMonth()+"-"+date1.getDay());
}
}
알아만 둬도 괜찮다
package day0628;
//궁금증만 해소하고 잊어도 되는 코드
//Calendar때 만들었던 것이 어떻게 만들어졌는지 보기위해서 한 코드
class Test{
public Test() {
System.out.println("디폴트 생성자");
}
//생성자 대신 instance를 반환해주는 메서드
//Test라는 클래스 반환
public static Test getInstance()
{
return new Test();
}
//일반 메서드
public void writeMessage()
{
System.out.println("안녕??");
}
}
//////////////////////////////////////////////////
public class ConstGetInstance_02 {
public static void main(String[] args) {
Test t1=new Test(); //다른 패키지라면 오류가 날 것임
t1.writeMessage();
//////////////////////////////////////
Test t2=Test.getInstance();
t2.writeMessage();
}
}
클래스
class Test2{
int n=0;
static int cnt=0;
public Test2() {
System.out.println("생성자호출");
n++; //instance 메모리
cnt++; //static 메모리
}
public void write()
{
System.out.println("cnt="+cnt+"\tn="+n);
}
}
메인
public class VarTest_03 {
public static void main(String[] args) {
Test2 t1=new Test2();
t1.write();
Test2 t2=new Test2();
t2.write();
Test2 t3=new Test2();
t3.write();
}
}
결과값
생성자호출
cnt=1 n=1
생성자호출
cnt=2 n=1
생성자호출
cnt=3 n=1
--한꺼번에 return 받기...String값만 return한 경우
public String getData()
{
String s="상품명: "+sangpumName+"\t입고날짜: "+ipgoday+"\t출고일자: "+chulgoday;
return s;
}
--한꺼번에 return 받았을때 메인 출력
MyShop shop1=new MyShop();
shop1.setData("monitor","2023-01-01","2023-05-30");
System.out.println(shop1.getData());
클래스
class MyShop{
private String sangpumName;
private String ipgoday;
private String chulgoday;
//3개의 데이터를 한 번에 수정할 수 있는 메서드
public void setData(String s, String i, String c) {
this.sangpumName=s;
this.ipgoday=i;
this.chulgoday=c;
}
//3개의 데이터를 한 번에 출력할 수 있게 메서드
/*public void getData() {
System.out.println("품명: "+sangpumName+"\n입고일: "+ipgoday+"\n출고일: "+chulgoday);
}
*/
//한꺼번에 return 받기...String값만 return
public String getData()
{
String s="상품명: "+sangpumName+"\t입고날짜: "+ipgoday+"\t출고일자: "+chulgoday;
return s;
}
}
메인
public class QuizShop_05 {
public static void main(String[] args) {
System.out.println("***상품입고***");
/*MyShop in1=new MyShop();
in1.setMyshop("키보드", "2023-06-01", "2023-06-20");
in1.getMyshop();
System.out.println("---------------------------------------");
MyShop in2=new MyShop();
in2.setMyshop("Monitor", "2023-06-04", "2023-06-23");
in2.getMyshop();*/
//한꺼번에 return 받았을떄
MyShop shop1=new MyShop();
shop1.setData("monitor","2023-01-01","2023-05-30");
System.out.println(shop1.getData());
}
}
setter 명시적생성자
public Student(String n, String hp, int g) {
this.sname=n;
this.hp=hp;
this.grade=g;
}
배열 값 설정(따로따로 set)
자료형 참조변수명[]=new 자료형[갯수];
클래스명 참조변수명[]=new 클래스명[갯수];
Student stu[]=new Student[3];
stu[0]=new Student("최성현", "010-9773-7503", 5);
stu[1]=new Student("이성신", "010-2352-4356", 4);
stu[2]=new Student("진현규", "010-8234-3426", 2);
클래스명 참조변수명[]={new 클래스명(인자값1),new 클래스명(인자값2)}
배열 값 설정(한번에 set)
Student stu[]= {
new Student("최성현", "010-9773-7503", 5),
new Student("이성신", "010-2352-4356", 4),
new Student("진현규", "010-8234-3426", 2)};
출력#1 for문
for(int i=0;i<stu.length;i++)
{
Student s=stu[i];
s.getData();
//==stu[i].getData();
System.out.println("------------------------------");
}
출력#2 for~each
//for(자료형 참조변수명:배열)
//for(클래스명 참조변수명:배열)
for(Student s1:stu)
{
s1.getData();
System.out.println("==============================");
}
클래스
class Student{
private String sname;
private String hp;
private int grade;
//명시적 생성자
public Student(String n, String hp, int g) {
this.sname=n;
this.hp=hp;
this.grade=g;
}
//멤버값 출력 데이터
//int String 섞여서 return값 받기보다는 한 번에 출력값 넣는게 낫다
public void getData() {
System.out.println("이름: "+sname);
System.out.println("연락처: "+hp);
System.out.println("학년: "+grade+"학년");
}
메인
public class ArrObject_09 {
public static void main(String[] args) {
//자료형 참조변수명[]=new 자료형[갯수];
//클래스명 참조변수명[]=new 클래스명[갯수];
/*Student stu[]=new Student[3];
stu[0]=new Student("최성현", "010-9773-7503", 5);
stu[1]=new Student("이성신", "010-2352-4356", 4);
stu[2]=new Student("진현규", "010-8234-3426", 2);
*/
//클래스명 참조변수명[]={new 클래스명(인자값1),new 클래스명(인자값2)}
Student stu[]= {
new Student("최성현", "010-9773-7503", 5),
new Student("이성신", "010-2352-4356", 4),
new Student("진현규", "010-8234-3426", 2)};
//출력#1 for문
for(int i=0;i<stu.length;i++)
{
Student s=stu[i];
s.getData();
//==stu[i].getData();
System.out.println("------------------------------");
}
//출력#2 for~each
//for(자료형 참조변수명:배열)
//for(클래스명 참조변수명:배열)
for(Student s1:stu)
{
s1.getData();
System.out.println("==============================");
}
}