zoukankan      html  css  js  c++  java
  • Java 随心笔记8

    一、包
    在DOS窗口自动打包
    例:javac -d . Demo.java
    .代表当前位置
    -d代表打包

    如果不同的类在不同的包下面,需要通过import进行导包,
    例:import com.baidu.Student;
    com baidu代表文件夹,而Student则为需要调用的.class文件的名称。

    二、局部类
    JDK1.8之后 方法的局部变量 在局部内部类中的成员方法调用时,可以不加final.

     1 class Demo 
     2     {
     3         public static void main(String[] args) 
     4         {
     5             Outer.Inner oi = new Outer().new Inner();
     6             oi.show();//输出成员内部类的成员方法中的值;
     7         }
     8     }
     9     class Outer
    10     {
    11         public int num = 10;
    12         class Inner
    13         {
    14             public int num = 20;
    15             public void show(){
    16                 int num =30;
    17                 System.out.println(num);//调用成员内部类的局部变量
    18                 System.out.println(this.num);//调用成员方法的成员方法
    19                 System.out.println(Outer.this.num);//调用外部类的成员变量
    20             }
    21         }
    22     }
    成员内部类中调用成员变量和局部变量:

    三、内部类:
    1.定义:写在其他类的内部的类
    2.按位置分为:
    a.成员内部类
    定义:定义在外部类中的成员位置,public,private
    访问:外部类名.内部类名 变量名 = new 外部类名().new 内部类名();

     1 class Student{
     2                 public static void main(String[] args){
     3                     Body.Heart BH = new Body().new Heart();
     4                     BH.jump();        
     5                 }    
     6             }
     7             class Body{
     8                 private boolean life =true;
     9                 public class Heart{
    10                     public void jump(){
    11                         System.out.println("心脏噗的跳");            
    12                     }    
    13                 }    
    14             }
    成员内部类

    b.局部内部类
    定义:定义在外部类方法中的局部位置
    访问:在外部类方法中,创建内部类对象,进行访问

     1 public class test {
     2                 public static void main(String[] args) {
     3                     Party p = new Party();
     4                     p.puffBall();
     5                 }
     6             }
     7             class Party{
     8                 public void puffBall(){
     9                     class Ball{
    10                         public void puff(){
    11                             System.out.println("气123球膨胀了");
    12                         }            
    13                     }
    14                     new Ball().puff();    
    15                 }    
    16             }
    局部内部类

    c.匿名内部类(也是一种局部内部类)
    作用:匿名内部类是创建某个类型子类对象的快捷方式
    格式:new 父类或接口(){
    //进行方法重写
    };
    传参:多个方法重写时,不建议使用

    四、总结回顾
    1.补充程序,如下

     1 class  Test
     2         {
     3             public static void main(String[] args) 
     4             {
     5                 Outer.method().show();//Outer.method()是匿名对象
     6             }
     7         }
     8         interface Inter{
     9             void show();
    10         }
    11         class Outer{
    12             //以下为补充程序
    13             public static Inter method(){                
    14                 return new Inter(){
    15                     public void show(){
    16                         System.out.print("2018");
    17                     }    
    18                 };        
    19             }
    20         }

    Outer.method().show();//链式调用 Outer是父类,method()是方法,当返回值为静态对象时能够调用show方法

    同样的梦想,不一样的起点,大家共同进步, 微信公众号「孝客之乡」作者。
  • 相关阅读:
    libusb简介
    STM8S和STM8L调试串口中断的注意点
    QT 5.1.1 for Android 开发环境搭建与配置【Windows 7】
    【Luogu3806】点分治(点分治)
    NOIP2017+停课总结
    【BZOJ2301】【HAOI2011】Problem B(莫比乌斯反演)
    【Luogu3455】【POI2007】ZAP-Queries(莫比乌斯反演)
    【HDU1695】GCD(莫比乌斯反演)
    【BZOJ2816】【ZJOI2012】网络(Link-Cut Tree)
    莫比乌斯反演
  • 原文地址:https://www.cnblogs.com/JulyTail/p/9069635.html
Copyright © 2011-2022 走看看