public class Student {
public void study() {
Book book = new Book();
System.out.println(book.getName()+"을/를 공부한다.");
}
}
public class Book {
private String name= "영어";
public String getName() {
return this.name;
}
}
public class Test {
public static void main(String[] args) {
Student student = new Student();
student.study();
}
}
---
결과 : 영어을/를 공부한다.
public class Book {
// private String name= "영어";
private String name= "수학";
public String getName() {
return this.name;
}
}
결과 : 수학을/를 공부한다.
public class Student {
public void study() {
//Book book = new Book();
//System.out.println(book.getName()+"을/를 공부한다.");
Youtube youtube = new Youtube();
System.out.println(youtube.getName()+"을/를 공부한다.");
}
}
public class Youtube {
private String name= "꽃꽂이";
public String getName() {
return this.name;
}
}
결과 : 꽃꽂이을/를 공부한다.
public class Student {
private Resources resources;
public Student(Resources resources){
this.resources = resources;
}
public void study() {
System.out.println(resources.getThings()+"을/를 공부했다");
}
}
public interface Resources {
String getThings();
void setThings(String things);
}
public class Book implements Resources {
private String things;
@Override
public String getThings() {
return this.things;
}
@Override
public void setThings(String things) {
this.things = things;
}
}
public class Test {
public static void main(String[] args) {
Resources resources = new Book();
resources.setThings("국어");
Student student = new Student(resources);
student.study();
}
}
결과 : 국어을/를 공부했다
public class Test {
public static void main(String[] args) {
Resources resources = new Book();
//resources.setThings("국어");
resources.setThings("과학");
Student student = new Student(resources);
student.study();
}
}
결과 : 과학을/를 공부했다
public class Youtube implements Resources{
private String things;
@Override
public String getThings() {
return this.things;
}
@Override
public void setThings(String things) {
this.things = things;
}
}
public class Test {
public static void main(String[] args) {
// Resources resources = new Book();
// resources.setThings("국어");
Resources resources = new Youtube();
resources.setThings("요리");
Student student = new Student(resources);
student.study();
}
}
결과 : 요리을/를 공부했다
1) 클래스가 다른 클래스에 영향을 받는 것과 매개변수를 통해 값을 변경하는 것.
2) DI란?
3) 인터페이스의 장점
4) IoC란?
public class Test {
public static void main(String[] args) {
Resources resources = new Youtube();
resources.setThings("요리");
Student student = new Student(resources);
student.study();
}
}