您的位置:首页 > 其它

内部类,形式参数和返回值问题研究,API的使用

2017-10-26 16:45 387 查看

一.内部类

在一个类中定义另一个类,那么把这种情况叫:内部类:

       举例:

                在类A中定义一个类B,那么类B就是类A的内部类,同理,类A就是类B的外部类

 

                内部类是直接可以访问外部类的成员,包括私有

                外部类想要访问内部类的成员,必须通过创建内部类的对象访问该内部类的成员

//外部类
class Outer{
       //外部类的成员变量
       privateintnum = 10;
       //内部类
       class Inner{
              //内部类的成员方法
              publicvoid show() {
                     System.out.println(num);
              }
       }
       //外部类的成员方法
       publicvoid method() {
              //创建内部类对象
              Inner
i= new Inner();
              i.show();
       }
}
//测试类
publicclass InnerTest1 {
       publicstaticvoid main(String[]
args) {
              Outer
o = new Outer();
              o.method();
             
             
       }
}

 

  内部类的分类:

                成员内部类:在外部类的成员位置

                局部内部类:在外部类的局部位置定义的这个类

//外部类

class Outer2{

         //外部类的成员变量

         publicint num = 100 ;

        

         //成员内部类

         /*classInner2{

                  

         }*/

        

         //外部类的成员方法

         publicvoid method(){

                   //局部位置

                   classInner2{

                           

                   }

         }

}

 

//测试类

public class InnerDemo2 {

         publicstatic void main(String[] args) {

                  

         }

}

成员内部类:

                在测试类中需要访问成员内部类中的成员的方法:

 

                外部类名.内部类名对象名 =
外部类对象.内部类对象;

//外部类
class Outer3{
       //定义外部类的变量
       publicintnum = 10;
       //成员内部类(非静态)
       class Inner3{
              //成员内部类的成员方法(非静态)
              publicvoid show() {
                     System.out.println(num);
              }
       }
}
//测试类
publicclass InnerTest3 {
       publicstaticvoid main(String[]
args) {
              Outer3.Inner3
oi = new Outer3().new Inner3();
              oi.show();
       }
}

 

  成员内部类的修饰符:

                 private:为了保证数的安全性

                static修饰:为了方便调用

                                  
如果成员内部类被static修饰,那么要访问外部类的成员变量,这个变量必须被static修饰

  

                静态的成员内部类访问该类中的成员方法:

                   格式:外部类名.内部类名对象名
= new 外部类名.内部类名() ;

                   外部类名.内部类名.方法名(静态方法)();

//外部类
class Outer4{
       //外部类的两个成员变量
       publicstaticintnum1=10;
       publicstaticintnum2=20;
       /**
        *
结论:
        *
对于静态的成员内部类来说,无论静态成员内部类中的成员方法是静态还是非静态的,要访问外部类的成员变量,该变量必须被static修饰
        * */
       //定义一个静态的成员内部类
       publicstaticclass Inner4{
              //非静态的内部类的成员方法
              publicvoid show() {
                     System.out.println(num1);
                     System.out.println(num2);
              }
              //静态的内部类的成员方法
              publicstaticvoid show2() {
                     System.out.println(num1);
                     System.out.println(num2);
              }
       }
}
//测试类
publicclass InnerTest4 {
       publicstaticvoid main(String[]
args) {
              //静态的成员内部类访问该类中的成员方法:
                            //格式:外部类名.内部类名对象名
= new 外部类名.内部类名() ;
              Outer4.Inner4
oi = new Outer4.Inner4();
              oi.show();
              oi.show2();//静态方法
              System.out.println("-------------------");
              //show2
的另外一种访问方式
              Outer4.Inner4.show2();
       }
}

局部内部类:

                定义在外部类的局部位置

  

                结论:

                        
无论是局部内部类还是成员内部类(非静态的)都可以直接访问外部类的成员,包括私有

  

                面试题:

                        
局部内部类访问局部变量会出现问题?

                        
当前局部变量报错,必须用final修饰;为什么用final修饰?

                      
是由于局部变量是随着方法调用而生成的,随着方法的调用完毕消失,而现在局部位置有一个局部内部类它要在自己的成员方法位置访问当前的局部变量

           必须把变量变成一个常量,(需要用final:自定义常量),这样的一个变量的值永远是固定的!

class Outer5{
       //外部类的成员变量
       privateintnum =100;
       //外部类的成员方法
       publicvoid method() {
              //局部变量
               finalintnum2= 200;//自定义常量,细节问题
               //局部内部类
               class Inner5{
                            //局部内部类的成员方法
                      publicvoid show() {
                             System.out.println(num);
                            //局部内部类中访问局部变量
                             System.out.println(num2);
                      }
               }
               Inner5
i= new Inner5();
               i.show();
       }
}
//测试类
publicclass InnerTest5 {
       publicstaticvoid main(String[]
args) {
              //对于局部内部类访问具该类的成员方法:创建外部类对象使用外部类对象调用外部类的成员方法
              Outer5
o = new Outer5();
              o.method();
       }
}
匿名内部类

                        
是内部类的简化版格式

前提条件:

                        
必须存在一个接口或者是一个类(可以是具体类,也可以是一个抽象类)

  

书写的格式:

                           new接口名或者类名(){

                                  
方法重写;

                         }

匿名内部类的实质:

                          
继承了该类(抽象类)或者是实现了该接口的子类对象!

//定义一个接口
interface Inter{
       //抽象功能
       publicabstractvoid show();
       publicabstractvoid show2();
}
//外部类
class Outer6{
       //成员方法
       publicvoid method() {
              //使用接口名对象名 =
匿名内部类的格式: new
接口名(){
              //方法重写();
              //}
              Inter
i= new Inter() {
 
                     @Override
                     publicvoid show() {
                            System.out.println("show");
                           
                     }
 
                     @Override
                     publicvoid show2() {
                            System.out.println("show2");
                           
                     }
                    
              };
              //使用对象名调用
              i.show();
              i.show2();
       }
}
//测试类
publicclass InnerTest6 {
       publicstaticvoid main(String[]
args) {
              Outer6
o =new Outer6();
              o.method();
       }
}
 

/**

 * 匿名内部类在开发中的使用

 * */

interfaceInter3{

                          public abstract voidstudy() ;

}

 

classStudentDemo{

                          public voidmethod(Inter3 i){//形式参数是一个接口

                            i.study() ;

                          }

}

 

//方式1:

class Armayimplements Inter3{

                          public void study(){

                            System.out.println("好好学习,天天向上...");

                          }

}

 

//方式2:

 

//测试类

public classOuterTest {

                          public static voidmain(String[] args) {

                            //需求:调用StudentDemo中的method()方法

                            StudentDemo sd = newStudentDemo() ;

                           

                            Inter3 i = newArmay() ;

                            i.study() ;

                           

                            System.out.println("---------------------");

                           

                            //方式2:匿名内部类

                            StudentDemo sd2 =new StudentDemo() ;

                            sd2.method(newInter3(){

 

                                     @Override

                                     public voidstudy() {

                                               System.out.println("好好学习,天天向上...");

                                     }

                                    

                            }) ;

                           

                          }

}

 

/**

 *

 * 看程序

 * 面试题:

                            要求请填空分别输出30,20,10。

                           

                            外部类和内部类没有继承关系!

 * */

class Outer7 {

                          public int num = 10;

                         

                          class Inner7 {

                            public int num = 20;

                            public void show() {

                                     int num =30;

                                     System.out.println(num);

                                     System.out.println(this.num);

                                     System.out.println(newOuter7().num);//要访问外部类的成员变量:匿名对象 :new
外部类名().成员变量

                                     //外部类的this限定

                                     System.out.println(Outer7.this.num);

                                    

                            }

                          }

}

public classInnerClassTest {

                          public static voidmain(String[] args) {

                            Outer7.Inner7 oi =new Outer7().new Inner7();

                            oi.show();

                          }

}

 

/**
 *
匿名内部类面试题:
              按照要求,补齐代码
                     interface
Inter{ void show(); }
                     class Outer { //补齐代码 }
                     class OuterDemo{
                            publicstatic void main(String[]
args) {
                                  
  Outer.method().show();
                              }
                     }
                     要求在控制台输出”HelloWorld”

 * */
// 接口
interface Inter2{
       publicabstractvoid show();
      
}
class
Outer7{
       publicstatic Inter2 method() {
              returnnew Inter2() {
 
                     @Override
                     publicvoid show() {
                            System.out.println("HelloWord");
                           
                     }
                    
              };
       }
}
publicclass InnerTest7 {
       publicstaticvoid main(String[]
args) {
              Outer7.method().show();
               //当前method()能直接被类名调用---->method()方法是一个静态方法   
static
               //Outer8.method().show()---->Outer8.method()返回的是一个对象,拿对象调用show()方法
               //由于接口只有一个show()方法,最终要去重写show()方法
                      }

二. 形式参数和返回值问题研究

形式参数:

                基本数据类型,你要什么数据类型,在实际传参的时候就传什么数据类型;形式参数的改变对实际参数没有影响(String是引用类型和基本数据类型的效果一样String s ="abc")

                如果形式参数是引用类型:

                                                       具体类类:如果形式参数是一个具体类,那么需要创建该类对象

 

                                                     抽象类: 如果形式参数是抽象类的情况,那么需要自定义一个抽象类的子类,来进行实例化(创建对象)!,创建对象的实质:抽象类多态!

 

接口: 如果形式参数是接口情况,那么需要自定义一个接口的子实现类,然后通过接口多态的形式给接口进行实例化!(接口多态!)

 

 返回值:

                                   具体类:直接返回该类对象(通常实际开发中使用的是匿名对象)

                                   抽象类: 返回值如果是抽象类,需要的返回是该抽象类的子类对象

接口: 返回值如果是接口类型,需要的是返回该接口的子实现类对象(通过子实现类进行实例化!)

class Demo{
       publicfloat sum(floata,
floatb) {
              return (a +
b);
       }
}
class Student{
       publicvoid show() {
              System.out.println("好好学习..");
       }
}
class StudentDemo{
       public Student method() {
              returnnew Student();
       }
}
publicclass StudentTest {
       publicstaticvoid main (String[]args)
{
              floata= 10.0F;
              floatb = 12.34F;
              Demo
d = new Demo();
              floatresult =
d.sum(a,
b);
              System.out.println("result"+result);
              System.out.println("--------------------");
              StudentDemo
sd = new StudentDemo();
              Student
s = sd.method();
              s.show();
       }
}

interface
Inter3{
       publicabstractvoid love();
}
class TeacherDemo{
       publicvoid method(Inter3
i) {
              i.love();
       }
}
class Student3
implements
Inter3{
 
       @Override
       publicvoid love() {
              System.out.println("学生爱学习,爱java");
       }
      
}
publicclass TeacherTest {
       publicstaticvoid main(String[]
args) {
              TeacherDemo
td = new TeacherDemo();
              Inter3
i = new Student3();
              td.method(i);
              System.out.println("-----------------");
              Inter3
i2 = new
Inter3() {
 
                     @Override
                     publicvoid love() {
                            System.out.println("学生爱学习,爱java");
                     }
                    
              };
              i2.love();
       }
}



三.如何使用API

打开API:

         显示---->输入你查找的类

         索引---->搜索

        

找到某一个类:

         对该类的描述

         看类结构:

         看该类是否有字段(变量),构造方法(如何创建该类对象),方法(类的成员方法)

         出现该类的版本号:

        

        

Scanner java.util.Scanner;

        

JDK5.0以后的新特性:自动拆装箱(int--->Integer,char--->Character),静态导入(导入的方法级别),可变参数,增强for循环(集合),枚举

JDK7.0也有新特性(匿名内部类:局部内部类访问局部变量特性:局部变量必须被final讲)

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐