1. 程式人生 > >Java練習 SDUT-2670_3-1 Point類的建構函式

Java練習 SDUT-2670_3-1 Point類的建構函式

3-1 Point類的建構函式

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description

通過本題目的練習可以掌握類的建構函式的定義;

設計一個點類Point,私有資料成員有x、y;公有成員函式有:無引數的建構函式Point(),帶引數的建構函式Point(int,int);ShowPoint()輸出點物件的資訊

在主函式main()中呼叫相應成員函式,從鍵盤接收時間物件的x和y的值,並向顯示器輸出相應的值。

Input

輸入2個整數,用一個空格間隔

Output

要求先輸出預設的點值,再輸出使用者構造的點的值

點的格式為:一對圓括號內 x,y的值,中間用“,”間隔;

Sample Input

10 11

Sample Output

(0,0)
(10,11)

重構函式,關於輸出有點迷,初始值在輸入前後輸出都可以。

import java.util.*;

public class Main {
    public static void main(String[] args)
    {
        Scanner cin = new Scanner(System.in);
        point a = new point();
        a.show();
        a = new point(cin.nextInt(),cin.nextInt());
        a.show();
        cin.close();
    }
}


class point
{
    private int x,y;
    public point()
    {
        x = 0;
        y = 0;
    }
    public point(int x,int y)
    {
        this.x = x;
        this.y = y;
    }
    public void show()
    {
        System.out.printf("(%d,%d)\n",x,y);
    }
}