본문 바로가기
Programming/Java, Spring

[Java 기초 문법] 자바에서의 객체 지향 관점 Object Oriented Language

by Renechoi 2022. 10. 16.

 

역할을 분담하는 것

 

응집도는 강하게 결합은 느슨하게 

 

 

 

다음과 같은 예시 

 

 

 

 

Person 코드 

 

package model;

import java.time.LocalDate;

public class Person {
    private String firstName;
    private String lastName;
    private LocalDate dob;
    private Address address;
    private Person spouse;

    public Person getSpouse() {
        return spouse;
    }

    public void setSpouse(Person spouse) {
        this.spouse = spouse;
    }

    public Person(Person spouse) {
        this.spouse = spouse;
    }

    public Person(String firstName, String lastName, LocalDate dob) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.dob = dob;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public LocalDate getDob() {
        return dob;
    }
}

 

 

Address 코드 

 

package model;

public class Address {
    private String address1;
    private String address2;
    private String city;
    private String state;

    public Address(String address1, String address2, String city, String state) {
        this.address1 = address1;
        this.address2 = address2;
        this.city = city;
        this.state = state;
    }

    public String getAddress1() {
        return address1;
    }

    public void setAddress1(String address1) {
        this.address1 = address1;
    }

    public String getAddress2() {
        return address2;
    }

    public void setAddress2(String address2) {
        this.address2 = address2;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }
}

 

 

 

메인 코드 

 

import model.Person;

import java.time.LocalDate;

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World");

        Person tom = new Person("Tome","Smith", LocalDate.of(1990,6,15));
        Person janet = new Person("Janet", "Jackson", LocalDate.of(1985,12,4));

        tom.setSpouse(janet);


    }
}

 

메인코드에서 person을 불러온다

 

 

 

 

반응형