대상 지향 개념 & 자바 Tips

클래스: 클래스 대상 의 템 플 릿
대상: 클래스 의 구체 적 인 실례
대상 을 대상 으로 하 는 세 가지 기본 적 인 특징 은 포장, 계승, 다 형 이다.
계승:
http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.
자 바 는 단독 상속 이다.Object 클래스 를 제외 하고 각 클래스
있 고 단지 하나의 기류 만 있다.기본 클래스 를 명확 하 게 (explicit) 지정 하지 않 았 습 니 다. 기본 클래스 는 암시 적 으로 (implicitly) Object 클래스 로 지정 되 었 습 니 다.
그래서 만약 에 Father implicially extends Object, Son extends Father 가 있다 면 우 리 는 Object 가 Son 의 기류 라 고 말 할 수 없다.그러나 상속 은 오 브 젝 트 가 뿌리 인 나무 모양 의 층계 관계 이기 때문에 손 류 는 오 브 젝 트 류 의 members 를 '간접 상속' 했다.
다 중:
정의.
인용 하 다.
다 중 (동적 바 인 딩 / 실행 시 바 인 딩 / 후기) 은 '실행 기간 (컴 파일 기간 이 아 닌) 에 인용 대상 의 실제 유형 을 판단 하고 실제 유형 에 따라 해당 하 는 것 을 호출 합 니 다.
방법
다 중 존재 하 는 세 가지 필요 조건:
인용 하 다.
1. 상속 이 있어 야 한다
2. 다시 쓰 기 를 해 야 한다.
3. 부모 클래스 참조 지향 하위 클래스 대상
다 중 정의 에 주의 하 십시오.
방법클래스 의 방법 만 다 형 이 있 고 클래스 의 구성원 변 수 는 다 형 이 존재 하지 않 습 니 다!아래 의 예 와 같이:

public class AttributeNoPolymorphism {
	public static void main(String[] args) {
		Animal a = new Cat();
		
		//               
		System.out.println(a.i); //10
		System.out.println(a.si);//100 (Dont access the static fields in this way)
		
		//               get  (        getI()  )
		System.out.println(a.getI()); //20,   sysout          
		
		//getSi()      (static        !),       
		System.out.println(a.getSi()); //100 (Dont access the static methods in this way)
	}
}

class Animal {
	int i =10;
	static int si = 100;
	
	public int getI() {
		return i;
	}

	public static int getSi() {
		return si;
	}
}

class Cat extends Animal {
	int i = 20;
	static int si = 200;
	
	public int getI() {
		return i;
	}
	
	public static int getSi() {
		return si;
	}
}

재 작성 과 재 업로드
오 버 라 이 드 를 다시 쓰 는 것 은 부모 클래스 와 하위 클래스 간 의 다 형 적 표현 이 고, 오 버 라 이 드 를 다시 쓰 는 것 은 클래스 의 다 형 적 표현 이다.
http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.8.3
http://docs.oracle.com/javase/tutorial/java/IandI/override.html
다시 쓰기 override 에 대하 여:
하위 클래스 에 서 는 필요 에 따라 기본 클래스 에서 물 려 받 은 방법 을 다시 쓸 수 있다.
1. 하위 클래스 에서 재 작성 방법 은 부모 클래스 에서 재 작성 되 는 방법 과 같은 방법 으로 서명 해 야 합 니 다 (같은 방법 이름, 매개 변수 목록 (매개 변수 수량 이 일치 하고 위치 매개 변수 유형 이 똑 같 습 니 다).
2. 하위 클래스 에서 재 작성 방법의 반환 유형 은 부모 클래스 가 재 작성 되 는 방법의 반환 유형 이나 하위 유형 이 어야 합 니 다.
3 The overriding method must NOT throw checked exceptions that are new or broader than those declared by the overridden method. For example, a method that declares a FileNotFoundException cannot be overridden by a method that declares a SQLException, Exception, or any other non-runtime exception unless it's a subclass of FileNotFoundException.
4. 재 작성 방법 은 재 작성 방법 보다 더 엄격 한 접근 수식 자 를 사용 할 수 없습니다.private 방법 은 다시 쓸 수 없습니다.
인용 하 다.
http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.8.3
Note that a private method cannot be hidden or overridden in the technical sense of those terms. This means that a subclass can declare a method with the same signature as a private method in one of its superclasses, and there is no requirement that the return type or throws clause of such a method bear any relationship to those of the private method in the superclass.
5 구조 방법 은 계승 할 수 없 기 때문에 다시 쓸 수 없다.
6. 하위 클래스 는 정적 방법 으로 부모 클래스 의 비 정적 방법 을 다시 쓸 수 없습니다.
the static method cannot hide instance method from ParentClassXXX”
7. 하위 클래스 는 비 정적 방법 으로 부모 클래스 의 정적 방법 을 다시 쓸 수 없습니다. (또는 이렇게 표현 합 니 다. 하위 클래스 는 이러한 비 정적 방법 을 정의 할 수 없습니다. 1. 부모 클래스 의 방법 이름과 같 고 매개 변수 가 같 습 니 다. 2. 부모 클래스 의 이 방법 은 정적 입 니 다) 그렇지 않 으 면 보고 합 니 다. "
This instance method cannot override the static method from First     ParentClassXXX”
8. 부모 클래스 와 하위 클래스 에서 방법 명, 매개 변수, 반환 값 이 모두 같은 정적 방법 을 정의 할 수 있 지만 엄 밀 히 말 하면 재 작성 이 아 닙 니 다!재 작성 은 부모 대상 과 하위 대상 간 의 다 형 성 을 나타 내기 때문이다.이른바 '정적 재 작성 방법' 은 다 태 성 을 나타 내지 않 는 다. 정적 방법 은 클래스 이기 때문이다.부모 클래스 대상 의 정적 방법 을 호출 합 니 다 (
물론 클래스 인 스 턴 스 를 통 해 클래스 를 조정 하 는 정적 방법 을 추천 하지 않 습 니 다). 부모 클래스 에서 실 행 된 것 입 니 다.하위 클래스 대상 의 정적 방법 을 호출 합 니 다. 실행 하 는 것 은 하위 클래스 의 것 입 니 다!
Why static method can't be overridden in Java ?
http://en.allexperts.com/q/Java-1046/Static-method-override.htm
인용 하 다.
Static means one per class, not one for each object no matter how many instance of a class might exist.
This means that you can use them without creating an instance of a class.Static methods are implicitly final, because overriding is done based on the type of the object, and static methods are attached to a class, not an object.
A static method in a superclass can be shadowed by another static method in a subclass, as long as the original method was not declared final. However, you can't override a static method with a nonstatic method. In other words, you can't change a static method into an instance method in a subclass.

import java.util.AbstractMap;
import java.util.HashMap;

public class OverrideTest {
	public static void main(String[] args) {
		Son son = new Son(); 
        Father father = son;
        
        son.aMethod(null);
        father.aMethod(null);
	}
}

class Father {
	protected AbstractMap aMethod(AbstractMap param) {
		System.out.println("Father");
		return null;
	}  
}  

class Son extends Father {
	/***
	 *      :                  。      protected public,    private       。
	 *     :                               。              AbstractMap    HashMap
	 *    :        
	 *     :      :      ,            !            AbstractMap  ,            AbstractMap  !      ( Object)      ( HashMap)
	 * 
	 */
	@Override
    public HashMap aMethod(AbstractMap param) {
        System.out.println("Son");
        return null;
    }    
}  

과부하 에 대하 여:
http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
http://www.java-samples.com/showtutorial.php?tutorialid=284
방법의 과부하 란 같은 이름 을 정의 할 수 있 지만 매개 변수 가 다른 여러 가지 방법 을 말한다.
방법의 이름과 매개 변수 유형 구성 방법의 서명 입 니 다. 예 를 들 어 calculate Answer 방법의 서명 입 니 다.
      calculateAnswer(double, int, double, double)
자바 는 방법의 서명 을 통 해 방법 을 구분 하 는데 이것 이 바로 과부하 의 이론 적 기초 이다.여기 서 주의해 야 할 것 은 방법 서명 은 반환 값 형식 을 포함 하지 않 기 때문에 코드 에 방법 서명 을 설명 할 수 없습니다. 반환 값 이 다른 두 가지 방법 일 뿐 입 니 다. 그렇지 않 으 면 컴 파일 오류 가 발생 했 습 니 다. JVM 은 무엇 을 조정 해 야 할 지 모 르 기 때 문 입 니 다.물론 반환 값 유형 은 방법 으로 다시 불 러 오 는 평가 기준 으로 사용 할 수 없 을 뿐 다시 불 러 오 는 방법 이 똑 같은 반환 값 유형 을 가 져 야 하 는 것 은 아 닙 니 다!아래 것 도 무 거 운 짐 입 니 다.
4. 567913. 또한 주의해 야 할 것 은 아버지 와 같은 방법 도 다시 실 을 수 있다 는 것 이다.예:

	public String m(String s) {
		return s;
	}
	public double m(int i) {
		return i;
	}

인터페이스 에 있 는 구성원 변 수 는 기본적으로 Public static final 입 니 다.
이 세 개의 장식 부 호 를 넣 지 않 아 도;기본 값 은 final 이기 때문에 명시 적 으로 할당 해 야 합 니 다 (
http://wuaner.iteye.com/admin/blogs/994819 )。
인터페이스 와 추상 류 는 모두 예화 할 수 없다.
만약 한 종류 에 적어도 하나의 추상 적 인 방법 이 포함 된다 면, 이 종 류 는 반드시 추상 류 로 성명 해 야 한다.
반대로 추상 류 는 반드시 추상 적 인 방법 을 포함 하 는 것 이 아니 라 모두 구체 적 인 방법 일 수 있다.

좋은 웹페이지 즐겨찾기