zoukankan      html  css  js  c++  java
  • 第四周总结

    实验二 Java简单类与对象
    实验目的
    掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值;
    理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性;
    理解static修饰付对类、类成员变量及类方法的影响。
    1。写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:
    (1) 使用构造函数完成各属性的初始赋值

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

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

    一:实验代码
    class test {
    private double width;
    private double height;
    private String color;

    public test1() {
    }
    
    public test1(double width, double height, String color)
    {
        this.setWidth(width);
        this.setHeight(height);
        this.setColor(color);
    }
    
    public void setWidth(double w) {
        width = w;
    }
    
    public void setHeight(double h) {
        height = h;
    }
    
    public void setColor(String c) {
        color = c;
    }
    
    public double getWidth() {
        return width;
    }
    
    public double getHeight() {
        return height;
    }
    
    public String getColor() {
        return color;
    }
    
    public double getArea() {
        return width * height;
    }
    
    public double getLength() {
        return 2 * (width + height);
    }
    

    }

    public class first {
    public static void main(String[] args) {
    test t = null;
    t = new test(15, 18, "blue");
    System.out.println("周长为"+t.getLength());
    System.out.println("面积为"+t.getArea());
    }
    }
    结果

    2.银行账号
    import java.util.Scanner;
    import java.util.Date;

    class Account {
    private String id;
    private String name;
    private Date date;
    private int password;
    private int money;

    public Account(String id, String name, int money) {
        super();
        this.id = id;
        this.name = name;
        this.money = money;
        this.date = new Date();
        this.password = 123456;
    }
    

    public String getId() {
    return id;
    }
    public void setId(String id) {
    this.id = id;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public void put(int num) {
    this.money = this.money+num;
    }
    public void out(int num) {
    this.money = this.money-num;
    }
    public Date getDate() {
    return date;
    }
    public void setDate(Date date) {
    this.date = date;
    }
    public int getMoney() {
    return money;
    }
    public void setMoney(int money) {
    this.money = money;
    }
    public int getPassword() {
    return password;
    }
    public void setPassword(int password) {
    this.password = password;
    }
    public void Test3() {
    Scanner sc=new Scanner(System.in);
    int password=sc.nextInt();
    this.password = password;
    sc.close();
    }
    }

    总结
    不是很会,主要靠室友和同学的帮忙

  • 相关阅读:
    移动端 video播放器层级过高,遮挡住其他内容
    页面多个H5播放器video,如何点击当前视频其他视频暂停
    h5页面在移动端禁止缩放,双击放大,双指放大
    uni-app发h5本地浏览index.html空白的问题
    运行nvue 页面报错reportJSException >>>> exception function:GraphicActionAddElement, exception:You are trying to add a u-text to a u-text, which is illegal as u-text is not a container
    nvue页面注意事项
    移动端可能遇到的问题解决方案
    将博客搬至CSDN
    安装node后配置在全局配置
    swiper使用loop后,无法获取索引的问题
  • 原文地址:https://www.cnblogs.com/xiaomouren/p/11559993.html
Copyright © 2011-2022 走看看