final, authority, reference type data

1. final keyword

1 Overview

In order to avoid the emergence of a subclass of the case freely rewrite the parent class, java provide a keyword finalfor modifying the content can not be changed

  • final: unchangeable, can be modified classes, methods and variables
    • Categories: modified class, it can not be used to inherit
    • Methods: The modified method can not be overridden
    • Variables: modified variables can not be reassigned

2. use

Modified class

Format is as follows:

public final class 类名{ } 含义: 当前这个类不能拥有任何的子类. (太监类) 注意: 一个类如果是final的, 那么其中的所有的成员方法都无法进行覆盖重写(没有儿子) 

String, Math, Scanner these classes are being modified final aim is for our use, and not let us change the content

Modification methods

format:

修饰符 final 返回值类型 方法名(参数列表){ //方法体 } //注意: 对于类,方法来说, abstract关键字和关键字final 不能同时使用,矛盾 

Rewrite finalmodified method, will compile-time error

Modified variables
1. Local variables - basic types

Basic types of local variables, was finallater modified, can only be assigned once and can not be changed.

public class FianlDemo{
  public static void main(String[] args){ // 声明变量,被final修饰 final int a; //第一次赋值 a = 10; //第二次赋值直接报错 final int b = 10; } } 
写法一
final int c = 0;
for(int i=0;i<10;i++){ c = i; System.out.println(c); } 
写法二
for(int i=0;i<10;i++){ final int c = i; System.out.println(c); } 

Written two will not complain, because every cycle is a new variable

2. Local variables - the type of data reference

Local variables of reference types, was modified after the final, can only point to an object, the address can not be changed, but does not affect the member variables inside the object to modify worth

public class FinalDemo2{
  public static void main(String[] args){ // 创建User对象 final User u = new User(); // 创建另一个对象 u = new User(); //报错 //调用方法 u.setName("张三"); //可以修改 } } 
3. member variables

Member variable initialization of design problems, two kinds of initializing a second election:

  1. Display initialization

    public class User{
      final String UserName = "张三"; private int age; } 
  2. Constructor to initialize

    public class User{
      final String UserName; private int age; public User(String username, int age){ this.UserName = username; this.age = age; } } // 注意: 被final修饰的常量名称,一般全部大写 

2. permission modifier

When java in four access rights, use different permissions modifier modified, the modified content will have different access rights

  1. public: public
  2. protected: Protected
  3. default: default
  4. private: private

1. The ability to access different privileges

  public protected default private
The same class yes yes yes yes
The same package (regardless of class and subclass) yes yes yes  
Subclasses of different packages yes yes    
Different classes of packets independent yes      

is the largest public authority, private authority is minimal

Compile the code recommends that:

  1. Member variables private, hidden details
  2. Constructor uses public, square changed to create objects
  3. Members method uses public, easy call

Without modifier, it is the ability to access the same default

3. The reference data usage summary

1.class as a member variable

Class as a member variable of time, to carry out his assignment, in fact, is assigned to a class of objects

2.interface as a member variable

Interface member variables as time, to carry out his assignment, and leave on the pretext that is assigned to a subclass object

3.interface value as a parameter and return type

Interface as parameters, is passed subclass object

Type as the return value of the interface when it returns subclass object

Guess you like

Origin www.cnblogs.com/liudemeng/p/11357081.html