5、Java 修饰符

Java访访

1访

publicprotecteddefaultprivate西使

2

  protected使javajavamainmain

  main使访main访java访访

  访privatepublic

3

package com.example.test;

/**
* @author lin
* @version 1.0
* @date 2020/6/21 20:21
* @Description TODO
*/
public class Person {
   public String uname = "person  ";

   public void introduceMyself() {
       System.out.println("com.example.test " + uname);
  }
}
package com.example.test;

/**
* @author lin
* @version 1.0
* @date 2020/6/21 20:21
* @Description TODO
*/
public class Student {
   Person p = new Person();

   public void test() {
       System.out.println("com.example.test " + p.uname);
  }
}
package com.example.test;

/**
* @author lin
* @version 1.0
* @date 2020/6/21 20:21
* @Description TODO
*/
public class Teacher extends Person {
   public int age;
   Person p = new Person();

   public void test1() {
       System.out.println("com.example.test " + p.uname);
  }
}
package com.example.test2;

/**
* @author lin
* @version 1.0
* @date 2020/6/21 20:22
* @Description TODO
*/
public class Person {
   public String uname = "haha";

   public void test2() {
       System.out.println("com.example.test2 " + uname);
  }

}

package com.example.test;

/**
* @author lin
* @version 1.0
* @date 2020/6/21 20:23
* @Description TODO
*/
public class Test {
   public static void main(String[] args) {
       Person person = new Person();
       person.introduceMyself();
       Student student = new Student();
       student.test();
       Teacher teacher = new Teacher();
       teacher.test1();
       com.example.test2.Person person1 = new com.example.test2.Person();
       person1.test2();
  }
}

1 public访

2  protected

Personuanmeprotected  Parents便  The field Person.uname is not visible

3private

访 Personunamesetget便访

4default

访  使 访

原文地址:https://www.cnblogs.com/naimao/p/13346482.html