zoukankan      html  css  js  c++  java
  • 七.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");
          }
        }    
  • 相关阅读:
    USACO Section 2.2 Subset Sums
    九度 1399 名侦探柯南
    九度 1416 猴子吃坚果
    pch文件的使用(原作者太逗了)
    线程同步
    extern "c"
    进程与线程
    排序算法代码汇总
    Linux Shell 常用命令与目录分区的学习总结 (开始学习linux)
    堆和栈
  • 原文地址:https://www.cnblogs.com/Magic-Li/p/12790962.html
Copyright © 2011-2022 走看看