Object-oriented ----- 21Java access control modifier, static keyword, final keyword

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_44787898/article/details/102759441

1. Access control modifiers

Data privatization of public behavior

  • public: public, any class
  • private: private, this class
  • protected: protected, this class, subclass, with bags
  • Default: nothing written, this class, with bags
  • Access modified class: public and default
  • Class members access modifications: If these four are
    Here Insert Picture Description

2.static

2.1 static variables

  • Modified by the static
  • With modified static member variables are not objects of the data structure, it belongs to the class, there is a method area, only one
  • Often accessed through the class name.
  • When to use: all data objects are the same when using
class Cat {
   private int age;
   private static int numOfCats;
   public Cat(int age){
      this.age = age;
      System.out.println(++numOfCats);
   }
}
Cat c1 = new Cat(2);
Cat c2 = new Cat(3);

Here Insert Picture Description

2.2 static method

  • The methods will typically involve operations on specific objects, when calling these methods, the need to pass implicit object reference (this)
int d = p1.distance(p2);
//调用distance方法时,除了传递p2参数外,还隐式的传递了p1作为参数,在方法中的this关键字即表示该参数
  • static modification of the method is not required for the operation of certain objects, its operating results are only related to the input parameters, with direct reference to the class name when calling
double c = Math.sqrt(3.0 * 3.0 + 4.0 * 4.0);
//该方法在调用时,没有隐式的传递对象引用,因此在static方法中不可以使用this关键字
  • This does not mean there is no object, the object must be an instance variable. Access, so a static method can not directly access instance members
  • Since there is no specific static object when invoked, it is not non-static members (members of the) access within a static method. The method of action is to provide static "tool methods" and "factory method" and the like.

2.3 Static block

  • Modified by the static
  • Belong to a class, the class is automatically performed during the loading, the class is loaded only once, so that the static blocks may also be executed only once
  • When to use: they are often used to load / initialize static resources (images, audio, video)
class Foo{
   static{
     //类加载期间,只执行一次
     System.out.println("Load Foo.class");
   }
   public Foo{
     System.out.println("Foo()");
   }
}
Foo foo = new Foo();
//输出的结果为:
//Load Foo.class
//Foo()
//Foo类加载时,运行静态块,在创建对象之前

3.final

  • ------ modified variable variables can not be changed
  • A method of modifying --------- method can not be overridden
  • Modified class ------------ class can not be inherited
package oo.day04;
//final演示
public class FinalDemo {
	public static void main(String[] args) {
	}
}
class Zoo{}
final class Xoo extends Zoo{} //可以继承别的类
//class Yoo extends Xoo{}//错误,final修饰类不能被继承

class Loo{  //final修饰方法---不可被重写
	void show(){}
	final void say(){}
}
class Moo extends Loo{
	void show(){}
	//void say(){} //错误,final方法不能被重写
}

class Koo{  //final修饰变量---不能改变,用得比较少
	final int a = 5; //声明同时初始化
	final int b;   
	Koo(){
		b = 5;   //先声明,再在构造中初始化
	}
	void show(){
		final int c;  //应用率几乎为零
		//System.out.println(c);//错误,c没有值
		c = 5;
		System.out.println(c);
	}
  void say(){
  	//a = 88; //错误,final修饰的变量不能改变
  }
}

  • Modified static final member variables are called constants must be declared at the same time to initialize. Not be changed
  • static final constants will be replaced at compile time
class Foo{
   public static final int NUM = 100;
}
class Goo{
   public static void main(String[] args){
      System.out.println(Foo.NUM);
      //代码编译时,会替换为:System.out.println(100);
   }
}

supplement

Member variables

1. instance variables:
1) the object belongs to, the presence of the heap
2) the number of instance variables there are several objects
3) must be accessed through the object name.
2. A static variable
1) belong to a class, there is a method in zone
2) only one
3) often accessed through the class name.

class Aoo{
    int a;//实例变量
    static int b;//静态变量
}

Guess you like

Origin blog.csdn.net/qq_44787898/article/details/102759441