zoukankan      html  css  js  c++  java
  • Java学习第十一章 之 final、static、内部类

      1 /*
      2 
      3 final修饰的类,变量,方法有什么特点
      4 
      5        被final修饰的类不能被继承,可以继承其他类
      6 
      7        被final修饰的变量不能更改,可以在创建对象前赋值,创建对象前调用构造方法,为变量赋值
      8 
      9        被final修饰的引用类型变量,地址值不能更改,地址中的属性可以改变
     10 
     11        被final修饰的方法,不能被子类重写
     12 
     13        
     14 
     15 */
     16 
     17 //定义一个被final修饰的类
     18 
     19 public final class Final{
     20 
     21     final int a = 1;
     22 
     23     final int b;
     24 
     25    public Final(){
     26 
     27       b = 2;
     28 
     29  
     30 
     31    public final void work(){}
     32 
     33    }
     34 
     35 
     36 
     37   public static void main(String[] args){
     38 
     39       Final f = new Final();
     40 
     41      f.name = "张三";
     42 
     43   }
     44 
     45 }
     46 
     47 
     48 
     49 /*
     50 
     51 static:静态修饰符
     52 
     53            多个对象在访问或修改static修饰的成员变量时,其中一个对象改变了成员变量的值,其他对象的成员变量的值跟着改变,即多个对象共享同一个static修饰的成员变量
     54 
     55            被static修饰的成员可以直接用类名调用,类名.静态成员变量      类名.静态成员方法(参数)
     56 
     57            同一类中,静态成员只能访问静态成员
     58 
     59            静态优先于非静态
     60 
     61            定义静态常量,通常使用public static final 数据类型  变量名 = 值
     62 
     63 */
     64 
     65 public class Static{
     66 
     67    public staitc int num = 10;
     68 
     69    public static void main(String[] args){
     70 
     71         Static s = new Static();
     72 
     73         Static s2 = new Static();
     74 
     75         s.num = 111;
     76 
     77         System.out.println(s.num + " " + s2.num);  
     78 
     79   }
     80 
     81 }
     82 
     83 publi class Demo{
     84 
     85 
     86 
     87     public int i = 2;
     88 
     89     public static int a = 1;
     90 
     91     public static final String NAME = "李四";
     92 
     93     public  static void method(){
     94 
     95       System.out.println("静态方法");
     96 
     97     }
     98 
     99 public  static void main(String[] args){
    100 
    101        System.out.println(i);
    102 
    103        System.out.println(Demo.a);
    104 
    105        System.out.println(Demo.NAME);
    106 
    107        Demo.method();       
    108 
    109 }
    110 
    111 }
    112 
    113 
    114 
    115 /*
    116 
    117 匿名对象
    118 
    119 */
    120 
    121 public class Animal{
    122 
    123        public Animal eat(){
    124 
    125               return new Animal; //匿名对象可以作为返回值
    126            }
    127 
    128 
    129           public static void method(Person p){
    130 
    131                 
    132 
    133             }
    134 
    135     public static void main(String[] args){
    136 
    137                 Animal a = new Animal();//普通对象
    138 
    139                 new Animal();//匿名对象
    140 
    141                 Animal.method(new Person());//匿名对象可以作为方法参数
    142 
    143      }
    144 
    145 }
    146 
    147 /*
    148 
    149 内部类:成员内部类、局部内部类、匿名内部类
    150 
    151 */
    152 
    153 //成员内部类
    154 
    155 public class Person{
    156 
    157     class Heart{
    158 
    159       public void jump(){
    160 
    161          System.out.println("跳动");
    162 
    163         }
    164 
    165     }
    166 
    167   public static void main(String[] args){
    168 
    169        Person.Heart p = new Person().new Heart();
    170 
    171         p.jump();
    172 
    173     }
    174 
    175 }
    176 
    177 //局部内部类
    178 
    179 public class Animal{
    180 
    181      public void eat(){
    182 
    183           System.out.println("吃");
    184 
    185            class Cat{
    186 
    187             public void run(){
    188 
    189                System.out.println("跑");
    190 
    191              }
    192 
    193          }
    194 
    195        new Cat().run();//创建内部类对象,调用run方法
    196 
    197     }
    198 
    199    public static void main(String[] args){
    200 
    201       Animal a = new Animal();//创建外部类对象
    202 
    203       a.eat(); //调用外部类方法
    204 
    205    }
    206 }
    207 
    208 /*匿名内部类
    209 
    210   创建接口
    211 
    212 */
    213 
    214 public interface Person{
    215 
    216      public  void eat();
    217 
    218      public void run();
    219 
    220 }
    221 
    222 public class TestPerson implements Person{
    223 
    224    public static void main(String[] args){
    225 
    226               Person p = new Person(){
    227 
    228                   public void eat(){
    229 
    230                    System.out.println("吃饭");
    231 
    232                  }
    233 
    234                  public void run(){
    235 
    236                    System.out.println("跑步");
    237 
    238                   }
    239 
    240              };
    241 
    242             p.eat();
    243 
    244             p.run();
    245 
    246    }
    247 
    248 }
    249 
    250 
    251 
    252 /*
    253 
    254   代码块:静态代码块只执行一次
    255 
    256 */
    257 
    258 public class Animal{
    259 
    260      String name;
    261 
    262      {
    263 
    264       System.out.println("构造代码块");
    265 
    266      }
    267 
    268      public Animal(){
    269 
    270          super();
    271 
    272          System.out.println("无参构造代码块");
    273 
    274      }
    275 
    276      public Animal(String name){
    277 
    278               this.name = name;
    279 
    280      }
    281 
    282     static{
    283 
    284         System.out.println("静态代码块");
    285 
    286      }
    287 
    288      public static void main(String[] args){
    289 
    290               Animal a = new Animal();
    291 
    292               Animal a2 = new Animal("张三");
    293 
    294               System.out.println("普通代码块");
    295 
    296      }
    297 
    298 }
  • 相关阅读:
    慎用const_cast
    python中string的操作函数
    C++ Const总结
    python dict sorted 排序
    "没有找到MSVCP80D.dll,因此这个应用程序未能启动。重新安装应用程序...
    提高你开发效率的十五个 Visual Studio 使用技巧
    一些 python 插件 编译安装的注意事项
    html 制作表格 合并 样式
    将一个表的数据更新到另一个表中
    屏蔽web页面的右键,但不屏蔽输入框中的右键
  • 原文地址:https://www.cnblogs.com/z97-/p/12618195.html
Copyright © 2011-2022 走看看