package two;
class Fruit {
private int apple; //5 //10
private int straw; //10 //15
private int grapes; //15 //20
public void setFruit(int ap, int st, int gr) {
apple = ap; //객체 참조 변수
straw = st;
grapes = gr;
}
public int getFruit() {
int result = apple + straw + grapes;
return result;
}
}
public class Ex2 {
public static void main(String[] args) {
int quantity1, quantity2;
Fruit f1 = new Fruit();
Fruit f2 = f1;
f1.setFruit(5, 10, 15);
quantity1 = f1.getFruit();
quantity2 = f2.getFruit();
System.out.println("객체 f1의 초기 과일 개수 " + quantity1 + "개"); //객체 f1의 초기 과일 개수 30개
System.out.println("객체 f2의 초기 과일 개수 " + quantity2 + "개"); //객체 f2의 초기 과일 개수 30개
f2.setFruit(10, 15, 20);
quantity1 = f1.getFruit();
quantity2 = f2.getFruit();
System.out.println("객체 f1의 값 변동 후 개수 " + quantity1 + "개"); //객체 f1의 값 변동 후 개수 45개
System.out.println("객체 f2의 값 변동 후 개수 " + quantity2 + "개"); //객체 f2의 값 변동 후 개수 45개
}
}
CallAnotherConst.java
package two;
class Person {
String name; //null //이름 없음
int age; //0 //1
Person() {
this("이름 없음", 1);
}
Person(String name, int age) {
this.name = name;
this.age = age;
}
Person returnItSelf() {
return this;
}
}
public class CallAnotherConst {
public static void main(String[] args) {
Person noName = new Person();
System.out.println(noName.name);//이름 없음
System.out.println(noName.age);//1
Person p = noName.returnItSelf();
System.out.println(p); //two.Person@73a28541
System.out.println(noName); //two.Person@73a28541
}
}
ThisMain.java
package two;
class ThisTest {
int x, y;//0,0 //10,20 //30,10
public ThisTest() { this(10, 10); }
public ThisTest(int x) { this(x, 10); }
public ThisTest(int x, int y) {
this.x = x;
this.y = y;
}
public void setX(int x) { this.x = x;}
public void showPoint() {System.out.println(x + ", " + y);}
}
public class ThisMain {
public static void main(String[] args) {
ThisTest t01 = new ThisTest(10, 20);
t01.showPoint(); //10, 20
ThisTest t02 = new ThisTest(30);
t02.showPoint(); //30, 10
ThisTest t03 = new ThisTest();
t03.showPoint(); //10, 10
}
}
ThisEx.java
package two;
class This {
int num =1; //2
String str = "한글"; //테스트
public void setNum(int num, String str) {
this.num = num ;
this.str = str ;
}
public int getNum() {
return num;
}
public int getNum(int j) {
this.num = num * j;
return num ;
}
public void amd() {
int num = 5;
int str = 7;
if (num <=5) {
//int num =3;
System.out.println("this.num 는 " + this.num); //1 //2
//String str ="wow";
System.out.println("this.str는 " + this.str); //한글 //테스트
}
System.out.println("num 는 " + num ); //5
System.out.println("str는 " + str); // 7
System.out.println();
}
}
public class ThisEx {
public static void main(String[] args) {
This a = new This();
a.amd();
a.setNum(2, "테스트");
a.amd();
int x = a.getNum();
System.out.println(x); //2
int y = a.getNum(5);
System.out.println(y); //10
}
}
this.num 는 1
this.str는 한글
num 는 5
str는 7
this.num 는 2
this.str는 테스트
num 는 5
str는 7
2
10
MyDateTest.java
package two;
class MyDate {
private int y, m, d;
private String str;
private void setMyDate(int r) {
if (r == 1) str = "유효한 날짜입니다.";
else str = "유효하지 않은 날짜입니다.";
}
public String getMyDate() { return str; }
public MyDate(int d, int m , int y) {
this.y = y; this.m = m; this.d = d;
}
public void isValid() {
int r;
switch (m) {
case 4: case 6: case 9: case 11:
r = (d <=30) ? 1 : 0;
break;
case 2:
if ( y % 4 == 0) {
r = (d <= 29) ? 1 : 0;
break;
} else {
r = (d <= 28) ? 1 : 0;
break;
}
default :
r = (d <=31) ? 1 : 0;
}
setMyDate(r);
}
}
public class MyDateTest {
public static void main(String[] args) {
MyDate id = new MyDate(31, 5, 2021);
id.isValid();
System.out.println(id.getMyDate());
}
}