TestPerson.java
class="java" name="code">class Person { private String name; private int age; public void setName(String name) { this.name=name; } public void setAge(int age) { this.age=age; } public String getName(){ return name; } public int getAge(){ return age; } } class Student extends Person { private String school; public String getSchool() { return school; } public void setSchool(String school) { this.school =school; } } public class TestPerson { public static void main(String arg[]){ Student student = new Student(); student.setName("John"); student.setAge(18); student.setSchool("SCH"); System.out.println(student.getName());//John System.out.println(student.getAge());//18 System.out.println(student.getSchool());//SCH } }
TestAccess.java
//对于class的权限修饰只可以用public和default public class TestAccess { } //权限控制 class T { //修饰符修饰成员变量 private int i = 0;//private 类内部 int j = 0;//default 类内部 同一包 protected int k = 0;//protected 类内部 同一包 子类 public int m = 0;//public 类内部 同一包 子类 任何地方 public void m() { i = 9; } } class TT { public void m() { T t = new T(); //System.out.println(t.i);//TestAccess.java:18: 错误: i可以在T中访问private,不能访问private System.out.println(t.j);//编译通过 } }
?