Java 手机类示例:封装属性、构造方法和 getter/setter
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/nhPb 著作权归作者所有。请勿转载和采集!