zoukankan      html  css  js  c++  java
  • Java面向对象--访问权限

    访问权限


    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); // 报错
        }
    
    }
    
  • 相关阅读:
    【逆序对】N*M Puzzle / Simple Puzzle
    【逆序对】Ultra
    bzoj 1814 Fornula 1
    hdu 1693 插头dp入门
    bzoj 2154
    没有上司的舞会
    【HNOI】合唱队
    luogu 自适应Simpson2
    luogu 自适应Simpson1
    【bzoj 1502】月下柠檬树
  • 原文地址:https://www.cnblogs.com/isChenJY/p/12730807.html
Copyright © 2011-2022 走看看