zoukankan      html  css  js  c++  java
  • 第四周课程总结和实验报告2

    实验二 Java简单类与对象

    实验目的

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

    实验内容

    写一个名为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;
    public Rectangle(double width,double height,String color){
    this.setWidth(width);
    this.setHeight(height);
    this.setColor(color);
    }
    public void tell(){
    System.out.println("宽:"+width+" "+"高:"+height+" "+"颜色:"+color);
    }
    public double getWidth(){
    return width;
    }
    public double getHeight(){
    return height;
    }
    public String getColor(){
    return color;
    }
    public void setWidth(double width){
    this.width=width;
    }
    public void setHeight(double height){
    this.height=height;
    }
    public void setColor(String color) {
    this.color=color;
    }
    public void getArea(){
    System.out.println("面积为:"+(width*height));
    }
    public void getLength(){
    System.out.println("周长为:"+(width+height)*2);
    }

    }
    public class juxing{
    public static void main(String[] args){
    Rectangle per=new Rectangle(5,10,"红色");
    per.tell();
    per.getArea();
    per.getLength();
    }
    }

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

    代码

    import java.util.Date;

    import java.util.Scanner;

    class Account{

        private String id,name,password;

        private Date date;

        private double balance;

        public Account() {

    }

    public Account(String id,String name,double balance) {

        this.setId(id);

        this.setName(name);

        this.date=new Date();

        this.password="123456";

        this.setBalance(balance);

    }

    public String getId() {

        return id;

    }

    public String getName() {

        return name;

    }

    public Date getDate() {

        return date;

    }

    public String getPassword() {

        return password;

    }

    public double getBalance() {

        return balance;

    }

    public void setId(String i) {

        id=i;

    }

    public void setName(String n) {

        name=n;

    }

    public void setDate(Date d) {

        date=d;

    }

    public void setPassword(String p) {

        password=p;

    }

    public void setBalance(double b) {

        balance=b;

    }

    public void cunqian(double money) {

        this.balance=this.balance+money;

    }

    public void quqian(double money) {

        this.balance=this.balance-money;

    }

    public Date date() {

        return date;

    }

    public void changepassword() {

        Scanner sc=new Scanner(System.in);

        System.out.println("请输入新密码:");

        String password=sc.nextLine();

    }

    public static void main(String[] args) {

        Account a=new Account("20188383","李文杰",1000);

        a.cunqian(1000);

        a.quqian(500);

        a.changepassword();

        a.date();

        System.out.println("账户标识:"+a.getId());

        System.out.println("姓名:"+a.getName());

        System.out.println("开户日期:"+a.date());

        System.out.println("当前余额:"+a.getBalance());

    }

    }

    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()方法进行判断的。

     

    3.String类两种对象实例化方式的区别

    (1)直接赋值,对于这个代码String str1="lichen";实际上就是把一个在堆中开辟好的堆内存空间的使用权给了str1这个对象,使用这种方法还有另外一个好处,那就是,如果一个字符串已经被一个名称所引用,则以后再有相同的字符串声明的时候,不会再重新开辟空间,并且会自动保存在对象池之中以供下次重复使用。

    (2)使用new String()的方式实例化String对象,上面说过一个字符串就是一个String类的匿名对象,所以使用new关键词的话,不管如何都会再开辟一个新的空间,但空间的内容不变,所以实际上开辟了两个内存空间,但真正使用的只是一个使用关键字new开辟的空间,另一个是垃圾空间。

     

    4.String类的使用特点

     

    字符串的内容一旦声明则不可改变。

     

    包的概念

    包是使用多个类或接口时,为了避免名称重复而采用的一种措施。

    包的主要目的时实现程序的分割保存,这样既便于开发且方便维护。

    import java.util.Date;import java.util.Scanner;class Account{    private String id,name,password;    private Date date;    private double balance;    public Account() {}public Account(String id,String name,double balance) {    this.setId(id);    this.setName(name);    this.date=new Date();    this.password="123456";    this.setBalance(balance);}public String getId() {    return id;}public String getName() {    return name;}public Date getDate() {    return date;}public String getPassword() {    return password;}public double getBalance() {    return balance;}public void setId(String i) {    id=i;}public void setName(String n) {    name=n;}public void setDate(Date d) {    date=d;}public void setPassword(String p) {    password=p;}public void setBalance(double b) {    balance=b;}public void cunqian(double money) {    this.balance=this.balance+money;}public void quqian(double money) {    this.balance=this.balance-money;}public Date date() {    return date;}public void changepassword() {    Scanner sc=new Scanner(System.in);    System.out.println("请输入新密码:");    String password=sc.nextLine();}public static void main(String[] args) {    Account a=new Account("010114","唐嘉诚",1000);    a.cunqian(1000);    a.quqian(500);    a.changepassword();    a.date();    System.out.println("账户标识:"+a.getId());    System.out.println("姓名:"+a.getName());    System.out.println("开户日期:"+a.date());    System.out.println("当前余额:"+a.getBalance());}}

  • 相关阅读:
    MySql cmd下的学习笔记 —— 引擎和事务(engine,transaction)
    MySql cmd下的学习笔记 —— 有关视图的操作(algorithm)
    MySql cmd下的学习笔记 —— 有关视图的操作(建立表)
    MySql cmd下的学习笔记 —— 有关常用函数的介绍(数学函数,聚合函数等等)
    MySql cmd下的学习笔记 —— 有关多表查询的操作(多表查询练习题及union操作)
    MySql 在cmd下的学习笔记 —— 有关多表查询的操作(内连接,外连接,交叉连接)
    MySql cmd下的学习笔记 —— 有关子查询的操作(where型,from型,exists型子查询)
    MySql cmd下的学习笔记 —— 有关select的操作(order by,limit)
    剑指Offer--第21题 调整数组顺序使奇数位于偶数前面;
    剑指Offer--和为s的连续正数序列
  • 原文地址:https://www.cnblogs.com/slob/p/11560049.html
Copyright © 2011-2022 走看看