유디의 공간정보·개발일기

2. 0706_생성자(Constructor), Getter and Setter, 상속 본문

JAVA

2. 0706_생성자(Constructor), Getter and Setter, 상속

55yudi 2021. 10. 20. 23:09

- Class명 (변수1, 변수2, 변수3,,,) { }

- 클래스명과 이름이 같은 함수 만들기 --> 생성자 함수

 

- 생성자(Constructor) : 변수의 초기값을 위한 것

- package 에 주소를 적어주기

 

- 생성자 함수에는 void를 명시하지 않는다.

 

- 파라미터가 없는 생성자 함수와 파라미터가 있는 생성자 함수

 

package [com.lx.container] : Sea

package [com.lx.fish] : Fish, Shark

 

[Sea class]

package com.lx.container;

import com.lx.fish.Fish;
import com.lx.fish.Shark;

public class Sea {

	public String name;

	public Fish fish;
	
	public Shark shark;
}

- public : 공용으로 사용하기 위함, 다른 클래스에서도 사용할 수 있다.

- Fish와 Shark는 다른 클래스에서 가져와야 하기 때문에 import를 넣어준다.

 

[Fish class]

package com.lx.fish;

public class Fish {
	String name;
}

[Shark class]

package com.lx.fish;

public class Shark {
	String name;
}

 

- 실행하기

[Test1]

import com.lx.container.Sea ;
import com.lx.fish.Fish;
import com.lx.fish.Shark;

public class Test1 {

	public static void main(String[] args) {
		
		//각각의 Class 지정하기
		Sea sea1 = new Sea();
		//sea1.name = "태평양";
		System.out.println ("바다가 만들어 졌다");

		Fish fish1 = new Fish();
		sea1.fish = fish1;
		System.out.println("물고기를 바다에 넣는다");

		Shark shark1 = new Shark();
		sea1.shark = shark1;
		System.out.println("상어도 바다에 넣는다");
	}
}

- 생성자 함수 사용해서 다른 방법으로 실행하기

 

[Sea class]

package com.lx.container;

import com.lx.fish.Fish;
import com.lx.fish.Shark;

public class Sea {

	public String name;
	public Fish fish;
	public Shark shark;
	
	public Sea(String name, Fish fish, Shark shark){
		this.name = name;
		this.fish = fish;
		this.shark = shark;
	}
}

- public, import 써주기

- 생성자 함수에 변수초기값으로 3개의 구멍을 만들고 값을 던져줌

 

[Test1]

import com.lx.container.Sea;
import com.lx.fish.Fish;
import com.lx.fish.Shark;

public class Test1 {
	public static void main(String[] args) {
		
		Fish fish1 = new Fish();
		System.out.println("물고기 한마리를 바다로");

		Shark shark1 = new Shark();
		System.out.println("상어 한마리를 바다로");

		Sea sea1 = new Sea("태평양", fish1, shark1);
		System.out.println("바다가 만들어졌다");
	}
}

 

 

- Setter and Getter

- SetName : 값이 들어갈 구멍, 앞에 void 붙음

- GetName : 값이 나옴, return

 

[Sea class]

package com.lx.container;

import com.lx.fish.Fish;
import com.lx.fish.Shark;

public class Sea {
	
		public String name;
		public Fish fish;
		public Shark shark;
	
		public Sea() { //Test1에서 new sea를 만들기 때문에 넣음
		}

		public void setFish(Fish fish) {
				this.fish = fish;
		}

		public Fish getFish() {
				return this.fish;
		}
}

 

[Sea class]

package com.lx.fish;

public class Fish {
		public String name;
}

[Shark class]

package com.lx.shark;

public class Shark {
		public String name;
}

 

[Test1]

import com.lx.container.Sea;
import com.lx.fish.Fish;
import com.lx.fish.Shark;

public class Test1 {
		public static void main(String[] args) {
				
				Fish fish1 = new Fish();
				fish1.name = "니모";
				System.out.println ("니모를 바다로!");
				
				Shark shark1 = new Shark();
				System.out.println ("상어도 바다로!");

				Sea sea1 = new Sea();
				sea1.name = "대서양";
				sea1.shark = shark1;
				sea1.setFish(fish1);

				System.out.println("바다의 이름은 " + sea1.name);

				Fish fish2 = sea1.getFish();
				System.out.println("그곳에 사는 물고기의 이름은 " + fish2.name);
	}
}

 


[Test2]_[Test1]

public class Test1 {

	public static void main(String[] args) {

		Person person1 = new Person("홍길동", 20, "010-1000-1000");
		/*
		person1.name = "홍길동";
		person1.age = 20;
		person1.mobile = "010-1000-1000";
		*/

		person1.walk(10);

		Person person2 = new Person();
		person2.name = "홍길동";
        person2.age = 20;
		person2.mobile = "010-1000-1000";

	}
}

 

[Person]

public class Person {
	String name;
	int age;
	String mobile;
	
	Person() {
		System.out.println("Person 함수 실행됨");
		
	}
	
	Person(String name, int age, String mobile) {
		System.out.println("Person 함수 실행됨");
		
		this.name = name;
		this.age = age;
		this.mobile = mobile;
			
	}
	
	
	void run(int distance) {
		System.out.println("사람이 뛰어갑니다. : " + distance + "km");
		
	}
	
	void walk(int distance) {
		System.out.println("사람이 걸어갑니다. : " + distance + "km");
		
		run(5);
		
	}
}

 


[Test4]_[Test1]

import com.lx.house.House;
import com.lx.person.Baby;
import com.lx.person.Person;

public class Test1 {

	public static void main(String[] args) {
		//count2 = 0;
		
	//setName
		Person person1 = new Person();
		//Person.count = 1;
		//count2 = 1;
		
		person1.setName("홍길동");
	//getName
		String name = person1.getName();
		System.out.println("첫번째 사람의 이름 : " + name);
		
		Person person2 = new Person();
		//Person.count = 2;
		//count2 = 2;
		System.out.println("두번째 사람의 이름 : " + Person.count);
				
		Baby baby1 = new Baby(); //extends Person 상속함
		baby1.setName("김아기");
		
		House house1 = new House();
		house1.person1 = person1;
        
		System.out.println("집 안에 있는 첫번째 사람의 이름 : " + house1.person1.getName());
		

	}
}

 

 

[House]

package com.lx.house;

import com.lx.person.Baby;
import com.lx.person.Person;

public class House {
	
	public String name;
	
	public Person person1;
	
	public Person person2;
	
	public Baby baby1;
	

}

 

[Person]

package com.lx.person;

public class Person {
	
	public static int count = 0;
    
	String name;
	
	public Person() {
		System.out.println("person생성자함수 실행됨.");
		
		count = count + 1;
	}
		public void setName(String name) {
		this.name = name;
	}
	
	public String getName() {
		return this.name;
	}

}

 

[Baby] - Person을 상속함

package com.lx.person;

public class Baby extends Person {

}

'JAVA' 카테고리의 다른 글

4. 0708_생성자함수, if문 (리뷰)  (0) 2021.10.23
3. 0707_review  (0) 2021.10.20
1. 0705_Java 개요, 문법  (0) 2021.10.20
[JAVA] Collection : List, Set, Map  (0) 2021.08.13
[Java] Wrapper Class(래퍼 클래스)  (0) 2021.08.12