Java实验报告

 

实验二 Java简单类与对象

一、 实验目的

(1) 掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值;

(2) 理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性;

(3) 理解static修饰付对类、类成员变量及类方法的影响。

二、 实验内容

  1. 写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色colorwidthheight都是double型的,而color则是String类型的。要求该类具有:

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

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

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

  1. 银行的账户记录Account有账户的唯一性标识(11个长度的字符和数字的组合),用户的姓名,开户日期,账户密码(六位的数字,可以用0开头),当前的余额。银行规定新开一个账户时,银行方面提供一个标识符、账户初始密码123456,客户提供姓名,开户时客户可以直接存入一笔初始账户金额,不提供时初始余额为0。定义该类,并要求该类提供如下方法:存款、取款、变更密码、可以分别查询账户的标识、姓名、开户日期、当前余额等信息。

 

三、 实验过程(请自己调整格式)

 

 1 class Main
 2 {
 3             private double width,heigh;
 4             private String color;
 5            
 6             public Rectangle(double width,double heigh,String color)
 7            {
 8                 this.setWidth(width);
 9                 this.setHeigh(heigh);
10                 this.setColor(color);
11            }
12             public double getWidth()
13             {
14                  return width;
15             }
16             public void setWidth(double width)
17             {
18                   this.width=width;
19             }
20              public double getHeigh()
21              {
22                   return heigh;
23              }
24              public void setHeigh(double heigh)
25              {
26                    this.heigh=heigh;
27              }
28              public String getColor()
29              {
30                     return color;
31              }
32              public void setColor(String color)
33              {
34                      this.color=color;
35              }
36              public double getArea()
37              {
38                       return this.width*this.heigh;
39              }
40              public double getLength()
41              {
42                       return 2*this.width+2*this.heigh;
43              }
44 }
45 public class Rectangle
46 {
47                   public static void main(String args[]) 
48                   {
49                              Rectangle i = null;
50                              i = new Rectangle(3,4,"red");
51                             System.out.println("矩形的颜色为:"+i.getColor());
52                             System.out.println("矩阵的周长为:"+i.getLength());
53                             System.out.println("矩阵的面积为:"+i.getArea());
54                    }
55 }

 

 

 

 

 

 第二题我尽力了,只能写到这了,真心不会了

 

 1 public class Account 
 2 {
 3      private String id;
 4      private String name;
 5      private Date date;
 6      private int password;
 7      private int money;
 8  
 9  public Account(String id, String name, int money)
10  {
11     super();
12     this.id = id;
13     this.name = name;
14     this.money = money;
15     this.date = new Date();
16     this.password = 123456;
17  }
18 
19   public String getId() 
20  {
21      return id;
22  }
23 
24    public void setId(String id) 
25   {
26       this.id = id;
27    }
28 
29    public String getName() 
30   {
31    return name;
32    }
33 
34    public void setName(String name) 
35   {
36        this.name = name;
37    }
38 
39     public void jin(int num)
40   {
41       this.money = this.money+num;
42    }
43 
44    public void chu(int num) 
45   {
46      this.money = this.money-num;
47   }
48  
49    public Date getDate() 
50  {
51     return date;
52   }
53 
54    public void setDate(Date date) 
55   {
56     this.date = date;
57   }
58 
59    public int getMoney() 
60   {
61      return money;
62   }
63 
64    public void setMoney(int money)
65   {
66        this.money = money;
67    }
68  
69    public int getPassword()
70   {
71        return password;
72   }
73 
74     public void setPassword(int password)
75    {
76       this.password = password;
77     }

 

 

 

 

 

四、 总结:

String 类两种实例方法区别:

 

(1)直接赋值:如String name = "lichen";

 

(2)利用new开辟一个新的空间:如String name = new String("lichen");
一个字符串就是一个String类的匿名对象。
匿名对象就是已经开辟了堆内存空间的并可以直接使用的对象
对象数组在使用前必须注意:
数组一定要先开辟空间;并且数组里面的每一个对象都是null值,所以在使用的时候数组中的每一个对象必须分别进行实例化。
包的概念:
包的概念,包是在使用多个类或接口时,为了避免名称重复而采用的一种措施。如果使用,直接在程序中加入package关键字即可。
import语句:
 
(1)定义,如果将几个类存放在不同的包中,则在使用类的时候就必须通过import语句导入。
(2)格式,import 包名称.子包名称.类名称;(手工导入所需要的类),import 包名称.子包名称.*;(由JVM自动加载所需要的类)。

 

posted on 2019-09-19 23:14  青春永驻岁月无忧  阅读(214)  评论(0编辑  收藏  举报