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

实验二 Java简单类与对象

一、实验目的

  • 掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值;
  • 理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性;
  • 理解static修饰付对类、类成员变量及类方法的影响。

二、实验内容

  • 1.写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:
  • (1) 使用构造函数完成各属性的初始赋值
  • (2) 使用get…()和set…()的形式完成属性的访问及修改
  • (3) 提供计算面积的getArea()方法和计算周长的getLength()方法

实验源码:

public class Main {
    public static void main(String[] args) {
        Rectangle re = new Rectangle();
        re.get();
        double i =re.getArea();
        System.out.println(i);
        double k =re.getLength();
        System.out.println(k);
    }
}

class Rectangle {
    double width;
    double heigh;
    String color;

    Rectangle() {
        this.width=3;
        this.heigh=4;
        this.color="Green";
    }

    public void get(){
        System.out.println("该矩形的宽是"+this.width);
        System.out.println("该矩形的高是"+this.heigh);
        System.out.println("该矩形的颜色是"+this.color);
    }

    public void setwidth(double single) {
        this.width=single;
    }

    public void setheigh(double single) {
        this.heigh=single;
    }

    public void setcolor(String single) {
        this.color=single;
    }

    public double getArea() {
        return this.width*this.heigh;
    }

    public double getLength(){
        return this.width*2+this.heigh*2;
    }
}

实验结果:

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

实验过程:

public class Main {
    public static void main(String[] args) {
        Account re = new Account("yanqing1234","YT",2019,9,17,"010614",999999.99);
        re.setAll();
        re.changebalance(0.01);
        re.setAll();
        re.changepsd("123456");
        re.setAll();
    }
}

class Account {
    String ID;            String psd;                int year;
    String name;          double balance;         int month;
                                                  int day;
    Account() {
        this.ID="yanqing1234";
        this.name="null";
        this.year=0;
        this.month=0;
        this.day=0;
        this.psd="null";
        this.balance=0;
    }
    Account(String ID, String name, int year, int month, int day, String psd, double balance) {
        this.ID       =   ID;
        this.name     =   name;
        this.year     =   year;
        this.month    =   month;
        this.day      =   day;
        this.psd      =   psd;
        this.balance  =   balance;
    }

    public void changebalance(double single) {
        this.balance += single;
    }

    public void changepsd(String single) {
        this.psd = single;
    }

    public void setAll() {
        System.out.println(this.ID);
        System.out.println(this.name);
        System.out.println(this.year+"."+this.month+"."+this.day);
        System.out.println("我太穷了:"+this.balance);
    }
}

实验结果:

个人总结:

1 String类

  • String类不可变,对String类的任何改变,都是返回一个新的String类对象。
  • 用“”或equals()方法来比较,“”比较双方的地址,equals()方法比较双方的内容。
  • 两种实例化方式:1.通过直接赋值 ; 2.使用标椎的new调用构造方法;

2 数组

声明数组
数据类型 数组名称[]=null;
分配空间
数组名称=new 数据类型[长度];
初始化
数据类型 数组名[]={初值0,初值1,……,初值n};
posted @ 2019-09-19 15:08  颜晴超甜♡  阅读(136)  评论(0编辑  收藏  举报