java base - class member access control

I. Introduction

This article is based on a text, not much depth, for the developer must be proficient in mind. The subject of this article is why the java class members to set access levels? The reason is very simple, is to package the object-oriented characteristics; class members will use different levels of access control, after the data package and the other class members want to access the current class members must have sufficient privileges to access; the purpose of this is that I do not want to be free to modify other class members do not have permission to access the data, as long as the good agreement related to the agreement does not affect access to other class members;

Permission to introduce two qualifiers

  1. public means public meaning, it modifies members of least privilege, to represent any member can access; note that point is a java source file can have only one class is declared public ;
  2. defaut default level, members did not modify symbols, other members can be accessed within the same package;
  3. protected by protective means; it represents the parent class subclasses can inherit; in the same package as modified by its members to access its subclasses; Attention will not modified outside the class ;
  4. private private means; the highest authority indicates that only allows access to the internal class members; pay attention to the point that can not be modified outside of class ;
Modifiers The same class A package with other classes Different subclasses packet Feizi different packet classes
public true true true true
protected true true true
default true true
private true

Three DETAILED DESCRIPTION

3.1 public

Com.youku1327.base.authority2 package created in a planet type, there is a member represented shine light, getShine method using the corresponding public modification process is to obtain the light means;

package com.youku1327.base.authority2;

/**
 * @Author lsc
 * <p> 星球类 </p>
 */
public class Planet {

    private String shine = "好耀眼";

    public String getShine(){
        return shine;
    }
}

Creating classes in the package com.youku1327.base.authority1 ZSZXZ, using import com.youku1327.base.authority2.Planet; import Planet planet class; class planet created in the main method, method calls to obtain the light, to obtain a good output is dazzling; verify the lowest public authority, have already sub-class can access between different packages;

package com.youku1327.base.authority1;

import com.youku1327.base.authority2.Planet;

/**
 * @Author lsc
 * <p> </p>
 */
public class ZSZXZ {

    public static void main(String[] args) {
        // 创建星球实体
        Planet planet = new Planet();
        // 访问公有成员
        String shine = planet.getShine();
        // 好耀眼
        System.out.println(shine);
    }

}

3.2 protected

The method of planet getShine modified symbols to protected class;

package com.youku1327.base.authority2;

/**
 * @Author lsc
 * <p> 星球类 </p>
 */
public class Planet {

    private String shine = "好耀眼";

    protected String getShine(){
        return shine;
    }
}

Creating Earth like planet in com.youku1327.base.authority1 inheritance class Planet (beginners once you understand the meaning of inheritance extends keyword indicates), an inherited method is getShine method, and then create an Earth instance in the main class, call getShine method, actually the Planet of getShine method invocation (super.getShine (), super senior means, represents the parent class); verify the subclasses of different packages can call the parent class members are protected modified;

package com.youku1327.base.authority1;

import com.youku1327.base.authority2.Planet;

/**
 * @Author lsc
 * <p> 地球类</p>
 */
public class Earth extends Planet {

    @Override
    protected String getShine() {
        // 调用了父类的getShine方法
        return super.getShine();
    }
    // 执行前注释掉ZSZXZ中的main方法
    public static void main(String[] args) {
        // 创建地球
        Earth earth = new Earth();
        // 好耀眼
        System.out.println(earth.getShine());
    }
}

Go back and look at the original ZSZXZ class getShine found being given; to verify the non-child classes in different packages, the use of protected modified inaccessible;

Here Insert Picture Description

3.3 default

The method modifier getShine removed Planet planet class, as follows

package com.youku1327.base.authority2;

/**
 * @Author lsc
 * <p> 星球类 </p>
 */
public class Planet {

    private String shine = "好耀眼";

    String getShine(){
        return shine;
    }
}

With the discovery of the Earth before being given a class, we put the whole earth class commented; verify the modified default package access members can not boast;

Here Insert Picture Description

We created under the same level of Planet Moon package type, method call getShine Planet of successful implementation; verify the default modified member can be accessed in the same package;

package com.youku1327.base.authority2;

/**
 * @Author lsc
 * <p> </p>
 */
public class Moon  {

    public static void main(String[] args) {
        // 创建星球实体
        Planet planet = new Planet();
        // 访问公有成员
        String shine = planet.getShine();
        // 好耀眼
        System.out.println(shine);
    }

}

3.4 private

The method modifier getShine Planet planet class to Private;

package com.youku1327.base.authority2;

/**
 * @Author lsc
 * <p> 星球类 </p>
 */
public class Planet {

    private String shine = "好耀眼";

    private String getShine(){
        return shine;
    }
}

The method of the main class getShine Moon previous methods found in the error, verify the package private with different classes can not be accessed; getShine () can shine in the field, to verify the members of the same class can be used in a modified private ;

 private String shine = "好耀眼";

    private String getShine(){
        return shine;
    }
}

The method of the main class getShine Moon previous methods found in the error, verify the package private with different classes can not be accessed; getShine () can shine in the field, to verify the members of the same class can be used in a modified private ;

Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/zszxz/p/12076695.html