java中关于定义变量的一些问题解决办法

不少同学看到一些定义变量,就觉得比较难了。例如下面这个,如何解决问题呢?


下面给出的方法:

1、成员变量和局部变量  一个在方法外部类内部,一个在方法内部

2、
public class MP3 {
public String brand;
public double weight;
public String type;
public double price;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "MP3 [brand=" + brand + ", weight=" + weight + ", type=" + type
+ ", price=" + price + "]";
}
 
public static void main(String[] args) {
MP3 mp3 =new MP3();
mp3.setBrand("爱国者F928");
mp3.setWeight(12.4);
mp3.setType("内置锂电池");
mp3.setPrice(499);
System.out.println(mp3.toString());
}
 
}

猜你喜欢

转载自blog.csdn.net/u012187684/article/details/79622941