1.创建一个代表天气对象的实体类 a)拥有私有属性:日期,地点,⻛速,温度,湿度 b)拥有属性的get,set公有方法
时间: 2024-02-27 12:57:57 浏览: 105
Python 面向对象编程:类的创建与初始化、实例属性与方法、类属性与方法
可以使用以下代码创建一个代表天气对象的实体类:
```java
public class Weather {
private String date;
private String location;
private double windSpeed;
private double temperature;
private double humidity;
public Weather(String date, String location, double windSpeed, double temperature, double humidity) {
this.date = date;
this.location = location;
this.windSpeed = windSpeed;
this.temperature = temperature;
this.humidity = humidity;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public double getWindSpeed() {
return windSpeed;
}
public void setWindSpeed(double windSpeed) {
this.windSpeed = windSpeed;
}
public double getTemperature() {
return temperature;
}
public void setTemperature(double temperature) {
this.temperature = temperature;
}
public double getHumidity() {
return humidity;
}
public void setHumidity(double humidity) {
this.humidity = humidity;
}
}
```
这个类拥有私有属性date、location、windSpeed、temperature、humidity,以及对应的公有getter和setter方法。可以通过构造函数或者setter方法来设置这些属性的值。
阅读全文