第四周课程总结&试验报告(二)

  • 实验目的
  • 掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值;
  • 理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性;
  • 理解static修饰付对类、类成员变量及类方法的影响。
  • 实验内容
  1. 写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:

(1) 使用构造函数完成各属性的初始赋值

(2) 使用get…()和set…()的形式完成属性的访问及修改

(3) 提供计算面积的getArea()方法和计算周长的getLength()方法

 实验代码:

 1 package jave;
 2 
 3 public class Jave {
 4     public static void main(String args[]) {
 5         Jave rec=new Jave();
 6         rec.setWidth(4);
 7         rec.setHeight(8);
 8         rec.setColor("绿色");
 9         rec.getArea();
10         rec.getLength();
11         System.out.println("长:"+rec.getWidth()+"\n高:"+rec.getHeight()+"\n颜色:"+rec.getColor());
12     }
13     
14     double width,height;
15     String color="red";        
16     public double getHeight() {
17         return height;                
18     }
19     public double getWidth() {
20         return width;
21     }
22     public String getColor() {
23         return color;
24     }
25     public void setHeight(double height) {
26         this.height = height;        
27     }
28     public void setWidth(double width) {
29         this.width = width;
30     }
31     public void setColor(String color) {
32         this.color = color;
33     }
34     
35     public void getArea() {
36         double area=0;
37         area=this.height*this.width;
38         System.out.println("面积为"+area);
39     }
40     public void getLength() {
41         double length=0;
42         length=(this.height+this.width)*2;
43         System.out.println("周长为"+length);
44     }
45 }

  实验过程:

总结:jave的话还是有很多不会,很多都是在请教黄诗荣的情况下才完成的。这周学习了string类的常用操作方法,split可以进行字符串的拆分,indexOf可以返回指定的字符串位置,不过第二题是真的不会,有望加强学习和实践。

posted @ 2019-09-20 16:37  时光碎片  阅读(180)  评论(0编辑  收藏  举报