权限控制
对类中的属性和方法的可见度this
访问权限spa
控制符 同类 同包 不一样 继承
public 能够 能够 能够 能够
default 能够 能够 不能够 不能够
protected 能够 能够 不能够 能够
private 能够 不能够 不能够 不能够对象
类的控制符
public 在本类,同一个包, 不一样包均可以访问
default 在本类,同一个包中能够访问,其余的不行继承
属性/方法
public 在本类,同一个包,不一样包均可以访问, 子父类是能够的
default 在本类,同一个包能够访问,其余不行,子父类在同一个包是能够,不一样包 不行get
protected 在本类,同一个包,还有继承能够访问,其余的不行
private 在本类能够,其余的不行权限控制
1.定义一个“点”(Point)类用来表示三维空间中的点(有三个坐标)。要求以下:
A。能够生成具备特定坐标的点对象。
B。提供能够设置三个坐标的方法。
//C。提供能够计算该点距离另外一点距离平方的方法。it
package com.qianfeng.homework;class
public class Point {
private double x;
private double y;
private double z;
public Point() {
}
public Point(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getZ() {
return z;
}
public void setZ(double z) {
this.z = z;
}
public void info() {
System.out.println("Point [x=" + x + ", y=" + y + ", z=" + z + "]");
}
//直接传坐标
public double distance(double x, double y, double z){
return Math.sqrt(Math.pow(this.x - x, 2)
+ Math.pow(this.y - y, 2)
+ Math.pow(this.z - z, 2));
}
//传点对象
public double distance(Point point){
return Math.sqrt(Math.pow(this.x - point.getX(), 2) +
Math.pow(this.y - point.getY(), 2) +
Math.pow(this.z - point.getZ(), 2));
}
public void method(){
Point point = this;
this.distance(this);
}
//只须要打印,不须要返回,或者说,不关心它返回值,能够使用void
public void print(Point point){
System.out.println("Point [x=" + point.getX() + ", y=" + point.getY()
+ ", z=" + point.getZ() + "]");
}
//方法传引用类型,须要对引用类型进行修改,这个时候,能够定义方法有返回值
//也能够没法方法值,建议使用没法返回值
//说白了,【调用方须要须要返回值时候,那方法就定义放回值】
public void editNoReturn(Point point){
point.setX(7);
point.setY(8);
point.setZ(9);
}
public Point editHasReturn(Point point){
point.setX(7);
point.setY(8);
point.setZ(9);
return point;
}
//可是,若是调用方想知道方法的执行结果时,或者说,须要经过这个方法返回值进行
//下一步逻辑处理时,这个时候,须要返回值
public boolean editNoReturn2(Point point){
point.setX(7);
point.setY(8);
point.setZ(9);
return true;
}
public static void main(String[] args) {
/*Point point = new Point();
FieldQX f = new FieldQX();
System.out.println(point);
System.out.println(f);*/
//System.out.println( Math.pow(2, 3));
Point point1 = new Point(1, 2, 3);
Point point2 = new Point(2, 4, 6);
point1.print(point2);
System.out.println("-----------------------");
//point1.editNoReturn(point2);
//point1.print(point2);
/*Point point3 =*/ point1.editHasReturn(point2);
point1.print(point2);
/*double x = point2.getX();
point1.editNoReturn(point2);
double xx = point2.getX();
if(x == xx){
System.out.println("改变了");
}else{
System.out.println("未改变");
}*/
if(point1.editNoReturn2(point2)){
System.out.println("改变了");
}else{
System.out.println("未改变");
}
System.out.println(point1.distance(point2));
}
}
权限