back/Java

KH 14일차

devel0per 2022. 7. 19. 18:40
package ja_0719;

interface Mbc{
	void play();
}

class kbs{
	
	
	void autoPlay(Mbc a)

	{
		a.play();
	}
}


class MySbs implements Mbc{

	@Override
	public void play() {
		System.out.println("Mysbs TV 생방송");
		
	}
	
	

}
class MyCNN implements Mbc
{

	@Override
	public void play() {
		System.out.println("Mysbs TV 야구 생중계");
		
	}
	
}

public class InterfaceTest_3 {
	
	public static void main(String[] args) {
		
		kbs obj = new kbs();
		
		obj.autoPlay(new MySbs());
		
		obj.autoPlay(new MyCNN());
		
	}

}

오늘은 상속, 인터페이스, 싱글톤패턴에 대해서 학습했다.

 

(1) 상속

public class SuperTest_1 {
	public static void main(String[] args) {
		
		Child obj_1 = new Child();
		
		obj_1.message();
		
	}

}

class Parent{
	
	int x = 888;
}

// 부모클래스에 상속된 자식클래스와 변수 이름이 중복되어 있으면 this를 사용하지 않는 한 자기 자신의 변수가 출력되지만 중복이 되면 부모클래스 속의 변수가 출력.

class Child extends Parent{
	
	int xx = 3456;
	void message() {
		System.out.println("x = " + x);
		System.out.println("this.x = " + this.xx);
		System.out.println("super.x = " + super.x);
	}
}

부모클래스에서 상속된 서브클래스가 중복된 변수명을 가지고 있다면 그냥 출력하면 부모클래스의 변수가 출력된다!

 

 

class AA_5{
	
	String str;
	boolean bbb;
	char sex = ' ';
	
	
	public AA_5() {
	}
	
	public AA_5(String str, boolean bbb, char sex) {
		this.str = str;
		this.bbb = bbb;
		this.sex = sex;
		
		System.out.println("AA_5 생성자 호출 ~~~ : " + str + "boolean : " + bbb + "sex" + "m : " + sex);
	}
}

class BB_5 extends AA_5{
	public BB_5(String aa, boolean bbb, char sex) {
		super(aa, bbb, sex);
		System.out.println("BB_5 생성자 호출 $$$ : " + aa + "boolean : " + bbb+ "m : " + sex);
	}
}

class CC_5 extends BB_5{
	public CC_5(String kbs, boolean bbb, char sex) {
		super(kbs, bbb, sex);
		System.out.println("CC_5 생성자 호출 $$$ : " + kbs + "boolean : " + bbb+ "m : " + sex);
	}
}



public class SuperTest_2 {
	public static void main(String[] args) {
		
		CC_5 obj_1 = new CC_5("monday + 1", true, 'm');
		
		
		System.out.println(obj_1.str);
		System.out.println(obj_1.bbb);
		System.out.println(obj_1.sex);
		
	}

}

(2) 인터페이스

 

package ja_0719;


interface AA_1{
	
	// 머리만 있고 몸통만 있는데 abstract 안 붙혀도됨. interface는 모두 추상메소드를 가지고 있기 때문이다.
	void print1();
	
}

class BB_1 implements AA_1{

	@Override
	public void print1() {
		System.out.println("AA_1 인터페이스 메소드 재정의");		
	}
	
	public void print2() {
		System.out.println("BB_1 클래스 메소드 print2()");		
	}

}
public class InterfaceTest_1 {
	public static void main(String[] args) {
		
		BB_1 obj_1 = new BB_1();
		
		obj_1.print1();
		obj_1.print2();
		
		System.out.println("===================================");
		
		
		AA_1 obj_2 = new BB_1();
		

		obj_2.print1();
		// print2는 BB_1	의 자체 메서드이기 때문에 AA_1객체인 obj_2 는 사용할 수 없다.
//		obj_2.print2();
	}
}

 

package ja_0719;



interface AA_2{
	
	int kbs = 999; // static final int kbs = 999; 인터페이스 안의 수는 상수이다 값변경이안됨
	
}

class BB_2 implements AA_2{
	
	int mbc = 345;
	int kbs = 789;
	
}


public class InterfaceTest_2 {
	public static void main(String[] args) {
		
		BB_2 obj_1 = new BB_2();
		
		obj_1.kbs = 345;
		obj_1.mbc = 678;
		
		System.out.println("kbs : " + obj_1.kbs);
		System.out.println("kbs22 : " + AA_2.kbs);	
		System.out.println("mbc : " + obj_1.mbc);
		
		AA_2 obj_2 = new BB_2();
		System.out.println(obj_2.kbs);
		
		
	}

}

(3) 싱글톤패턴

 

package ja_0719;

//상속금지.

final class Singleton{
	
	// 소프트웨어 디자인 패턴에서 싱클턴 패턴을 따르는 클래스는,
	// 생성자가 여러 차례 호출되더라도 실제로 생성되는 객체는 하나이고
	// 최초 생성 이후에 호출된 생성자는 최초의 생성자가 생성한 객체를 리턴한다.
	// 이와 같은 디자인 유형을 싱글턴 패턴이라고 한다.
	// 주로 공통된 객체를 여러개 생성해서 사용하는 DBCT (DataBase Connection Pool)와 같은
	// 상황에서 많이 사용된다.
	
	
private Singleton() {
		
}	

private static Singleton ss = new Singleton();
	

public static Singleton getInstance() {
	if (ss == null) {
			
		ss = new Singleton();
			
	}
	return ss;
	}
	
}
	
	
class HelloWorld{
	
}

public class SingletonTest{
	public static void main(String[] args) {
		
		// Singleton obj = new Singleton();
		// private 인 싱글톤을 호출하려고 메서드를 이용해서 호출하고 있다.
		Singleton ss1 = Singleton.getInstance();
		Singleton ss2 = Singleton.getInstance();
		Singleton ss3 = Singleton.getInstance();
		
		System.out.println("Singleton 객체 ss1 hashCode " + ss1.hashCode());
		System.out.println("Singleton 객체 ss1 hashCode " + ss1);
		System.out.println("Singleton 객체 ss2 hashCode " + ss2.hashCode());
		System.out.println("Singleton 객체 ss2 hashCode " + ss2);
		System.out.println("Singleton 객체 ss3 hashCode " + ss3.hashCode());
		System.out.println("Singleton 객체 ss3 hashCode " + ss3);
		
		System.out.println("=================================================");
		
		HelloWorld hh1 = new HelloWorld();
		HelloWorld hh2 = new HelloWorld();
		
		System.out.println("HelloWorld 객체 hh1 hashcode" + hh1.hashCode());
		System.out.println("HelloWorld 객체 hh1 hashcode" + hh1);
		System.out.println("HelloWorld 객체 hh2 hashcode" + hh2.hashCode());
		System.out.println("HelloWorld 객체 hh2 hashcode" + hh2);
	}
}
댓글수0