JAVA - Geter、Seter练习题

目录

题目

QQ图片20150415220117.png

解答

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

    }

标签: java

声明:本博客如无特殊说明皆为原创,转载请注明来源:JAVA - Geter、Seter练习题谢谢!

相关文章

发表评论:

icon_question.pngicon_razz.pngicon_sad.pngicon_evil.pngicon_exclaim.pngicon_smile.pngicon_redface.pngicon_biggrin.pngicon_surprised.pngicon_eek.pngicon_confused.pngicon_cool.pngicon_lol.pngicon_mad.pngicon_twisted.pngicon_rolleyes.pngicon_wink.pngicon_idea.pngicon_arrow.pngicon_neutral.pngicon_cry.pngicon_mrgreen.png