Java SE 학습 노트 03 코드

5217 단어 자바Access
부모 클래스 와 하위 클래스 의 속성 과 방법 호출 에 관 한 코드
 
/**
 *     
 */
package org.sean.module03;

/**
 *          ,             
 * 
 *              ,            ,              
 * 
 *   ,         ,             ,         ,        
 * 
 *     :              ,      ,                 (             )
 * 
 *                 ,                  
 * 
 *     (  ):              ,      ,               
 * 
 *            ,                。
 * 
 *   :            ,         (  )
 * 
 * @author      (Sean Yang)
 */
class Parent2 {

	//    ,Parent   
	public static String kind = "org.sean.module03.CoverAndHide.Parent";
	//    ,Parent   
	public static int age = 50;
	//     ,Parent   
	public String name = "Parent";

	/**     ,  Parent    */
	public static String getKind() {
		System.out.println("parent getKind() method called");
		return kind;
	}

	/**     ,  Parent    */
	public static int getAge() {
		System.out.println("parent getAge() method called");
		return age;
	}

	/**     ,  Parent    */
	public String getName() {
		System.out.println("parent getName() method called");
		return this.name;
	}
}

class Child2 extends Parent2 {

	//    ,Child   
	public static String kind = "org.sean.module03.CoverAndHide.Child";
	//    ,Child   
	public static int age = 25;
	//     ,Child   
	public String name = "child";

	/**     ,  Child    */
	public static String getKind() {
		System.out.println("child getKind() method called");
		return kind;
	}

	/**     ,  Child    */
	public static int getAge() {
		System.out.println("child getAge() method called");
		return age;
	}

	/**     ,  Child    */
	public String getName() {
		System.out.println("child getName() method called");
		return this.name;
	}

	/**     ,  Parent    */
	public String getParentName() {
		return super.name;
	}

	/**     ,  Parent    */
	public static String getParentKind() {
		return Parent2.kind;
	}
}

public class CoverAndHide {

	@SuppressWarnings("static-access")
	public static void main(String[] args) {
		Child2 child = new Child2();
		System.out.println("child name is " + child.name + ", child age is "
				+ child.age + " ,kind is " + child.kind);//       Child     
		/**  Child     Parent  ,      */
		// Parent2 parent = child;
		Parent2 parent = new Child2();
		System.out.println("   Parent   name is " + parent.name + ", age is "
				+ parent.age + " ,kind is " + parent.kind);
		/**
		 *   :
		 * 
		 *                       (  ),             ,                 
		 * 
		 *                      ,    "super.   "
		 * 
		 *                     ,    "   .   "
		 */
		System.out.println("              :" + child.getParentName());
		System.out.println("             :" + child.getParentKind());

		/**               */
		child.getName();
		child.getKind();
		parent.getName();
		parent.getKind();
		/**
		 *   :
		 * 
		 *                    ,                  (  )
		 * 
		 *            (    ),           ,            
		 */
	}
}

 
 
   부모 클래스 와 하위 클래스 로 딩 순서 에 대한 코드
 
/**
 *       
 */
package org.sean.module03;

/**
 *         --->        --->        --->        --->       --->       
 * 
 *        ,             
 * 
 * @author      (Sean Yang)
 */
class Parent1 {

	@SuppressWarnings("unused")
	private int x = 50;

	static {
		System.out.println("parent static block");
	}

	@SuppressWarnings("unused")
	private static int sx = getNext(99);

	{
		System.out.println("parant init block");
	}

	public Parent1() {
		System.out.println("parent constructor");
	}

	public static int getNext(int base) {
		System.out.println("static parameter initialized");
		return ++base;
	}
}

class Child1 extends Parent1 {

	static {
		System.out.println("child static block");
	}

	{
		System.out.println("child init block");
	}

	public Child1() {
		System.out.println("child constructor");
	}
}

public class LoadingOrder {

	public static void main(String[] args) {
		@SuppressWarnings("unused")
		Child1 child = new Child1();
	}
}

좋은 웹페이지 즐겨찾기