第四周

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

 

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

要求该类具有:

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

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

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

 

实验代码

复制代码
class Rectangle             //定义矩形类名
{
    private double width;     //矩形宽度
    private double height;    //矩形高度
    private String color;     //矩形颜色
    
class Rectangle             //定义矩形类名
{
    private double width;     //矩形宽度
    private double height;    //矩形高度
    private String color;     //矩形颜色
    
    public Rectangle()     //定义无参构造
    {     
    }
//定义有三个参数的构造方法,为类中的属性初始化
public Rectangle(double width,double height,String color)
{
    this.setWidth(width);    //设置宽度
    this.setHeight(height);  //设置高度
    this.setColor(color);    //设置颜色
}
   public void setWidth(double s)  //设置宽度
   {
       width = s;       
   }
   public void setheight(double n)   //设置长度
   {
       height = n;
   }
   public void setcolor(String y)   //设置颜色
   {
       color = y;
   }
   public double getWidth()    //取得编号
   {
       return width;
   }
   public double getHeight()   //取得高度
   {
       return height;
   }
   public String getColor()    //取得颜色
   {
       return color;
   }
   public double getArea()    //计算面积
   {
       return  height*width;
   }
   public double getLength()   //计算周长
   {
       return (height+width)*2;
   }
}
复制代码

 

 

 

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

 

实验代码

复制代码
import java.util.Date;
import java.util Scanner;
public class bank
{
    private String id;
    private String name;
    private double createtime;
    private String password;
    private int money;
    
    public amount(String id,String name,int money)
    {
        this.setID(id);
        this.setName(name);
        this.setMoney(money);
        this.setPassword="123456";
        this.setCreatetime = new Date();
    }
    public int deposit()
    {
        return money+amount;
    }
    public int withdraw()
    {
        return amount-money;
    }
    public void setCreatetime(int s)
    {
        createtime = s;
    }
    public int getcreatetime()
    {
        return createtime;
    }
    public void changepassword()
    {
      Scanner x = new Scanner(System.in);
      System.out.println("输入新的密码:");
      String password = x.nextString();
      this.password = new password;
    }
    public static void main(String [] args)
    {
        Bank n = new bank("id","name","createtime");
        System.out.println("账号:"+n.getID());
        System.out.println("姓名:"+n.getName());
        System.out.println("开户日期:"+n.getCreatetime());
        System.out.println("余额:"+n.getAmont());
    }
}
复制代码

 

注:以上代码,仅代表个人思路。

                     

实验总结:

一.当我们创建一个类后,这一类中就会有多个对象,对象在可以被使用之前必须要被正确地实例化。

实例化作用:

1.检查类是否加载

2.分配父类型空间, 按照属性分配空间, 属性自动初始化为"零"

这两个作用是我所知道的,当然我说的可能不全面,希望以后能补充完整

 

二.为什么私有属性要使用set跟get方法设置和访问?

将数据与行为进行分离,体现了对象的封装性,封装就是将属性私有化,提供公有的方法访问私有的属性。我个人理解就是每个人都有特定的名字跟特定的性格,不能随意修改,只有通过特定的方法才能修改,所以我们将成员变量声明为private,再通过 public的方法来对这个变量进行访问

 

三.

我放过一个经常的错误就是,前面定义类属性的类型的时候一定要保证后面调用这个类的属性,类型一定要保证跟前面保证一致

 

posted @ 2019-09-18 22:35  宋逸豪  阅读(207)  评论(1编辑  收藏  举报