实验二 Java简单类与对象

实验目的

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

实验内容

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

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

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

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

1.代码

package ectangle;

 public class Rectangle
{
            private double heigh;
            private double width;
            private String color;
           
            public Rectangle(double width,double heigh,String color)   //使用构造函数
           {
               this.setWidth(width);
                this.setHeigh(heigh);
                this.setColor(color);
           }
            public void setWidth(double width)
            {
                  this.width=width;
            }
            public double getWidth()
            {
                 return width;
            }
             public void setHeigh(double heigh)
             {
                   this.heigh=heigh;
             }
             public double getHeigh()
             {
                  return heigh;
             }
             public void setColor(String color)
             {
                     this.color=color;
             }
             public String getColor()
             {
                    return color;
             }
             public double getArea()  //计算面积
             {
                      return this.width*this.heigh;
             }
             public double getLength()  //计算周长
             {
                  return 2*this.width+2*this.heigh;
             }
   
   
          public static void main(String args[]) 
          {
               Rectangle item = new Rectangle(5,6,"red");
               System.out.println(item.getColor());
               System.out.println(item.getLength());
               System.out.println(item.getArea());
           }
}
2.截图

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

还没有完全做完

1.代码
package test;
import java.util.Date;
import java.util.Scanner;
public class test 
{
    private String id;
    private String name;
    private double days;
    private String password;
    private int money;
    private int amount;
    
    public test(String id,String name,int money)
    {
        this.setID(id);
        this.setName(name);
        this.setMoney(money);
        this.setPassword="123456";
        this.setDays = new Date();
    }
    public int deposit()
    {
        return money+amount;
    }
    public int withdraw()
    {
        return amount-money;
    }
    public void setDays(int s)
    {
    	days = s;
    }
    public int getDays()
    {
        return days;
    }
    public void changepassword()
    {
      Scanner x = new Scanner(System.in);
      String password = x.nextString();
      this.password = new password;
    }
    public static void main(String [] args)
    {
        test item = new test("id","name","createtime");
        System.out.println(item.getID());
        System.out.println(item.getName());
        System.out.println(item.getDays());
        System.out.println(item.getAmont());
    }


}
2.截图

3.课程总结:

1.知识点概括

(1). String类的七大基本操作方法

(2). static类

一旦编写了至少一一个构造方法,那么编译器将不再赠送。
'static'
如果一个成员变量使用了static关键字,那么这个变量不再属于对象自己,而是属于所在的类。多个对象共享同一份数据。
一旦使用static修饰成员方法, 那么这就成为了静态方法。静态方法不属于对象,而属于类的。如果没有static关键字,那么必须首先创建对象,然后通过对象才能使用它。如果有了static关键字,那么不需要创建对象,直接就能通过类名称来使用它。无论是成员变量,还是成员方法。如果有了static, 都推荐使用类名称进行调用。
静态变量:类名称.静态变量
静态方法:类名称.静态方法()
静态方法当中不能使用this

(3). Math类

(4). 构造方法,构造函数,及private,this的封装

public int setAge(){
    this.age = age;
}

注意事项:

1.构造方法的名称必须和所在的类名称完全样,就连大小写也要一-样

2.构造方法不要写返回值类型,连void都不写

3.构造方法不能return一个具体的返回值

4.如果没有编写任何构造方法,那么编译器将会默认赠送一个构造方法, 没有参数、方法体什么事情都不做。

public Student() {}

5.如果成员变量没有进行赋值,那么将会有一一个默认值, 规则和数组-样。

构造方法是专门用来创建对象的方法,当我们通过关键字new来创建对象时,其实就是在调用构造方法。

格式:
public 类名称(参数类型参数名称) {
方法体

}
 posted on 2019-09-20 12:22  怪她  阅读(209)  评论(0编辑  收藏  举报