第四周Java实验总结
2020-12-13 14:25
标签:函数 信息 客户 声明 实验目的 感想 计算机科学 查询 bsp Java实验报告 班级:计算机科学与技术2班 学号:20188430 姓名:詹洋 完成时间:4个小时 实验二 Java简单类与对象 (1) 使用构造函数完成各属性的初始赋值 (2) 使用get…()和set…()的形式完成属性的访问及修改 (3) 提供计算面积的getArea()方法和计算周长的getLength()方法 实验过程: 写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。 实验源码: 实验感想: 通过这次实验,了解到了Java的一些更精深的操作,类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值;使我有了更高的进步。 第四周Java实验总结 标签:函数 信息 客户 声明 实验目的 感想 计算机科学 查询 bsp 原文地址:https://www.cnblogs.com/lll0719/p/11559997.html
public class Rectangle {
private double Width;
private double Height;
private String color = "blue";
public double getWidth() {
return Width;
}
public void setWidth(double width) {
this.Width = width;
}
public double getHeight() {
return Height;
}
public void setHeight(double height) {
this.Height = height;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getArea() {
return Height*Width;
}
public double getLength() {
return (Width+Height)*2;
}
public static void main(String args[]) {
Rectangle rec = new Rectangle();
rec.getArea();
rec.getLength();
rec.getHeight();
rec.getWidth();
rec.getColor();
System.out.println("长"+rec.getHeight());
System.out.println("宽"+rec.getWidth());
System.out.println("颜色"+rec.getColor());
System.out.println("面积为:"+r.getArea());
System.out.println("周长为:"+r.getLength());
}
}运行结果:
上一篇:C# tostring()汇总