java程序创建手机类将手机类的属性品牌价格颜色定义为私有成员定义getter和setter方法访问已有的数据成员。 定义一个无参构造方法 定义有参构造方法来完成属性值的初始化构造方法参数列表中有三个参数其类型和成员变量类型相同对其三个属性进行初始化定义共有的print方法来显示其信息
public class Phone { private String brand; private double price; private String color;
public Phone() {}
public Phone(String brand, double price, String color) {
this.brand = brand;
this.price = price;
this.color = color;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public void print() {
System.out.println("Brand: " + brand);
System.out.println("Price: " + price);
System.out.println("Color: " + color);
}
}
原文地址: https://www.cveoy.top/t/topic/bTZL 著作权归作者所有。请勿转载和采集!