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

第四周课程总结
学习总结:
对于缺了一个半月课的我,我瞬间感觉,我太难了,我太难了,难上加难,我好难。当初为了给学院争光,毅然决然的选择去带军训,现在。。。。。。。。
少说题外话,我们谈正事

本周学习了String、对象数组和包。

一:对象数组

对象数组,包含了一组相关对象,对象数组的使用时一定要记住:数组一定要先开辟空间,但是因为其是引用数组类型,所以数组里面的每一个对象都是null值,则在使用的时候数组中的每一个对象必须进行实例化操作!
包,其实就是在创建文件夹,使得代码在同一个文件夹里,便于查找。
1.对象数组的定义,所谓的对象数组,就是指包含了一组相关的对象,而且数组里面的每一个对象都是null值,所以在使用时数组中的每一个对象都必须分别进行实例化操作。
2.对象数组的声明,类 对象数组名称[ ] = new 类[数组长度];。
3.对象数组的初始化,与数组初始化的方式一样,对象数组也分为静态初始化和动态初始化。
二:String类
1.String类的两种实例化方法
(1)直接赋值:如String name = "lichen";。
(2)利用new开辟一个新的空间(实际上就是直接调用String类中的构造方法):如String name = new String("lichen");。
2.String类的比较方法
(1)通过使用“”进行内容的比较,但“”并不能适用于字符串内容的比较,因为字符串的内容看似相等,但其实地址不同,而“”主要的功能是进行数值的相等判断的,如果用在了String对象上表示的是内存地址数值的比较,所以“”就不能用于比较字符串内容啦。
(2)使用equals()方法对String的内容进行比较,equals()方法是String提供的一个方法,此方法专门负责进行字符串内容的比较,所以字符串内容比较是用equals()方法进行判断的。

三:包的概念
1.包的概念,包是在使用多个类或接口时,为了避免名称重复而采用的一种措施。如果使用,直接在程序中加入package关键字即可。
2.包的定义,package 包名称.字包名称;。
3.import语句
(1)定义,如果将几个类存放在不同的包中,则在使用类的时候就必须通过import语句导入。
(2)格式,import 包名称.子包名称.类名称;(手工导入所需要的类),import 包名称.子包名称.*;(由JVM自动加载所需要的类)。
4.系统常见包
下面简单介绍几个
(1)java.lang,此包为基本的包,像String这样的类就都保存在此包之中。
(2)java.util,此包为工具包,一些常用的类库、日期操作等都在此包之中。
(3)java.io,输入、输出处理。
试验报告(二)
实验二 Java简单类与对象

  • 实验目的
  • 掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值;
  • 理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性;
  • 理解static修饰付对类、类成员变量及类方法的影响。
  • 实验内容
    1.写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:
    (1) 使用构造函数完成各属性的初始赋值。
    (2) 使用get…()和set…()的形式完成属性的访问及修改。
    (3) 提供计算面积的getArea()方法和计算周长的getLength()方法。
    源代码
    public class Rectangle {
    private double height;
    private double width;
    private String color;
    public double getHeight() {
    return height;
    }
    public void setHeight(double height) {
    this.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 Rectangle(double width,double height,String color){
    this.setColor(color);
    this.setHeight(height);
    this.setWidth(width);
    }
    public void getArea(){
    double area=0;
    area=this.height*this.width;
    System.out.println("矩形的面积为"+area);
    }
    public String toString(){
    String recStr="矩形的高度:"+this.getHeight()+"宽度:"+this.getWidth()
    +"颜色:"+this.getColor();
    return recStr;
    }
    /**
  • 测试函数
  • @param args
    */
    public static void main(String[] args) {
    Rectangle rec=new Rectangle(3, 4, "红色");
    rec.getArea();
    System.out.println(rec.toString());
    }
    }

运行结果:

import java.util.Date;
import java.util.Scanner;
public class Bank {
private String id;
private String name;
private Date createTime;
private String mima;
private int balance;
public amount(String id,String name, int balance){
super();
this.id=id;
this.name=name;
this.balance=balance;
this.createTime=new Date();
this.mima="123456";
}
public void deposit(int amount){
this.balance+=amount;
}
public void withdraw(int amount){
this.balance-=amount;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime( Date createTime){
this.createTime=createTime;
}
public void changemima(){
Scanner sc=new Scanner(System.in);
System.out.println("请输入新的密码:");
String mima=sc.nextString();
this.mima=new mima;
}
public static void main(String[] args){
Bank a=new Bank("1234567890","luxi",1000);
a.deposit(100);
a.withdraw(150);
a.changemima();
a.createTime();
System.out.println("账户账号:"+a.getId());
System.out.printf("账户姓名: "+a.getName());
System.out.println("账户日期: "+a.getCreateTime());
System.out.println("账户余额: "+a.getAmount());
}
}

原文地址:https://www.cnblogs.com/freezinng/p/11558633.html