例如:
具有private
属性的类:
public class Main {
private String fname = "cjavapy";
private String lname = "python";
private String email = "cjavapy@gmail.com";
private int age = 30;
public static void main(String[] args) {
Main myObj = new Main();
System.out.println("Name: " + myObj.fname + " " + myObj.lname);
System.out.println("Email: " + myObj.email);
System.out.println("Age: " + myObj.age);
}
}
1、定义和用法
private
关键字是用于属性,方法和构造函数的访问修饰符,使它们只能在声明的类中访问。