我創建了2類:四合院和點,四合院使用4個點創建一個實例。請解釋一下這種情況:掃描儀在繼承
在點,我有一個掃描儀得到inputPoint,這樣我就可以用這些點數來建立一個四合院
的更詳細的實施在下面的源代碼提供。 問題是,
當我做了掃描儀的inputPoint內部本地,它需要在方法的端部緊密權利。
如果我關閉它,inputQuadrangle方法中的inputPoint不能連續使用以創建4個需要的點。我只是認爲,在每個inputPoint調用時,掃描器一次又一次地創建,它應該可以工作。那麼,這裏有什麼問題?
下面的代碼塊中存在錯誤。
如果我不關閉它,在Point類中存在一個弱點,但inputQuadrangle方法中的inputPoint可以連續使用以創建4個需要的點。
但是,當我使掃描儀靜態像一個字段,它不需要關閉,並與此同時,我可以在inputQuadrangle方法中使用inputPoint沒有任何錯誤或弱點。
那麼,我錯過了什麼?請指出。
Point class with local scanner
package OOPBaiTap;
import java.util.Scanner;
public class Point {
// Thuoc tinh
private String name;
private double x;
private double y;
// Phuong thuc khoi tao mac dinh
public Point() {
this("A", 0, 0);
}
// Phuong thuc khoi tao nhan hai so thuc lam tham so
public Point(double x, double y) {
this.x = x;
this.y = y;
}
// Phuong thuc khoi tao nhan mot chuoi va hai so thuc lam tham so
public Point(String name, double x, double y) {
this.name = name;
this.x = x;
this.y = y;
}
// Phuong thuc khoi toa nhan mot doi tuong cua lop Poin lam tham so
public Point(Point point) {
this.name = point.getName();
this.x = point.getX();
this.y = point.getY();
}
// Cac phuong thuc get, set
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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;
}
// Phuong thuc nhap vao mot diem duoi dang hai so thuc, cach nhau boi dau
// ","
public static Point inputPoint() {
Point point = null;
System.out
.println("Nhap vao tung do va hoanh do, cach nhau mot khoang trang: ");
Scanner input = new Scanner(System.in);
String[] s = input.nextLine().split(" ");
point = new Point(Double.parseDouble(s[0]), Double.parseDouble(s[1]));
input.close();
return point;
}
// Phuong thuc toString()
@Override
public String toString() {
String s;
if (this.getName() != null) {
s = this.getName() + "(" + this.getX() + ", " + this.getY() + ")";
} else {
s = "(" + this.getX() + ", " + this.getY() + ")";
}
return s;
}
// Phuong thuc main
public static void main(String[] args) {
// Diem khoi tao mac dinh
Point A = new Point();
System.out.println(A.toString());
System.out.println();
// Diem khoi tao voi hai so thuc
Point B = new Point(2, 5);
System.out.println(B.toString());
System.out.println();
// Diem khoi tao voi mot xau va hai so thuc
Point C = new Point("C", 4, 5);
System.out.println(C.toString());
Point D = new Point("D", 3, 2);
System.out.println();
// Diem khoi tao voi tham so la mot doi tuong cua lop Point
Point E = new Point(D);
System.out.println(E.toString());
System.out.println();
// Nhap vao tung do va hoanh do cua mot diem, sau do in diem do ra
// console
Point F = inputPoint();
System.out.println(F.toString());
}
}
Error code thrown out when trying to input a Quadrangle.
****
input p1: success
input p2:
Exception in thread "main" Java.util.NoSuchElementException: No line found
at Java.util.Scanner.nextLine(Scanner.Java:1540)
at OOPBaiTap.Point.inputPoint(Point.Java:68)
at OOPBaiTap.Quadrangle.inputQuadrangle(Quadrangle.Java:30)
at OOPBaiTap.Quadrangle.main(Quadrangle.Java:95)
Point class with static field scanner
package OOPBaiTap;
import java.util.Scanner;
public class Point {
// Thuoc tinh
private String name;
private double x;
private double y;
private static Scanner input;
// Phuong thuc khoi tao mac dinh
public Point() {
this("A", 0, 0);
}
// Phuong thuc khoi tao nhan hai so thuc lam tham so
public Point(double x, double y) {
this.x = x;
this.y = y;
}
// Phuong thuc khoi tao nhan mot chuoi va hai so thuc lam tham so
public Point(String name, double x, double y) {
this.name = name;
this.x = x;
this.y = y;
}
// Phuong thuc khoi toa nhan mot doi tuong cua lop Poin lam tham so
public Point(Point point) {
this.name = point.getName();
this.x = point.getX();
this.y = point.getY();
}
// Cac phuong thuc get, set
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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;
}
// Phuong thuc nhap vao mot diem duoi dang hai so thuc, cach nhau boi dau
// ","
public static Point inputPoint() {
Point point = null;
System.out
.println("Nhap vao tung do va hoanh do, cach nhau mot khoang trang: ");
input = new Scanner(System.in);
String[] s = input.nextLine().split(" ");
point = new Point(Double.parseDouble(s[0]), Double.parseDouble(s[1]));
return point;
}
// Phuong thuc toString()
@Override
public String toString() {
String s;
if (this.getName() != null) {
s = this.getName() + "(" + this.getX() + ", " + this.getY() + ")";
} else {
s = "(" + this.getX() + ", " + this.getY() + ")";
}
return s;
}
// Phuong thuc main
public static void main(String[] args) {
// Diem khoi tao mac dinh
Point A = new Point();
System.out.println(A.toString());
System.out.println();
// Diem khoi tao voi hai so thuc
Point B = new Point(2, 5);
System.out.println(B.toString());
System.out.println();
// Diem khoi tao voi mot xau va hai so thuc
Point C = new Point("C", 4, 5);
System.out.println(C.toString());
Point D = new Point("D", 3, 2);
System.out.println();
// Diem khoi tao voi tham so la mot doi tuong cua lop Point
Point E = new Point(D);
System.out.println(E.toString());
System.out.println();
// Nhap vao tung do va hoanh do cua mot diem, sau do in diem do ra
// console
Point F = inputPoint();
System.out.println(F.toString());
}
}
Quadrangle class
package OOPBaiTap;
public class Quadrangle {
// Thuoc tinh
private Point p1;
private Point p2;
private Point p3;
private Point p4;
// Phuong thuc khoi tao mac dinh
public Quadrangle() {
this(new Point("A", 0, 0), new Point("B", 0, 1), new Point("C", 1, 1),
new Point("D", 1, 0));
}
// Phuong thuc khoi tao nhan 4 doi tuong thuoc lop poin lam tham so
public Quadrangle(Point p1, Point p2, Point p3, Point p4) {
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
this.p4 = p4;
}
// Phuong thuc nhap vao mot tu giac 4 diem tu ban phim
public static Quadrangle inputQuadrangle() {
Quadrangle quadrangle = null;
System.out.println("Nhap vao diem p1: ");
Point p1 = Point.inputPoint();
System.out.println("Nhap vao diem p2: ");
Point p2 = Point.inputPoint();
System.out.println("Nhap vao diem p3: ");
Point p3 = Point.inputPoint();
System.out.println("Nhap vao diem p4: ");
Point p4 = Point.inputPoint();
quadrangle = new Quadrangle(p1, p2, p3, p4);
return quadrangle;
}
// Cac phuong thuc get set
public Point getP1() {
return p1;
}
public void setP1(Point p1) {
this.p1 = p1;
}
public Point getP2() {
return p2;
}
public void setP2(Point p2) {
this.p2 = p2;
}
public Point getP3() {
return p3;
}
public void setP3(Point p3) {
this.p3 = p3;
}
public Point getP4() {
return p4;
}
public void setP4(Point p4) {
this.p4 = p4;
}
// Phuong thuc toString()
@Override
public String toString() {
String s = new String(p1.toString() + ", " + p2.toString() + ", "
+ p3.toString() + ", " + p4.toString() + ".");
return s;
}
// Phuong thuc main()
public static void main(String[] args) {
// Phuong thuc khoi tao mac dinh
Quadrangle a = new Quadrangle();
System.out.println(a.toString());
System.out.println();
// Phuong thuc khoi tao nhan 4 doi tuong thuoc lop point lam tham so
Point p1 = new Point(0, 0);
Point p2 = new Point(0, 2);
Point p3 = new Point(2, 1);
Point p4 = new Point(1, 0);
Quadrangle b = new Quadrangle(p1, p2, p3, p4);
System.out.println(b.toString());
System.out.println();
// Nhap vao 4 diem cua tu giac tu ban phim
Quadrangle c = inputQuadrangle();
System.out.println(c.toString());
}
}
關閉'java.util.Scanner'(使用'input.close()')還會關閉基礎流'System.in'。後續對'Scanner.nextLine()'的調用無法從關閉的System.in中讀取更多行。 – halfbit 2014-10-05 10:36:38