zoukankan      html  css  js  c++  java
  • static 静态域 类域 静态方法 工厂方法 he use of the static keyword to create fields and methods that belong to the class, rather than to an instance of the class 非访问修饰符

     总结:

    1、无论一个类实例化多少对象,它的静态变量只有一份拷贝;

    静态域属于类,而非由类构造的实例化的对象,所有类的实例对象共享静态域。

    class Employee

    {

      private static int nextId = 1;

      private int id;

      ...

    }

    静态变量

    静态常量

    public clas Math

    {

      ...

      public static final double PI = 3.14159...;

    }

    静态方法不能向对象实施操作

    Math.pow(2,10);

    Employee类的静态方法不能访问Id实例域,因为它不能操作对象;但是静态方法可以访问自身类中的静态域。

    public static int getNextId()

    {

      return nextId; // returns static field

    }

    使用静态方法的2种情况:

    0-

    一个方法不需要访问对象状态,其所需的参数都是通过显示参数提供; (Math.pow);

    1-

    一个方法只需要访问类的静态域;(Employee.getNextId);

    静态方法:

    factory method 工厂方法  LocalDate.now

    main 

    public class Application

    {

      public static void main(String[] args)

      {

        //construct objects here

        ...

      }

    }

    【call static method】

    public class StaticTest {
        public static void main(String[] args) {
            //fill the staff with three Employee objects
            Employee[] staff = new Employee[3];
    
            staff[0] = new Employee("Tom", 40000);
            staff[1] = new Employee("Dick", 60000);
            staff[2] = new Employee("Harry", 90000);
    
            //print out information about all Employee objects
            for (Employee e : staff) {
                e.setId();
                System.out.println("name=" + e.getName() + ",id=" + e.getId() + ",salary=" + e.getSalary());
            }
    
            //call static method
            int n = Employee.getNextId();
    
            System.out.println("Next available id=" + n);
        }
    }
    
    class Employee {
        private static int nextId = 1;
        private String name;
        private double salary;
        private int id;
    
        public Employee(String n, double s) {
            name = n;
            salary = s;
            id = 0;
        }
    
        public String getName() {
            return name;
        }
    
        public double getSalary() {
            return salary;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId() {
            id = nextId;
            nextId++;
        }
    
        public static int getNextId() {
            return nextId; //returns static field
        }
    
        public static void main(String[] args) {
            Employee e = new Employee("Harry", 50000);
            System.out.println(e.getName() + "" + e.getSalary());
        }
    }

    Java 修饰符 | 菜鸟教程 http://www.runoob.com/java/java-modifier-types.html

    非访问修饰符

    为了实现一些其他的功能,Java 也提供了许多非访问修饰符。

    static 修饰符,用来修饰类方法和类变量。

    final 修饰符,用来修饰类、方法和变量,final 修饰的类不能够被继承,修饰的方法不能被继承类重新定义,修饰的变量为常量,是不可修改的。

    abstract 修饰符,用来创建抽象类和抽象方法。

    synchronized 和 volatile 修饰符,主要用于线程的编程。

    static 修饰符

    • 静态变量:

      static 关键字用来声明独立于对象的静态变量,无论一个类实例化多少对象,它的静态变量只有一份拷贝。 静态变量也被称为类变量。局部变量不能被声明为 static 变量。

    • 静态方法:

      static 关键字用来声明独立于对象的静态方法。静态方法不能使用类的非静态变量。静态方法从参数列表得到数据,然后计算这些数据。

  • 相关阅读:
    抽象工厂模式
    工厂方法模式
    简单工厂模式
    多例模式
    深入分析 Java 中的中文编码问题
    PipedInputStream和PipedOutputStream详解
    单例模式
    Java IO和NIO文章目录
    wsdlLocation可以写成项目的相对路劲吗
    ssh框架配置事务管理器
  • 原文地址:https://www.cnblogs.com/rsapaper/p/7835051.html
Copyright © 2011-2022 走看看