【发布时间】:2021-01-14 05:23:27
【问题描述】:
就从课外的可访问性而言,可变等级中的可访问数据和可修改数据有什么区别?
这是我在课堂外调用grade方法的代码:
package studenttester;
public class student {
private String name;
private int age;
private String grade;
private double average;
private boolean disability;
private void printStudentInfo(){ //Data Encapsulation is methods of the public interface provide access to private data, while hiding implementation.
System.out.println("Name: "+name+",Age: "+age+",Grade: "+grade+",Average: "+average +" Disability: "+disability);
}
public void setGrade(String newGrade){
grade=newGrade;
}
public String getGrade(){
return grade;
}
}
public class StudentTester{
public static void main(String[] args){
student S1 = new student();
student S2 = new student();
S1.setGrade("11");
System.out.println("Student one: " +S1.getGrade()+", Student two: "+S2.getGrade());
/* The instance variables are name, age, grade, average, disability and those are the variables that the object S2 contains. The object’s attributes are : name, age, grade, average, disability.
S1 attributes values : name: null , age:0, grade:"11", average : 0.0, disability: false
S2 attributes values : name: null , age:0, grade:null, average : 0.0, disability: false */
}
}
【问题讨论】: