JAVA - Geter、Seter练习题

2015-04-25T16:40:00

题目

解答

    package com.software.oop;
    
    public final class MyPoint {
        private int x,y;
        private String name;
        //子类构造方法必须调用父类的构造方法,并且该调用必须放在第一句。
        public MyPoint() {
            super();//调用父类的方法
            this.x = 0;
            this.y = 0;
            this.name = "啦啦";
        }
        public MyPoint(int x, int y, String name) {
            super();
            this.x = x;
            this.y = y;
            this.name = name;
        }
        //geter_seter
        public int getX() {
            return x;
        }
        public void setX(int x) {
            this.x = x;
        }
        public int getY() {
            return y;
        }
        public void setY(int y) {
            this.y = y;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        /*定义成员方法move*/
        public void move(int x,int y) {
            this.x = x;
            this.y = y;
        }
        
        public MyPoint newPoint(MyPoint myPoint) {
            MyPoint mPoint = new MyPoint();
            mPoint.x = myPoint.x + 2;
            mPoint.y = myPoint.y + 6;
            mPoint.name = "lalalalaal";
            return mPoint;
        }
        
        public boolean myEqual(MyPoint myPoint) {
            if (this.x == myPoint.x && this.y ==myPoint.y)
                return true;
            else
                return false;
        }
        public static void main(String[] args) {
            MyPoint mPoint = new MyPoint(2, 1, "qweqw");
            MyPoint mPoint2 = new MyPoint();
            
            mPoint.move(3 , 3);
            System.out.println(mPoint.x);
            System.out.println(mPoint.y);
            /*输出两个点的横纵坐标是否相等*/
            System.out.println(mPoint.myEqual(mPoint2));
            
            mPoint2 = mPoint2.newPoint(mPoint);
            System.out.println(mPoint2.x);
            System.out.println(mPoint2.y);
        }
    
    }
当前页面是本站的「Baidu MIP」版。发表评论请点击:完整版 »