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");
          }
        }    
  • 相关阅读:
    如何写一个使用Web Service的IOS应用
    iPad定制相机界面
    IOS Quartz 2D 学习(1)
    cocoa Shallow Copy与Deep Copy
    sqlite3_prepare_v2返回1
    IOS 监听相机对焦事件
    UIImageView添加响应事件无响应
    二、Mongodb常用命令
    三、Mongodb Java中的使用
    多测师肖老师__第二个月python安装和pycharm安装
  • 原文地址:https://www.cnblogs.com/Magic-Li/p/12790962.html
Copyright © 2011-2022 走看看