gpt4 book ai didi

java - 更改器(mutator)在这里适用吗?

转载 作者:行者123 更新时间:2023-12-01 15:07:13 25 4
gpt4 key购买 nike

我有这个问题,这是给我的图表,我已经设法找到了解决方案,但实际问题本身要求我实现 get 和 set 方法,我不确定它们是否此处适用,

enter image description here

enter image description here

这只是有点长的问题的一部分,不想让它看起来像我希望用户通过粘贴整个问题来完成我的作业,但只是一个疑问,如果 get 和 set 方法确实可以已实现我该如何做到这一点?

这是我的代码,到目前为止我所得到的

public abstract class Property 
{
private static int autonumber = 0, propID,area,price;
private String address;

public Property(String addr, int area,int price)
{
autonumber++;
propID = autonumber;
this.area = area;
this.price = price;
}
public abstract double calculateMiscFee();

public int getArea()
{
return area;
}

public void setArea()
{

}
// public double calculateMiscFee()
// {
// return 100*
// }
}

这是我最新的代码

public abstract class Property 
{
private static int autonumber = 0;
private int propID;
private int area;
private double price;
private String address;
private int district;

public Property(String addr, int area,double price, int district)
{
autonumber++;
propID = autonumber;
this.area = area;
this.price = price;
this.district = district;
this.address = addr;

}
public abstract double calculateMiscFee();

public int getArea()
{
return area;
}

public void setArea(int a)
{
area = a;
}

public String getAddress()
{
return address;
}

public void setAddress(String add)
{
address = add;
}

public double getPrice()
{
return price;
}
public void setPrice(double p)
{
price = p;
}

public int getDistrict()
{
return district;
}

public void setDistrict(int disc)
{
district = disc;
}
// public double calculateMiscFee()
// {
// return 100*
// }

public String toString()
{

}

}

最佳答案

propID、面积和价格不应是静态静态变量在类的所有实例之间共享,这些变量应该是每个实例的,就像地址一样。

如果一个类声明了 String 类型的变量 abc,则 abc 的 getter 和 setter 将如下所示:

public String getAbc() {
return abc;
}

public void setAbc(String newAbc) {
/* Potentially do some validation of the new value
* For example make sure it's not null, or inside reasonable ranges
* (A price shouldn't be negative, etc.)
*/
abc = newAbc;
}

toString() 是所有类中都已存在的方法,但您仍然可以覆盖它。例如:

class MyClass {
private int myInt;

public MyClass(int myInt) {
this.myInt = myInt;
}

@Overrides
public String toString() {
return "MyClass with value " + myInt;
}
}

关于java - 更改器(mutator)在这里适用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12813690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com