public class Student { private int id; private String name; private int score; public Student copy() { Student std = new Student(); std.id = this.id; std.name = this.name; std.score = this.score; return std; } }
在copy()中,为什么std变量还能调用id,name,score属性,std.id,std.name....呢?这个属性不是private吗?
其实在内存里,不管是public还是private,都是一个地址而已。
语法规定class内部代码可以调用private,外部代码可以调用public,只是编译器防止人犯错。
Sign in to make a reply
young
在copy()中,为什么std变量还能调用id,name,score属性,std.id,std.name....呢?这个属性不是private吗?