java--内部类(局部)



package com.qianfeng.demo5;

 

public class Mother{

       //sex= 1  男孩

       //sex= 0 女孩

       publicPerson birth(int sex){

              if(sex == 1){

                     returnnew Boy("");

              }else{

                     returnnew Girl("");

              }

       }

       publicPerson birth2(int sex){

              class Boy extends Person{

                     publicBoy(String sex) {

                            super(sex);

                     }

              }

             

              class Girl extends Person{

                     publicGirl(String sex) {

                            super(sex);

                     }

              }

              if(sex == 1){

                     returnnew Boy("");

              }else{

                     returnnew Girl("");

              }

       }

      

       publicPerson birth3(int sex){

              if(sex == 1){

                     returnnew Person(){

                            int age = 0;

                            String sex = "";

                     };

              }else{

                     returnnew Girl("");

              }

       }

}

 

 

package com.qianfeng.demo5;

 

public class Person {

       publicString sex;

       publicint age;

      

       publicPerson(String sex){

              this.sex = sex;

       }

      

       publicPerson(){

       }

}

 

 

 

 

package com.qianfeng.demo5;

 

public class Girl extends Person {

 

       publicGirl(String sex) {

              super(sex);

              // TODO Auto-generated constructor stub

       }

 

}

 

 

package com.qianfeng.demo5;

 

public class Boy extends Person {

 

       publicBoy(String sex) {

              super(sex);

              // TODO Auto-generated constructor stub

       }

 

}

 

 

 

 

 

package com.qianfeng.demo4;

 

public class Outer {

       publicint a = 1;

       publicstatic int b = 2;

       publicvoid method(){

              final int abb = 22;

              class Inner{

                     publicint a = 33;

                     //单单使用static修饰的变量不能再局部内部类中定义

                     //配合final使用则表示常量

                     //publicstatic  int b = 22;

                     publicvoid method(){

                            System.out.println(a);

                            //Local variable abb defined in anenclosing

                            //scope must be final or effectivelyfinal

                            //a = abb;

                            //abb++;

                            System.out.println(abb);

                     }

                     //Themethod method2 cannot be declared static;

                     //staticmethods can only be declared in a static or top level type

                     /*publicstatic void method2(){

                     }*/

              }

              //abb++;

              System.out.println(abb);

              Inner inner = new Inner();

              inner.method();

       }

      

      

      

       publicstatic void main(String[] args) {

             

              Outer outer = new Outer();

              outer.method();

       }

      

      

}

 

你可能感兴趣的:(java--内部类)