superキーワードの詳細

8956 ワード

/* super   : super             。 super       : 1.             ,              ,     super            。 2.        ,               ,    super               。 super                 : 1.                         ,  java                 super()  。 2. super             ,                     。 3. super this                            。              。 super    this      : 1.         。 1. super              。 2. this                 。 2.        。 1. super               。 2. this                。 3.          : 1. super             。 2. this             。 */
class Fu{

    int x = 10;

    String name;


    public Fu(){
        System.out.println("Fu        ..");
    }

    public Fu(String name){
        this.name = name;
        System.out.println("Fu        ..");
    }


    public void eat(){
        System.out.println("       ..");
    }
}


class Zi extends Fu{

    int x = 20; 

    int num;

    public Zi(String name,int num){
        super(name); //               ...
        this(); //           ..
        //super(); //             。。。
        System.out.println("Zi        ..");
    }

    public Zi(){
        System.out.println("Zi        ..");
    }


    public void print(){
        System.out.println("x = " +super.x);
    }

    public void eat(){
        System.out.println("       ..");
    }
}

class Demo9 {

    public static void main(String[] args) 
    {
        Zi z = new Zi("  ");


    }
}
/*      :              。        :           。      :           ,             。              :               。           : 1.     ,             。 2.     ,                         。 3.     ,                          。 4.     ,                          。 Exception(  ) RuntimeException(  )      :                      ,       。         1.       。 2.        (               ) 3.         。 */
class Animal{  //        
}

class Fish extends Animal{  //Fish       。
}


class Fu{

    String name;

    public Fu(String name){
        this.name = name;
    }

    public Animal eat() throws RuntimeException {
        System.out.println(name+"   ...");
        return new Animal();
    }
}


class Zi extends Fu{

    String num;

    public Zi(String name){
        super(name);//              
    }


    //     eat  
    public Animal eat() throws Exception{
        System.out.println("     ..");
        System.out.println("   ....");
        System.out.println("    ....");
        System.out.println("   ....");
        System.out.println("   ....");
        System.out.println("    ...."); 
        return new Animal();
    }

}

class Demo10{

    public static void main(String[] args) 
    {
        Zi z = new Zi("    ");
        z.eat();

    }
}
/*   :  java         、 java      、       。          。          。       :        。       :     javase。      : javaee+android. */

//      
class Student{

    String name;

    //    
    public Student(String name){
        this.name = name;
    }

    public void study(){
        System.out.println(name+"         ");
    }
}

//              
class BaseStudent extends Student{


    public BaseStudent(String name){
        super(name);//          
    }

    //  
    public void study(){
        System.out.println(name+"  javase..");
    }
}


//                 
class WorkStudent extends Student{

    //     
    public WorkStudent(String name){
        super(name);
    }
        //  
    public void study(){
        System.out.println(name+"  javaee+android..");
    }
}


class Demo11 {
    public static void main(String[] args) 
    {
        //System.out.println("Hello World!");

        BaseStudent s = new BaseStudent("   ");
        s.study();

        //          
        WorkStudent w = new WorkStudent("  ");
        w.study();
    }
}