JAVA 객체정리

EunBi Na·2023년 5월 15일
0

7. 객체지향

  1. 객체지향의 개요
  2. 클래스와 객체
  3. 상속
class MultiDiv extends PlusMinus {
	int gop;
    double nanum;
    public String multi(int x, int y) {
    	gop = x * y
        return "두 수의 곱은" + gop;
    }
    public String div(int x, int y) {
    	nanum = (double) x / y;
        return "두 수의 나눈 값은 " + nanum;
    }
}
public class FourRulesTest1 {
	public static void main(String args[]) {
    String splus, sminus, smulti, sdiv;
    MultiDiv ob1 = new MultiDiv();
    splus = ob1.plus(50, 30);
    sminus = ob1.minus(50, 30);
    smulti = ob1.multi(50, 30);
    sdiv = ob1.div(50, 30);
    System.out.println(sminus);
    System.out.println(sminus);
    System.out.println(smulti);
    System.out.println(sdiv);
    }
}
class Parent {
	protected int a = 1000;
    public int fun1() {
    	return a;
    }
}
class Child extends Parent {
	private int b = 5;
    public int fun2(){
    	return a/b;
    }
}
class Example{
	public static void main(String[] args){
    	Child c1 = new Child();
        Parent p1 = new Child();
        	System.out.println( c1.fun1() );
            System.out.println( c1.fun2() );
            System.out.println( p1.fun1() );
            System.out.println( p1.fun2() );
            
  1. 클래스
  2. 메시지
  3. 추상화
  4. 다형성

8. 클래스 : 속성

1. 클래스의 일반 구조
2. 클래스 선언

1) 멤버변수 + 생성자(생성자 메소드) + 메소드
2) 멤버변수, 생성자 메소드로 구성된 클래스

public class Box { #속성-멤버 변수
	int width;
    int height;
    int depth;
    
    public Box(int w, int h, int d) #기능-생성자 메소드
    {
    	width = w;
        height = h;
        depth = d;
    }
    
    public void volume() { #기능-메소드
    	int vol;
        vol = width + height + depth;
        System.out.println("Volume is" + vol);
      }
  }
  
  public class Box {  #속성-멤버 변수
  	int width;
    int height;
    int depth;
    public void volume() { #기능-메소드
    	int vol;
        vol = width * height * depth;
        System.out.println("Volume is" + vol);
      }
 }

3. 객체의 선언과 생성

[ public / final / abstract ] class Class-name {}

  • public : 모든 클래스에서 접근 가능
  • final : 서브 클래스를 가질 수 없는 클래스
  • abstract : 객체를 생성할 수 없는 클래스
  • 객체의 생성
    클래스명 객체변수명 = new 클래스명();
class Box1 { #3개의 속성을 가진 클래스 Box1을 선언
	int width;
    int height;
    int depth;
}
public class Box1Test1 {
	public static void main(String args[]) {
    	Box1 mybox1 = new Box1(); #클래스 Box1으로부터 두개의 객체 생성
        Box1 mybox2 = new Box1();
        int vol1, vol2;

4. 멤버변수와 메소드 변수

멤버변수 : 객체변수, 클래스변수, 종단변수
생성자 또는 메소드 : 매개변수, 지역변수

  • 메소드 : 함수와 대응되는 개념으로,
    객체의 상태 및 속성 변경과 같이 객체에 대해 수행할 수 있는 작업
class Initial {
	int number;
    double rate;
    int[] score;
    public void aMethod() { #메소드 선언
    	int count;
        System.out.println(number);

*final : 종단변수, 변할 수 없는 상숫값을 가짐

static final int SONATA_LENGTH = 3200; 

5. 변수의 유효 범위
변수의 유효범위 : 그 변수가 사용될 수 있는 영역을 의미

6. 멤버 변수 접근 한정자

  • public : 항상 접근 가능
  • private : 소속된 클래스 내에서만 사용가능

9. 클래스 : 기능

  1. 생성자
  2. 생성자 오버로딩
  3. 예약자 this
  4. 메소드
  5. 메소드 오버로딩
  6. 메소드에 값 전달 기법

10. 상속

  1. 상속의 개요
  2. 상속과 한정자
  3. 상속과 생성자
class DA1 {
  1. 상속과 메소드 오버라이딩
  2. 예약자 super
  3. Object 클래스
  4. 예약자 final
  5. 상속과 포함

11. 다형성과 추상클래스, 인터페이스

  1. 객체의 형 변환
  2. 연산자 instanceof
  3. 다형성
  4. 추상 클래스
  5. 추상 클래스와 다형성
  6. 인터페이스
  7. 인터페이스와 다형성
profile
This is a velog that freely records the process I learn.

0개의 댓글