權限控制
對類中的屬性和方法的可見度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));
}
}
權限