본문 바로가기
java

0929_생성자

by 신방동불주먹 2022. 9. 29.
public class Car1Test {//0929-4 생성자

	public static void main(String[] args) {
		
		
//		Car1 c1 = new Car1(); // Car1()객체 생성과 동시에 생성자가 호출됨.
//		
//		//인스턴스 초기화 방법1
//		c1.color = "red";
//		c1.gearType = "Auto";
//		c1.door = 4;
//		
//		System.out.println(c1.color);
//		System.out.println(c1.gearType);
//		System.out.println(c1.door);
//		
//		
//		Car1 c2 = new Car1();
//		System.out.println(c2.color);
//		System.out.println(c2.gearType);
//		System.out.println(c2.door);
		
		Car1 c3 = new Car1("red");
		System.out.println(c3.color);
		System.out.println(c3.gearType);
		System.out.println(c3.door);
		
		Car1 c4 = new Car1("blue","auto");
		System.out.println(c4.color);
		System.out.println(c4.gearType);
		System.out.println(c4.door);
		
		Car1 c5 = new Car1("yellow","auto",3);
		System.out.println(c5.color);
		System.out.println(c5.gearType);
		System.out.println(c5.door);
		
		Car1 c6 = new Car1("green");
		System.out.println(c6.color);
	}

}​
public class Car1 {//0929-4 생성자
	//속성
	String color;
	String gearType;
	int door;
	
	
	//생성자 : 멤버변수(인스턴스)를 초기화 한다.
	//		클래스의 이름과 동일하게 작성.
	//		객체를 생성하는 시점에 한번 호출되어진다
	//		기본 생성자는 생략 가능 (다른생성자가 존재하지 않을때)
	//		생성자 오버로딩
	//		모든 클래스는 생성자를 포함한다. (안보이면 기본생성자)
	

//	Car1(){ //기본생성자
//	}
	
	Car1(){ //다른 생성자가 있는 경우 기본생성자를 직접 작성해야함 혹은 c1,c2를 못쓰게?
		
	}
	
//	//인스턴스를 초기화 방법2
//	Car1(){
//		color ="blue";
//		gearType = "st";
//		door = 3;
//	}
	
	
	//생성자 오버로딩
//	Car1(String c){ //매개변수 c로 들어온 값을 color를 초기화
//		color = c;
//	}
	
	//this ----------5 멤버변수와 매개변수의 변수명이 같을 때
	Car1(String color){
		this.color = color; //this.멤버변수 = 매개변수로 받아온 값의 변수 이름 
	}
	
	Car1(String color,String gearType){
		this.color = color;
		this.gearType = gearType;
	}
	
	Car1(String color,String gearType,int door){
		this.color = color;
		this.gearType = gearType;
		this.door = door;
	}

}

-------------------------------------------생성자 사용

 

public class Student { //0929 ----6 생성자 사용
	String name;
	int age;
	int grade;

	//생성자
	Student(String name,int age,int grade){
		this.name = name;
		this.age = age;
		this.grade = grade;
	}
	
	



}
public class StudentTest { //0929 ----6 생성자 사용

	public static void main(String[] args) {
		Student s1 = new Student("홍길동",10,3);
		//생성자의 편리함 
/*		s1.name = "홍길동";
 * 		s1.age = 10;
 * 		s1.grade = 3;
	
		
*/		
		Student s2 = new Student("이순신",10,3);
		Student s3 = new Student("강감찬",10,3);
		
		System.out.println(s1.name);
		System.out.println(s1.age);
		System.out.println(s1.grade);
	
		System.out.println(s2.name);
		System.out.println(s2.age);
		System.out.println(s2.grade);
		
		System.out.println(s3.name);
		System.out.println(s3.age);
		System.out.println(s3.grade);
	}

}

------------------------------------생성자간 호출

 

	//이 생성자로 받은 color와 다른 생성자의 gearType을 적어서 해당 생성자를 호출
	Car1(String color){
		//this.color = color;
		this(color,"Auto"); //-----------------------0929-7 생성자간 호출  
	}
		
	Car1(String color,String gearType){
//		this.color = color;
//		this.gearType = gearType;
		this(color,gearType,6);
	}
	
	Car1(String color,String gearType,int door){
		this.color = color;
		this.gearType = gearType;
		this.door = door;
	}

}

'java' 카테고리의 다른 글

0929_초기화블럭  (0) 2022.09.29
0929_this. / this()  (0) 2022.09.29
[java] 오버로딩  (0) 2022.09.29
0929_static method  (0) 2022.09.29
0928_최대값 출력 메소드(연습)  (0) 2022.09.28