JAva練習-継承とマルチステート05

39182 ワード

目次
  • 継承の概念
  • 2 thisとsuperの違いと応用
  • 3継承中の面接問題1
  • 4継承中の面接問題2
  • 5メソッド書き換え
  • finalキーワード導入
  • 7面試験問題:final修飾局所変数の問題
  • 8 final修飾変数の初期化タイミング
  • 9多態の概念
  • 10多態の練習問題-プログラムを見て結果を書く
  • 1継承の概念
        :
    	                      。
    	
           ?	
    	Java      :extends
    	
      :
    	class     extends     {}
    	
      :
    	A:         
    	B:         
    	C:           ,      
    
            ,           :
    	        。
    	
    	     :   ,   。
    	  :      
    	  :             
           :
    	A:                (         )
    	B:             ,      super(   )            。
    	C:            
      ,             ?
    		            :"is a"。
    			Person
    			Student
    			Teacher
    		  
    			  
    			  
    			  
    	     。
    		      A,B。      A B   ,  B A   ,         。
        :
    	    :
    	    :
    	    :
               ,  ,          ,           。
    
              :
    	A:                      ,     。
    	B:                     ,      ?
    		                 :
    			a:           ,    
    			b:         ,    
    			c:         ,    
    			d:      ,   。
              :
    	A:                  ,     。
    	B:                 ,       ?
    		          :
    			a:     ,        ,    
    			b:     ,       ,    
    			c:       。
    

    例:
    package cn.plsite.day04.demo04;
    
    public class FatherClass {
        int age;
    
        public FatherClass(int age){
            System.out.println("Father      ");
        }
        public FatherClass(){
            System.out.println("Father      ");
        }
    }
    
    package cn.plsite.day04.demo04;
    
    public class SonClass extends FatherClass{
        int age;
        public SonClass(){
            System.out.println("Son     ");
        }
        public SonClass(int age){
            System.out.println("Son     ");
        }
    
    
    }
    
    package cn.plsite.day04.demo04;
    
    public class FatherAndSon {
        public static void main(String[] args) {
            SonClass son = new SonClass();
            SonClass son1 = new SonClass(14);
        }
    }
    

    実行結果:
    Father      
    Son     
    Father      
    Son     
    

    2 thisとsuperの違いと応用
    this super   ?
    	      ?
    		this         。
    		super           (         ,         )
    
    	    ?
    		A:      
    			this.               (      )
    			super.               (      )
    		B:      
    			this(...)	         
    			super(...)	         
    		C:      
    			this.               
    			super.               
    
    
                ,                 ?
    	  。
         ?	
    	A:             
    	B:    super                  
    	C:    this            
    		                    ,            。
    		
        :
    	this(...)  super(...)               。
    	            ,                 ,            。
    

    3継承中の面接問題1
          :
    	A:         ,     ,         
    		     >     >    
    	B:               
    		             
    	C:                 
    

    プログラム:
    class Fu {
    	static {
    		System.out.println("     Fu");
    	}
    
    	{
    		System.out.println("     Fu");
    	}
    
    	public Fu() {
    		System.out.println("    Fu");
    	}
    }
    
    class Zi extends Fu {
    	static {
    		System.out.println("     Zi");
    	}
    
    	{
    		System.out.println("     Zi");
    	}
    
    	public Zi() {
    		System.out.println("    Zi");
    	}
    }
    
    class ExtendsTest2 {
    	public static void main(String[] args) {
    		Zi z = new Zi();
    	}
    }
    

    結果は次のとおりです.
         Fu
    	     Zi
    	     Fu
    	    Fu
    	     Zi
    	    Zi
    

    4継承中の面接問題2
          :
    	A:       
    		int x = 10; //         
    		Student s = new Student(); //         
    	B:         
    		        
    			     
    			     
    			       
    	C:       (     )
    		        ,         。
    		
    	
      :
    	              super()
    	      ,           。
    	            。
    	              ,        。
    

    タイトル:
    class X {
    	Y b = new Y();
    	X() {
    		System.out.print("X");
    	}
    }
    
    class Y {
    	Y() {
    		System.out.print("Y");
    	}
    }
    
    public class Z extends X {
    	Y y = new Y();
    	Z() {
    		//super
    		System.out.print("Z");
    	}
    	public static void main(String[] args) {
    		new Z(); 
    	}
    }
    

    答え:
    YXYZ
    

    5メソッド書き換え
        :
                         。
    
        :
    	           ,         。      。
    
               :
    	      ,    。
    	
           :
    	          ,               ,          。
    	  ,         ,           。
             
    	A:            
    		                 
    	B:         ,        
    		     
    	C:      ,               ,        @override,       		
    	           ,        。
    	
    1.            ?             ?
        :    ,                  。		
        :     ,        ,         。            ,           。
    Override:    
    Overload:    
    
    2:this    super         ?              。
    
    this:          
    super:           。(          ,               )
    
      :
    	    :
    		this.    
    		super.    
    	    :
    		this(...)
    		super(...)
    	    :
    		this.    
    		super.    
    

    6 finalキーワード導入
           
                :    。
      ,     ,         。
        ,                ,      。
        ,      ,Java         :final
    
    final:     。          ,  ,  。
    
    class Fu {
    	public final void show() {
    		System.out.println("       ,        ");
    	}
    }
    
    class Zi extends Fu {
    	// Zi  show()    Fu  show()
    	//public void show() {
    	//	System.out.println("      ");
    	//}
    }
    
    class ZiDemo {
    	public static void main(String[] args) {
    		Zi z = new Zi();
    		z.show();
    	}
    }
    
    final     ,  ,  
    
      :
    	final     ,       。
    	final      ,        。(  ,  )
    	final      ,          。          。
    	
      :
    	A:     
    		"hello",10,true
    	B:     
    		final int x = 10;
    

    7面試験問題:final修飾局所変数の問題
    	    :            。
    	    :              ,  ,               。
    

    class Student {
    	int age = 10;
    }
    
    class FinalTest {
    	public static void main(String[] args) {
    		//           
    		int x = 10;
    		x = 100;
    		System.out.println(x);
    		final int y = 10;
    		//       y   
    		//y = 100;
    		System.out.println(y);
    		System.out.println("--------------");
    		
    		//           
    		Student s = new Student();
    		System.out.println(s.age);
    		s.age = 100;
    		System.out.println(s.age);
    		System.out.println("--------------");
    		
    		final Student ss = new Student();
    		System.out.println(ss.age);
    		ss.age = 100;
    		System.out.println(ss.age);
    		
    		//        
    		//       ss   
    		ss = new Student();
    	}
    }
    

    結果:
    100
    10
    --------------
    10
    100
    --------------
    10
    100
    

    8 final修飾変数の初期化タイミング
    	A: final           。
    	B:        。(      )
    
    class Demo {
    	//int num = 10;
    	//final int num2 = 20;
    	
    	int num;
    	final int num2;
    	
    	{
    		//num2 = 10;//      
    	}
    	
    	public Demo() {
    		num = 100;
    		//       num2   
    		//num2 = 200;//      
    	}
    }
    
    class FinalTest2 {
    	public static void main(String[] args) {
    		Demo d = new Demo();
    		System.out.println(d.num);
    		System.out.println(d.num2);
    	}
    }
    

    9多態の概念
      :     (  ),              。
      :
    	   ,    。
    	 (  ,  ,  )。
    	
         :
    	A:      。
    	B:      。
    		         ,             。
    			   d = new  ();
    			d.show();
    			   d = new  ();
    			d.show();
    	C:            。
    		  f =  new  ();
    		
             。
    
              :
    	A:    
    		     ,     。
    	B:    
    		         ,         ,           。
    	C:    
    		     ,     。
    	D:    
    		     ,     。
    		(      ,     ,  ,       )
    		
    	            ,        。
    	     :
    		a:     
    			class Fu {}
    			class Zi extends Fu {}
    			
    			Fu f = new Zi();
    		b:     
    			abstract class Fu {}
    			class Zi extends Fu {}
    			
    			Fu f = new Zi();
    		c:    
    			interface Fu {}
    			class Zi implements Fu {}
    			
    			Fu f = new Zi();
    
    class Fu {
    	public int num = 100;
    
    	public void show() {
    		System.out.println("show Fu");
    	}
    	
    	public static void function() {
    		System.out.println("function Fu");
    	}
    }
    
    class Zi extends Fu {
    	public int num = 1000;
    	public int num2 = 200;
    //    
    	public void show() {
    		System.out.println("show Zi");
    	}
    	
    	public void method() {
    		System.out.println("method zi");
    	}
    	
    	public static void function() {
    		System.out.println("function Zi");
    	}
    }
    
    class DuoTaiDemo {
    	public static void main(String[] args) {
    		//            。
    		//  f =  new  ();
    		Fu f = new Zi();
    		System.out.println(f.num);//100
    		//     
    		//System.out.println(f.num2);
    		
    		f.show();
    		//     
    		//f.method();
    		f.function();
    	}
    }
    
         :
    	A:         (    )
    	B:         (     )
         :
    	           。
            :
    		    :
    			Fu f = new Zi();
    		    :
    			Zi z = (Zi)f; //   f        Zi 。
    

    10多態の練習問題-プログラムを見て結果を書く
          :        ,    ,    
    
             :
    	  :     ,     。
    	
         :
    	             ,   。
    	              ,         。
    
    class A {
    	public void show() {
    		show2();
    	}
    	public void show2() {
    		System.out.println(" ");
    	}
    }
    class B extends A {
    	/*
    	public void show() {
    		show2();
    	}
    	*/
    
    	public void show2() {
    		System.out.println(" ");
    	}
    }
    class C extends B {
    	public void show() {
    		super.show();
    	}
    	public void show2() {
    		System.out.println(" ");
    	}
    }
    public class DuoTaiTest4 {
    	public static void main(String[] args) {
    		A a = new B();
    		a.show();
    		
    		B b = new C();
    		b.show();
    	}
    }
    

    実行結果: