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");
          }
        }    
  • 相关阅读:
    LeetCode Count of Range Sum
    LeetCode 158. Read N Characters Given Read4 II
    LeetCode 157. Read N Characters Given Read4
    LeetCode 317. Shortest Distance from All Buildings
    LeetCode Smallest Rectangle Enclosing Black Pixels
    LeetCode 315. Count of Smaller Numbers After Self
    LeetCode 332. Reconstruct Itinerary
    LeetCode 310. Minimum Height Trees
    LeetCode 163. Missing Ranges
    LeetCode Verify Preorder Serialization of a Binary Tree
  • 原文地址:https://www.cnblogs.com/Magic-Li/p/12790962.html
Copyright © 2011-2022 走看看