zoukankan      html  css  js  c++  java
  • Java SE 第二十三讲----static关键字and final关键字

    1.static关键字 【在二十二讲视频中30分钟开始讲授】

    2.static修饰属性:无论一个类生成了多少个对象,所有这些对象共同使用唯一一份静态的成员变量;一个对象对该静态成员变量进行了修改,其他对象的该静态成员变量的值也会随之发生变化。如果一个成员变量是static的,那么我们可以通过类名.成员变量的方式来使用它(java推荐使用这种方式)。

    package com.cl.abstracttest;
    
    public class StaticTest {
    
        public static void main(String[] args){
            MyStatic mystatic = new MyStatic();
            MyStatic mystatic2 = new MyStatic();
            mystatic.a = 10;
            System.out.println(mystatic2.a);
        }
    }
    
    class MyStatic
    {
         int a;
    }

    结果为:0

    
    

    将int a 改为 static int a 结果就变成了10

     
    package com.cl.abstracttest;
    
    public class StaticTest {
    
        public static void main(String[] args){
        
            MyStatic mystatic = new MyStatic();
            MyStatic.a = 10;//直接用类名.成员变量方式
            System.out.println(mystatic.a);
            
        }
    }
    
    class MyStatic
    {
         static int a;
    }

     3.static修改方法:static修饰的方法叫做静态方法。对于静态方法来说,可以使用类名.方法名的方式来访问

    package com.cl.staticandfinal;
    
    public class StaticTest2 {
    
        public static void main(String[] args) {
            /*方法一
            MyStatic2 s = new MyStatic2();
            s.output();*/
            
            //方法二
            MyStatic2.output();//需要将方法变成static
        }
    }
    
    class MyStatic2{
        public  static void output(){
            System.out.println("output");
        }
    }

    4.静态方法只能继承,不能重写(Override)。子类不能重写父类方法,可以重载父类方法

    package com.cl.staticandfinal;
    
    public class StaticTest3 {
        public static void main(String[] args) {
            M m = new N();
            m.output();
        } 
        
    }
    
    class M
    {
        public  static void output(){
            System.out.println("M");
        }
    }
    class N extends M
    {
        public static void output(){
            System.out.println("N");
        }
    }
    输出结果为:M
    如果把
     M m = new N();改为 N m = new N();结果就是N
    
    

    5.final关键字:final可以修饰属性、方法、类。

    6.final修饰类:当一个类被final所修饰时,表示该类是一个终态类,即不能被继承。

    7.final修饰方法:当一个方法被final所修饰时,表示该方法是个终态方法,即不能被重写(Overried)

    package com.cl.staticandfinal;
    
    public class FinalTest2 {
        public static void main(String[] args) {
            H h = new H();
            h.output();
        }
    
    }
    class G
    {
        public final/*加了final修饰*/ void output(){
            
            System.out.println("G");
        }
    }
    class H extends G
    {
        public void output()//编译报错:Cannot override the final method  from G
        {
            System.out.println("H");
        }
    }

    8.final修饰属性:当一个属性被fina所修饰时,表示该属性不能被改写。

    package com.cl.staticandfinal;
    
    public class FinalTest3 {
    
        public static void main(String[] args) {
            People people = new People();
            people.age = 20;//age这里会编译错误The final field People.age cannot be assigned
        }
    }
    class People
    {
        final int age = 10;
    }

    9.当final修饰一个原生数据类型时,表示该原生数据类型的值不能发生变化(比如说不能从10变为20),如果final修饰一个引用类型时,表示该引用类型不能再指向其他对象了,但该引用所指向的对象的内容是可以发生变化的。

    package com.cl.staticandfinal;
    
    public class FinalTest3 {
    
        public static void main(String[] args) {
            People people = new People();
            people.address = new Address();//这里会编译错误The final field People.address cannot be assigned
            people.address.name="shanghai";//这里不会报错
            
        }
    }
    class People
    {
        final Address address = new Address();
    }
    
    class Address{
        
        String name = "beijing";
    }

    10.

  • 相关阅读:
    JVM classloader
    面试整理
    工具配置链接
    IntelliJ IDEA 热部署
    IntelliJ IDEA 常用快捷键
    类文件结构
    判断对象存活的方法
    JVM 运行时数据区域
    vim编辑16进制
    阿里云yum源
  • 原文地址:https://www.cnblogs.com/dieyaxianju/p/5142719.html
Copyright © 2011-2022 走看看