The second test and study report summary

1. Write a class called Rectangle rectangle representing. Attributes which includes a wide width, height, and color of high color, width and height are double-type, and the color is of type String. Requirements class has:

(1) using the constructor completes the initial assignment of each attribute

(2) using the get ... () and set ... () in the form of complete access and modify the properties of

(3) calculate the area of ​​providing getArea () method and the calculated circumference getLength () method

Experiment code:

   package xingqisan;

   public class Rectangle {                    //定义Rectangle类
        private double width;                       
        private double height;
        private String color;                //定义属性
        public double getHeight() {
            return height;                  //取得height属性
        }
        public void setHeight(double height) {
            this.height = height;          //设置height属性下同
        }
        public double getWidth() {
            return width;
        }
        public void setWidth(double width) {
            this.width = width;
        }
        public String getColor() {
            return color;
        }
        public void setColor(String color) {
            this.color = color;
        }
        
        public void getArea() {
            double area=0;
            area=this.height*this.width;
            System.out.println("面积为"+area);
        }
        public void getLength() {
            double length=0;
            length=(this.height+this.width)*2;
            System.out.println("周长为"+length);
        }
        public static void main(String args[]) {
            Rectangle rec=new Rectangle();
            rec.setWidth(3);
            rec.setHeight(4);
            rec.setColor("黄色");
            rec.getArea();
            rec.getLength();
            System.out.println("长:"+rec.getWidth()+",高:"+rec.getHeight()+",颜色:"+rec.getColor());
        }
   }

Screenshot results:

Experimental encountered problems:

1. First, the code is too complicated, there should be a lot simpler code than this, but now can not achieve
2. not quite understand the application of this keyword, there is only according to the application on the book.
3. Output problem, location plus the comma and did not pay attention, it has been displayed error.
4. understand the problem and get on the set, and their understanding is not very good, you have to pay more to learn via video.
5. For the last to create a new class do not understand.

Guess you like

Origin www.cnblogs.com/fengmixinluo/p/11541228.html