class MyMath2{
//iv
long a, b;
//생성자 자동추가
MyMath2(){
super(); //생성자호출 자동추가
}
//ins메소드1
long insAdd() {
return a+b;
}
//static메소드1
static long staticAdd1(long a, long b) {
return a+b; //매개변수로 받은 지역변수lv
//iv 사용안함!!
}
}
public class MyMathTest2 {
long a=77L, b=88L;
//ins메소드2
long insAdd() {
return a+b;
}
//static메소드2
static long staticAdd1(long a, long b) {
return a+b; //매개변수
}
public static void main(String[] args) {
//ins메소드 : 객체생성+참조변수.
MyMath2 mm1 = new MyMath2(); //기본생성자
mm1.a = 11L;
mm1.b = 22L;
System.out.println("다_인스턴스메소드1 : "+mm1.insAdd());
//ins메소드 : 같은 클래스라도 객체생성+참조변수.
MyMathTest2 mm2 = new MyMathTest2();
mm2.insAdd();
System.out.println("같_인스턴스메소드2 : "+mm2.insAdd());
//static메소드 : 바로! 다른 클래스라면 +클래스명.
System.out.println("다_static메소드1 : "+MyMath2.staticAdd1(33L, 44L));
//static메소드 : 바로! 같은 클래스라면 메소드만 사용!!
System.out.println("같_static메소드2 : "+staticAdd1(55L, 66L));
}
}
다_인스턴스메소드1 : 33
같_인스턴스메소드2 : 165
다_static메소드1 : 77
같_static메소드2 : 121
class Card{
String kind;
int n;
static int length;
static int width;
//1기본생성자
// Card() { //따로 만든게 없으면 자동추가
// super(); //생성자 안 첫줄에 생성자 호출 없으면 자동추가
// }
//2기본생성자, 디폴트값 만들때1
// Card(){
// super(); //생성자 안 첫줄에 생성자 호출 없으면 자동추가
// kind = "diamond";
// n = 8;
// length = 99;
// width = 88;
// }
//3매개생성자
Card(String kind, int n, int l, int w){
super(); //생성자 안 첫줄에 생성자 호출 없으면 자동추가
this.kind = kind;
this.n = n;
length = l;
width = w;
}
//4기본생성자, 디폴트값 만들때2
//**매개생성자 만들고 this()
Card(){
this("clover", 3, 51, 61); //생성자 안 첫줄에 생성자 호출 했으므로 (O)
}
}
public class Ex6_03n {
public static void main(String[] args) {
//1기본생성자
// Card c1 = new Card();
// c1.kind = "spade";
// c1.n = 1;
// Card.length = 25;
// Card.width = 15;
// System.out.printf("Card(): %s,%d,%d,%d%n", c1.kind,c1.n,Card.length,Card.width);
// //2기본생성자
// Card c2 = new Card();
// System.out.printf("Card(): %s,%d,%d,%d%n", c2.kind,c2.n,Card.length,Card.width);
//3매개생성자
Card c3 = new Card("heart", 2, 33, 44);
System.out.printf("Card(s,i,i,i): %s,%d,%d,%d%n", c3.kind,c3.n,Card.length,Card.width);
//4매개생성자
Card c4 = new Card();
System.out.printf("Card(): %s,%d,%d,%d%n", c4.kind,c4.n,Card.length,Card.width);
}
}
//Card(): spade,1,25,15
//Card(): diamond,8,99,88
Card(s,i,i,i): heart,2,33,44
Card(): clover,3,51,61
class Data_1{
int value;
Data_1(){ //생성자가 없으면 컴파일러가 기본생성자 자동추가
super(); //생성자 첫줄에 생성자 호출이 없으면 컴파일러가 자동추가, Object클 호출
}
}
class Data_2{
int value;
Data_2(){ //이걸 추가해야 30줄이 에러가 안남
this(7); //생성자호출했으므로 ok
}
Data_2(int x){ // 매개변수 있는 생성자 만듦
super(); //자동추가, Object클 호출
value = x;
}
}
public class Ex6_11 {
public static void main(String[] args) {
Data_1 d1 = new Data_1(); //기본생성자 호출
d1.value = 3;
System.out.println(d1.value);
Data_2 d2 = new Data_2(); //기본생성자 호출인데
/*기본생성자가 아닌 생성자를 내가 만들어 놨기에, 기본생성자가 없어서 에러임
*만약 내가 만든 생성자가 없다면?? 컴파일러가 기본생성자를 만들기에 에러가 안남!
*/
System.out.println(d2.value);
Data_2 d3 = new Data_2(10); //매개변수 있는 생성자 호출
/*기본생성자가 아닌 매개변수 있는 호출인데
*위에 내가 만들었기에 에러가 안난다.
*/
System.out.println(d3.value);
}
}
3
7
10
class FCar2{
String color;
String gearType;
FCar2(String color, String gearType){
// super();
this.color = color;
this.gearType = gearType;
}
FCar2(){ //1-2. 아래방법대로 한다면+다른 생성자가 있으므로 꼭 필요함!!
// super();//1-2. 다른생성자가 없다면 자동추가 되므로 작성안해도 됨
}
}
class Car2 extends FCar2{
int door;
Car2(){ //디폴트
this("d_red", "", 4); //생성자 호출
}
Car2(String gearType){
this("new_grey", gearType, 8); //생성자 호출
}
Car2(String color, String gearType, int door){
super(); //1-1. 자동추가되기 때문에 FCar2 기본생성자 호출됨
this.color = color; //1. FCar2 기본생성자가 있으면 가능은 하나! 좋은 방법은 아님
this.gearType = gearType;
this.door = door;
}
}
super();
자동추가로 ✨✨✨조상의 기본생성자가 호출되기에, 다른 조상의 매개생성자가 있다면 내가 반드시 조상의 기본생성자를 작성해야 한다!!class FCar2{
String color;
String gearType;
FCar2(String color, String gearType){
// super();
this.color = color;
this.gearType = gearType;
}
}
class Car2 extends FCar2{
int door;
Car2(){ //디폴트
this("d_red", "", 4); //생성자 호출
}
Car2(String gearType){
this("new_grey", gearType, 8); //생성자 호출
}
Car2(String color, String gearType, int door){
super(color, gearType); //2. 조상의 생성자 호출방법이 더 좋음
this.door = door;
}
}
public class Ex6_14 {
public static void main(String[] args) {
//기본생성자+this생성자로 디폴트값
Car2 c1 = new Car2();
System.out.printf("Car2의 디폴트 : %s, %s, %d%n", c1.color, c1.gearType, c1.door);
Car2 c2 = new Car2("manual");
System.out.printf("Car2의 기어타입만 : %s, %s, %d%n", c2.color, c2.gearType, c2.door);
Car2 c3 = new Car2("orange", "half", 2);
System.out.printf("Car2의 세개 다 : %s, %s, %d%n", c3.color, c3.gearType, c3.door);
}
}
Car2의 디폴트 : d_red, , 4
Car2의 기어타입만 : new_grey, manual, 8
Car2의 세개 다 : orange, half, 2
class MyPoint3 {
int x;
int y;
String getlocation() {
return "x:"+x+", y:"+y;
}
MyPoint3(){}
MyPoint3(int x, int y){
this.x = x;
this.y = y;
}
}
class My3D extends MyPoint3 { // 상속받고
int z;
String getlocation() { // 그 클래스의 메소드를 받아서 덮어쓴다.
return "x:"+x+", y:"+y+", z:"+z;
}
// Object클래스의 toString()을 오버라이딩
public String toString() {
return "x:"+x+", y:"+y+", z:"+z;
}
My3D(){} //super();==MyPoint3(); 호출
My3D(int x, int y, int z) {
super(x, y);
this.z = z;
}
}
public class OverrideTest {
public static void main(String[] args) {
My3D p = new My3D();
p.x = 3;
p.y = 5;
p.z = 7;
System.out.println(p.getlocation());
// toString()을 오버라이딩
My3D p2 = new My3D(3,5,7);
System.out.println(p2.getlocation());
System.out.println(p2.toString());
System.out.println(p2);
}
}
x:3, y:5, z:7
x:3, y:5, z:7
x:3, y:5, z:7
x:3, y:5, z:7
이건나중에 정리하자ㅏㅏㅏㅏㅏㅏㅏㅏㅏㅏㅏㅏㅏㅏㅏㅏ
없애야 할듯
-> 인스턴스 메소드, static 메소드
-> static 메소드, 생성자
-> 인스턴스 메소드, 생성자
-> 생성자, (void)인스턴스 메소드, (void)static 메소드
. | 인스턴스 | static | 생성자 |
---|---|---|---|
반환 | O | O | X |
매개변수 | X | O | O |
iv | O | X | O |