码迷,mamicode.com
首页 > 编程语言 > 详细

Java面向对象--访问权限

时间:2020-04-19 12:38:51      阅读:79      评论:0      收藏:0      [点我收藏+]

标签:ext   person   main   面向   ring   lan   对象   ext1   port   

访问权限


1. public   公共的
2. private  私有的
3. default  包内的
package com.kjy.entity;

public class Person {

    public String pub = "public"; // 公共的
    private String pri = "private"; // 私有的
    String def = "default"; // 包内的

    public static void main(String[] args) {
        Person p = new Person();
        System.out.println(p.pub);
        System.out.println(p.pri);
        System.out.println(p.def);
    }
}
// 在同个包内,private报错
package com.kjy.entity;

public class PersonText {
    public static void main(String[] args) {
        Person p = new Person();
        System.out.println(p.pub);
        System.out.println(p.pri); // 报错
        System.out.println(p.def);
    }
}
//在不同的包内,private,default报错
package com.kjy.dao;

import com.kjy.entity.Person;

public class PersonText1 {
    public static void main(String[] args) {
        Person p = new Person();
        System.out.println(p.pub);
        System.out.println(p.pri); // 报错
        System.out.println(p.def); // 报错
    }

}

Java面向对象--访问权限

标签:ext   person   main   面向   ring   lan   对象   ext1   port   

原文地址:https://www.cnblogs.com/isChenJY/p/12730807.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!