ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Java] μžλ°” 클래슀, 객체
    Android/Java 2021. 1. 18. 00:54
    λ°˜μ‘ν˜•
    • 이 글은 "온라인 μžλ°” μŠ€ν„°λ”” λ‚΄μš©"을 μ •λ¦¬ν•œ κΈ€μž…λ‹ˆλ‹€.

    ν΄λž˜μŠ€λž€?

    • ν΄λž˜μŠ€λž€ 객체지ν–₯ ν”„λ‘œκ·Έλž˜λ°μ—μ„œ 객체λ₯Ό μ •μ˜ν•˜κ³  μ‚¬μš©ν•  수 μžˆλ„λ‘ μ„€κ³„ν•˜λŠ” 의미둜 μ‚¬μš©λ©λ‹ˆλ‹€.

    • μžλ°”μ—μ„œλŠ” 클래슀λ₯Ό 가지고 , μ—¬λŸ¬ 객체λ₯Ό μƒμ„±ν•˜κ³  μ‚¬μš©ν•©λ‹ˆλ‹€.

    • ν΄λž˜μŠ€λŠ” 객체의 μƒνƒœλ₯Ό λ‚˜νƒ€λ‚΄λŠ” λ³€μˆ˜(variable)와 객체의 행동을 μ •μ˜ν•˜λŠ” λ©”μ†Œλ“œ(method)둜 κ΅¬μ„±λ©λ‹ˆλ‹€.

    • μžλ°”μ—μ„œλŠ” main을 μ‹€ν–‰ν•˜κΈ° μœ„ν•΄μ„œλ„ μ΅œμ†Œν•œ ν•˜λ‚˜μ˜ ν΄λž˜μŠ€κ°€ μ‘΄μž¬ν•΄μ—¬ μ‹€ν–‰ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

    public class Point {
    
        private int y;
        private int x;
    
        public Point(int y, int x) {
            this.y = y;
            this.x = x;
        }
    
        public int getY() {
            return y;
        }
    
        public int getX() {
            return x;
        }
    
        public void setY(int y) {
            this.y = y;
        }
    
        public void setX(int x) {
            this.x = x;
        }
    }

    λ‹€μŒκ³Ό 같이 클래슀λ₯Ό μ •μ˜ν•  수 μžˆμŠ΅λ‹ˆλ‹€. ν΄λž˜μŠ€μ— ν¬ν•¨λœ λ³€μˆ˜λ“€λ‘œ κ³΅ν†΅λœ 속성을 μ •μ˜ν•˜κ³  λ©”μ†Œλ“œλ₯Ό μ΄μš©ν•˜μ—¬ νŠΉμ • 행동을 ν•  수 있게 λ©λ‹ˆλ‹€.


    κ°μ²΄λž€?

    • μ‹€μƒν™œμ—μ„œ νŠΉμ • 사물듀을 ν”„λ‘œκ·Έλž˜λ°μœΌλ‘œ ν‘œν˜„ν•˜λŠ” 것을 객체라고 ν•©λ‹ˆλ‹€.

    • μ΄λŸ¬ν•œ 객체의 μƒνƒœμ™€ 행동을 κ΅¬μ²΄ν™”ν•˜λŠ” ν˜•νƒœμ˜ ν”„λ‘œκ·Έλž˜λ°μ΄ 객체지ν–₯ν”„λ‘œκ·Έλž˜λ°μž…λ‹ˆλ‹€.

    • 클래슀둜 μ •μ˜κ°€ 된 ν›„, new ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ 객체 생성을 ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

    • 객체λ₯Ό μƒμ„±ν•˜μ—¬ λ©”λͺ¨λ¦¬μ— ν• λ‹Ήλ˜λŠ”λ° λ©”λͺ¨λ¦¬μ˜ ν• λ‹Ήλœ 객체λ₯Ό μΈμŠ€ν„΄μŠ€λΌκ³  ν•©λ‹ˆλ‹€.

    public class Main {
        public static void main(String[] args) {
            // μΈμŠ€ν„΄μŠ€ 생성
            Point point = new Point(2,3);
            Point point2 = new Point(3,4);
        }
    }

    λ©”μ†Œλ“œ

    • λ©”μ†Œλ“œλž€ μ–΄λ– ν•œ νŠΉμ • μž‘μ—…μ„ μˆ˜ν–‰ν•˜κΈ° μœ„ν•œ λͺ…λ Ήλ¬Έμ˜ 집합이라고 ν•©λ‹ˆλ‹€.

    • 클래슀 내뢀에 μ„ μ–Έλœ λ©”μ†Œλ“œλŠ” 객체λ₯Ό μ΄μš©ν•˜μ—¬ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

    • 객체지ν–₯의 정보 은닉 νŠΉμ§•μ€ μ‚¬μš©μžκ°€ ꡳ이 μ•Œ ν•„μš”κ°€ μ—†λŠ” μ •λ³΄λŠ” μ‚¬μš©μžλ‘œλΆ€ν„° μˆ¨κ²¨μ•Ό ν•œλ‹€λŠ” κ°œλ…μž…λ‹ˆλ‹€.

    • μ ‘κ·Ό μ œμ–΄μžλ₯Ό μ‚¬μš©ν•˜μ—¬ 정보 은닉 κΈ°λŠ₯을 μ œκ³΅ν•  수 μžˆμŠ΅λ‹ˆλ‹€. (ex. private or public)

    public class Point {
    
        private int y;
        private int x;
    
        public Point(int y, int x) {
            this.y = y;
            this.x = x;
        }
    
        // μ—¬λŸ¬ λ©”μ†Œλ“œκ°€ μ‘΄μž¬ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
        public void sum(Point p){
            this.y += p.y;
            this.x += p.x;
        }
    
        public void sub(Point p){
            this.y -= p.y;
            this.x -= p.x;
        }
    
        public void mul(Point p){
            this.y *= p.y;
            this.x *= p.x;
        }
    
        public void div(Point p){
            this.y /= p.y;
            this.x /= p.x;
        }
    
        public int getY() {
            return y;
        }
    
        public int getX() {
            return x;
        }
    
        public void setY(int y) {
            this.y = y;
        }
    
        public void setX(int x) {
            this.x = x;
        }
    }
    public class Main {
        public static void main(String[] args) {
    
            // μΈμŠ€ν„΄μŠ€ 생성
            Point point = new Point(2,3);
            Point point2 = new Point(3,4);
    
            // sum λ©”μ†Œλ“œ μ‹€ν–‰ μ „  y = 2 , x = 3
            System.out.println(point.getY()+", "+point.getX());
    
            // sum λ©”μ†Œλ“œ μ‹€ν–‰
            point.sum(point2);
    
            // sum λ©”μ†Œλ“œ μ‹€ν–‰ ν›„ y = 5, x = 7
            System.out.println(point.getY()+", "+point.getX());
    
        }
    }

    μƒμ„±μžλž€?

    • 객체λ₯Ό 생성할 λ•Œ μ‹€ν–‰λ˜λŠ” κ²ƒμœΌλ‘œ, 객체의 λ³€μˆ˜λ“€μ„ μ΄ˆκΈ°ν™”ν•΄μ£Όκ±°λ‚˜ ν•„μš”ν•œ λ³€μˆ˜λ“€μ„ μ„€μ •ν•˜λŠ” λ©”μ†Œλ“œμž…λ‹ˆλ‹€.

    • 클래슀 λ‚΄μ—λŠ” μƒμ„±μžκ°€ μ΅œμ†Œ 1개 이상은 μ‘΄μž¬ν•΄μ•Ό ν•˜κ³ , 리턴 νƒ€μž…μ΄ μ—†μŠ΅λ‹ˆλ‹€.

    • μƒμ„±μžλ₯Ό μ—¬λŸ¬ 개λ₯Ό λ§Œλ“€μ–΄ μ›ν•˜λŠ” λ³€μˆ˜λ“€λ§Œ μ΄ˆκΈ°ν™” ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

    public class Point {
    
        private int y;
        private int x;
    
        // κΈ°λ³Έ μƒμ„±μž
        public Point(){
        }
    
        public Point(int y) {
            this.y = y;
        }
    
        public Point(int y, int x) {
            this.y = y;
            this.x = x;
        }
    
        public void sum(Point p){
            this.y += p.y;
            this.x += p.x;
        }
    
        public void sub(Point p){
            this.y -= p.y;
            this.x -= p.x;
        }
    
        public void mul(Point p){
            this.y *= p.y;
            this.x *= p.x;
        }
    
        public void div(Point p){
            this.y /= p.y;
            this.x /= p.x;
        }
    
        public int getY() {
            return y;
        }
    
        public int getX() {
            return x;
        }
    
        public void setY(int y) {
            this.y = y;
        }
    
        public void setX(int x) {
            this.x = x;
        }
    }

    this ν‚€μ›Œλ“œ

    • 클래슀 λ‚΄λΆ€μ—μ„œ 객체 자기 μžμ‹ μ„ λ‚˜νƒ€λ‚΄λŠ” ν‚€μ›Œλ“œλ‘œ this ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€.

    • 클래슀의 속성과 λ©”μ†Œλ“œμ˜ λ§€κ°œλ³€μˆ˜(parameter)의 이름이 같을 경우 ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€.

    • ν΄λž˜μŠ€μ— μ˜€λ²„λ‘œλ”©λœ λ‹€λ₯Έ μƒμ„±μžλ₯Ό ν˜ΈμΆœν•  λ•Œλ„ μ‚¬μš©ν•©λ‹ˆλ‹€.

    • 클래슀의 μΈμŠ€ν„΄μŠ€λ₯Ό λ°˜ν™˜ν•  λ•Œλ„ μ‚¬μš©ν•©λ‹ˆλ‹€.

    public class Point {
    
        private int y;
        private int x;
    
        // κΈ°λ³Έ μƒμ„±μž
        public Point(){
        }
    
        public Point(int y) {
            this(y,0);
        }
    
        public Point(int y, int x) {
            this.y = y;
            this.x = x;
        }
    
        public Point getInstance(){
            return this;
        }
    
        public void sum(Point p){
            this.y += p.y;
            this.x += p.x;
        }
    
        public void sub(Point p){
            this.y -= p.y;
            this.x -= p.x;
        }
    
        public void mul(Point p){
            this.y *= p.y;
            this.x *= p.x;
        }
    
        public void div(Point p){
            this.y /= p.y;
            this.x /= p.x;
        }
    
        public int getY() {
            return y;
        }
    
        public int getX() {
            return x;
        }
    
        public void setY(int y) {
            this.y = y;
        }
    
        public void setX(int x) {
            this.x = x;
        }
    }
    λ°˜μ‘ν˜•
Designed by Tistory.