• 七.final与static


    类:final修饰的类不可派生,static修饰的可以是一个静态内部类

    方法:final修饰的方法不可重写(但是可以重载),static修饰的方法优先加载

    属性:final修饰常量,static修饰的静态变量优先加载

    static修饰的方法变量属于类,属于这个类创建的所有对象

    父类的静态方法可以被子类继承,但是不能被子类重写

      1.父类的静态方法不可以重写为非静态方法
      例如:

        public class Person {
          public static void method() {}
        }    
        //编译报错
        public class Student extends Person {
          public void method(){}
        }

      2.父类的静态方法重写为静态方法(或者说:和非静态方法重写后的效果不一样)
      例如:

        public class Person {
          public static void test() {
            System.out.println("Person");
          }
        }
        //编译通过,但不是重写
        public class Student extends Person {
          public static void test(){
            System.out.println("Student");
          }
        }
        main:
          Perosn p = new Student();
          p.test();//输出Person
          p = new Person();
          p.test();//输出Perosn

      3.父类的非静态方法不能被子类重写为静态方法
      例如:

        public class Person {
          public void test() {
            System.out.println("Person");
          }
        }
        //编译报错
        public class Student extends Person {
          public static void test(){
            System.out.println("Student");
          }
        }    
  • 相关阅读:
    Halcon图像分割
    Halcon图像变量
    C# 线程同步
    C# 单例模式实现
    C# 限制窗体弹窗显示必须关闭后才能重新实例化窗体
    Java定时清理过期文件
    GrayLog + Logspout + Docker 实现分布式日志聚合
    基于Spring Security OAuth2搭建的Spring Cloud 认证中心
    快速搭建Docker Registry私有仓库
    让你的Spring Boot应用快速运行在Docker上面
  • 原文地址:https://www.cnblogs.com/Magic-Li/p/12790962.html
Copyright © 2020-2023  润新知