JAVA/Java 기초

클래스와 인스턴스 등...(리마인드용)

john_ 2023. 1. 12. 12:48
728x90
class Car1{
	
	//필드(객체에 대한 변수 == 속성)
	boolean powerOn;	//false
	String color;		//null
	int wheel;			//0
	int speed;			//0
	boolean wiperOn;	//false
	
	// 메소드 (함수 == function == 기능)  변수명() <- 다 메소드입니다!
	
	void power() {powerOn = !powerOn;}
	void speedUp() {speed++;}
	void speedDown() {speed--;}
	void wiper() {wiperOn = !wiperOn;}
}

public class Car{
	public static void main(String[] args) {
		//객체가 생성되지 않으면 클래스 사용이 불가능합니다.
//		System.out.pritnln("시동 처음 초기화 : " + car1.powerOn); // 사용불가
		
		Car1 mycar = new Car1(); // car1클래스를 이용해서 mycar 라는 객체 생성
		System.out.println("시동 처음 초기화 : " + mycar.powerOn);
		System.out.println("시동 처음 초기화 : " + mycar.color);
		System.out.println("시동 처음 초기화 : " + mycar.wheel);
		System.out.println("시동 처음 초기화 : " + mycar.speed);
		System.out.println("시동 처음 초기화 : " + mycar.wiperOn);
		mycar.power();
		System.out.println("시동 메소드 동작 : " + mycar.powerOn);
		mycar.power();
		System.out.println("시동 메소드 동작 : " + mycar.powerOn);
		
		mycar.color = "black";
		System.out.println("시동 처음 초기화 : " + mycar.color);
	}
}
public class Class_practice_2 {

	public static void main(String[] args) {
		
		Car mycar1 = new Car(); // mycar1 이라는 인스턴스 객체가 생성되며 새로운 메모리의 주소를 할당받습니다.
		Car mycar2 = new Car();	// mycar2 라는 인스턴스 객체의 생성으로 새로운 메모리의 주소를 할당받습니다. 
		// Car != mycar1 != mycar2 입니다.
		
		mycar1.color = "red";
		mycar2.color = "black";
		mycar1.speedUp();
		mycar2.wiper();
		
		System.out.println("mycar1의 색: " + mycar1.color);
		System.out.println("mycar2의 색: " + mycar2.color);
		System.out.println();
		System.out.println("mycar1의 속도 : " + mycar1.speed);
		System.out.println("mycar1의 속도 : " + mycar2.speed);
		System.out.println();
		System.out.println("mycar1의 와이퍼 작동여부 : " + mycar1.wiperOn);
		System.out.println("mycar1의 와이퍼 작동여부 : " + mycar2.wiperOn);
		
		System.out.println(mycar1);
		System.out.println(mycar2);
	}
}
mycar1의 색: red
mycar2의 색: black

mycar1의 속도 : 1
mycar1의 속도 : 0

mycar1의 와이퍼 작동여부 : false
mycar1의 와이퍼 작동여부 : true
Car@15db9742
Car@6d06d69c

// 인스턴스 변수, 정적변수 예시

class Cars{
	static int wheel = 4;	// 클래스 변수, 정적멤버 (static)
	int speed;				// 인스턴스 변수, 인스턴스 멤버
}

public class Class_practice_3 {

	public static void main(String[] args) {

		System.out.println(Cars.wheel); //객체선언 없이 클래스변수 (정적멤버) 접근가능
//		System.out.println(Cars.speed); // 에러발생
		
		Cars myCar1 = new Cars();	//객체 생성
		
		System.out.println(Cars.wheel);
		System.out.println(myCar1.speed); // 인스턴스 멤버 접근가능
	
		Cars myCar2 = new Cars();
		
		System.out.println("<변경 전>");
		System.out.println("myCar1.speed : " + myCar1.speed);
		System.out.println("myCar2.speed : " + myCar2.speed);
		System.out.println("myCar1.wheel : " + myCar1.wheel);
		System.out.println("myCar2.wheel : " + myCar2.wheel);
		
		myCar2.speed = 100;	//인스턴스
		myCar2.wheel = 5;	// 정적
		
		System.out.println("<변경 후>");
		System.out.println("myCar1.speed : " + myCar1.speed);
		System.out.println("myCar2.speed : " + myCar2.speed);
		System.out.println("myCar1.wheel : " + myCar1.wheel);
		System.out.println("myCar2.wheel : " + myCar2.wheel);
		
	}
}

멤버와 필드 ?

멤버 : 필드와 메소드

필드 : String abc 같은 변수(속성)

메소드 : 실제 작동하는 기능부분(function), 메소드 명은 소문자로 시작하게합니다.

    또한 메소드명 뒤에 ()을 붙여줘야합니다.

생성자는 반드시 있어야합니다.
(없으면 자동생성됨)

 


// 클래스 메소드와 인스턴스 메소드 차이
class Area {
	static void manual() {	// 정적 메소드 생성 : 바로 사용 가능, void : 반환되는 값이 없다.
		System.out.println("현재 사용가능한 함수 목록");
		System.out.println("triangle : 삼각형 넓이");
		System.out.println("rectangle : 사각형 넓이");
		System.out.println("입니다.");
	}
	
	double triangle(int a, int b) {	//인스턴스 메소드
		return (double)a * b/2;
	}
	
	int rectangle(int a, int b) { //인스턴스 메소드
		return a * b;
	}
	
}

public class Method2 {

	public static void main(String[] args) {
	
		Area.manual();
		
		Area area1 = new Area();
		// Area.triangle(3, 5);
		// Area.rectangle(3, 4);
		area1.triangle(3, 5);
		area1.rectangle(3, 4);
		
		System.out.println(area1.triangle(3, 5));
	}
}

 

인스턴스 멤버의 클래스 멤버 사용 -> 가능
클래스 멤버의 인스턴스 멤버 사용 -> 에러 

// 클래스멤버는 생성되는 순간 메모리에 로딩되어 인스턴스 멤버가 사용이 가능.
// 인스턴스 멤버는 선언이 되어야 메모리에 로딩이 되므로 메모리상에 실제로는 존재하지않기 때문에
// 클래스 멤버가 사용할수 없습니다.
-------------------------------------------------------
인스턴스 멤버의 인스턴스 멤버 사용 -> 가능
클래스 멤버의 클래스 멤버 사용 - > 가능

오버라이딩 - 

public class Overloading {

	static int sum(int a, int b) {
		System.out.println("인자가 둘일 경우 호출됩니다.");
		return a+b;
	}
	
	static int sum(int a, int b, int c) {
		System.out.println("인자가 셋일 경우 호출됩니다.");
		return a+b+c;
	}
	static double sum(double a, double b, double c) {
		System.out.println("double 타입일 경우 호출됩니다.");
		return a+b+c;
	}
	
	
	public static void main(String[] args) {

		Overloading.sum(20,40);
		Overloading.sum(3.4, 2.5, 5.23);
		System.out.println();
		
		System.out.println(sum(20,40));
		System.out.println(sum(5,6,7));
		System.out.println(sum(5.2,6.3,7.1));
	}
}

 

매개변수를 이용한 생성자 

= 기본생성자 + 필드값 초기화

클래스 이름(매개변수){ }
class Cellphone {
	String model = "Galaxy 8";
	String color;
	int capacity;
	
//	Cellphone(){
//	}

	Cellphone(String color, int capacity){
		this.color = color;
		this.capacity = capacity;
	}
}


public class Constructor1 {

	public static void main(String[] args) {
		
//		Cellphone myphone1 = new Cellphone();  // Cellphone 에서 Cellphone에 대한 다른 해당되는 생성자가 없어서 생성불가
	
		Cellphone myphone2 = new Cellphone("Silver",5);
		
		System.out.println(myphone2.model);
		System.out.println(myphone2.color);
		System.out.println(myphone2.capacity);
	}
}

 

매개병수가 있는 생성자가 작성되면 기본 생성자는 무시됩니다.

Cellphone(){
 this.phone="galaxy";
 this.capacity = 5;
 }		// 다음과 같이 생성자에서 속성값을 부여할수도 있습니다
728x90

'JAVA > Java 기초' 카테고리의 다른 글

메소드 재정의.jav  (0) 2023.01.13
오버라이딩(overriding).java  (0) 2023.01.12
상속.java  (1) 2023.01.12
static 이란?  (0) 2023.01.11
싱글톤 패턴.java  (0) 2023.01.11