请解释一下这种情况:继承中的扫描仪

时间:2014-10-05 09:36:31

标签: java inheritance java.util.scanner

我创建了2个类: Quadrangle Point Quadrangle 使用4个 Points 来创建一个实例。< / p>

积分中,我有一个扫描器来获取 inputPoint ,以便我可以使用这些积分来构建四边形

下面的源代码中提供了更详细的实现。 问题是,

  1. 当我在inputPoint内部使Scanner本地化时,它需要在方法结束时关闭。

    如果我关闭它, inputQuadrangle 方法中的inputPoint不能连续使用来创建4个所需的点。我只是认为,每调用一个inputPoint,扫描仪就会一次又一次地创建,它应该可以工作。那么,这里有什么不对?

    以下代码块出错。

    如果我不关闭它,Point类中会出现一个弱点,但inputQuadrangle方法中的inputPoint可以连续使用以创建4个所需的点。

  2. 但是当我让扫描仪像场一样静止时,它不需要关闭,并且随之而来,我可以在inputQuadrangle方法中使用inputPoint而没有任何错误或弱点。

  3. 那么,我错过了什么?请指出。


    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());
    }
    }
    

1 个答案:

答案 0 :(得分:0)

这来自System.in

的文档
The "standard" input stream. This stream is already open and ready to supply input data. 
Typically this stream corresponds to keyboard input or another input source specified by
the host environment or user.

所以,你不必担心关闭System.in,因为它主要用于debugging / testing目的,一旦关闭,你不应该试图从中读取它将导致异常。

您收到NoSuchElementException个异常,因为System.inclosed的上一次调用中inputPoint()并且您的代码正在尝试从中读取。此外,错误与static无关,正如您在第二点中所引用的那样。在这种情况下,如果您关闭扫描仪实例 - 您将收到相同的错误。

但是,如果它是指向某个系统资源(例如file)的流,则应将其关闭。