构造方法:构造实例的方法
如果 init 方法能在对象创建的时候就自动被调用多方便
自动调用的唯一的问题:调用哪个方法呢?
看例程:学习构造方法 (constructor) 的语法
public class MerchandiseV2DescAppMain {
public static void main(String[] args) {
MerchandiseV2WithConstructor merchandise = new MerchandiseV2WithConstructor
("书桌", "DESK9527", 40, 999.9, 500);
// >> TODO 如果我们自己添加类构造方法,Java就不会再添加无参数的构造方法。
// >> TODO 这时候,就不能直接 new 一个对象不传递参数了(看例子)
// MerchandiseV2WithConstructor merchandise2 = new MerchandiseV2WithConstructor();
merchandise.describe();
}
}
public class RunLittleSupperMarketAppMain {
public static void main(String[] args) {
// 创建一个小超市类
LittleSuperMarket littleSuperMarket = new LittleSuperMarket(
"有家小超市", "浦东新区世纪大道666号",
100, 200, 200);
System.out.println("下面请利润最高的商品自我介绍:");
littleSuperMarket.getBiggestProfitMerchandise().describe();
}
}
public class LittleSuperMarket {
public String superMarketName;
public String address;
public int parkingCount;
public double incomingSum;
public MerchandiseV2WithConstructor[] merchandises;
public int[] merchandiseSold;
/**
* 初始化小超市
*
* @param superMarketName
* @param address
* @param parkingCount
* @param merchandiseCount 商品种类数
* @param count 每种商品缺省库存
*/
public LittleSuperMarket(String superMarketName, String address, int parkingCount,
int merchandiseCount, int count) {
this.superMarketName = superMarketName;
this.address = address;
this.parkingCount = parkingCount;
merchandises = new MerchandiseV2WithConstructor[merchandiseCount];
for (int i = 0; i < merchandises.length; i++) {
double purchasePrice = Math.random() * 200;
// 创建并给商品的属性赋值
MerchandiseV2WithConstructor m = new MerchandiseV2WithConstructor(
"商品" + i,
"ID" + i,
count,
purchasePrice * (1 + Math.random()),
purchasePrice
);
// 用创建的商品,给商品数组的第i个引用赋值,all和小超市的商品数组引用指向的是同一个数组对象
merchandises[i] = m;
}
merchandiseSold = new int[merchandises.length];
}
// 简单的访问成员变量
public String getSuperMarketName() {
return superMarketName;
}
public String getAddress() {
return address;
}
public int getParkingCount() {
return parkingCount;
}
public double getIncomingSum() {
return incomingSum;
}
public void setSuperMarketName(String superMarketName) {
this.superMarketName = superMarketName;
}
public void setAddress(String address) {
this.address = address;
}
public void setParkingCount(int parkingCount) {
this.parkingCount = parkingCount;
}
public void setIncomingSum(double incomingSum) {
this.incomingSum = incomingSum;
}
public void setMerchandises(MerchandiseV2WithConstructor[] merchandises) {
this.merchandises = merchandises;
}
public void setMerchandiseSold(int[] merchandiseSold) {
this.merchandiseSold = merchandiseSold;
}
// 一些特殊的逻辑
/**
* 得到利润最高的商品
*
* @return
*/
public MerchandiseV2WithConstructor getBiggestProfitMerchandise() {
MerchandiseV2WithConstructor curr = null;
for (int i = 0; i < merchandises.length; i++) {
MerchandiseV2WithConstructor m = merchandises[i];
// 这个逻辑有问题吗?相同的利润怎么判断?
if (curr == null || curr.calculateProfit() < m.calculateProfit()) {
curr = m;
}
}
return curr;
}
/**
* 根据索引获取商品
*
* @param merchandiseIndex
* @return
*/
public MerchandiseV2WithConstructor getMerchandiseOf(int merchandiseIndex) {
if (merchandiseIndex < 0 || merchandiseIndex >= merchandises.length) {
return null;
}
return merchandises[merchandiseIndex];
}
/**
* 赚钱
*
* @param toBeAdded
*/
public void addIncomingSum(double toBeAdded) {
this.incomingSum += toBeAdded;
}
/**
* 花钱
*
* @param toBeSpent
* @return
*/
public boolean spendMoney(double toBeSpent) {
if (toBeSpent > incomingSum) {
return false;
}
incomingSum -= toBeSpent;
return true;
}
}
public class MerchandiseV2 {
public String name;
public String id;
public int count;
public double soldPrice;
public double purchasePrice;
public void init(String name, String id, int count, double soldPrice, double purchasePrice) {
this.name = name;
this.id = id;
this.count = count;
this.soldPrice = soldPrice;
this.purchasePrice = purchasePrice;
}
public void describe() {
System.out.println("商品名字叫做" + name + ",id是" + id + "。 商品售价是" + soldPrice
+ "。商品进价是" + purchasePrice + "。商品库存量是" + count +
"。销售一个的毛利润是" + (soldPrice - purchasePrice));
}
public double calculateProfit() {
double profit = soldPrice - purchasePrice;
// if(profit <= 0){
// return 0;
// }
return profit;
}
public double buy(int count) {
if (this.count < count) {
return -1;
}
return this.count -= count;
}
}
public class MerchandiseV2WithConstructor {
public String name;
public String id;
public int count;
public double soldPrice;
public double purchasePrice;
// >> TODO 构造方法(constructor)的方法名必须与类名一样,而且构造方法没有返回值。这样的方法才是构造方法。
// >> TODO 构造方法可以有参数,规则和语法于普通方法一样。使用时,参数传递给 new 语句后类名的括号后面。
// >> TODO 如果没有显示的添加一个构造方法,Java会给每个类都会默认自带一个无参数的构造方法。
// TODO 如果我们自己添加类构造方法,Java就不会再添加无参数的构造方法。这时候,就不能直接 new 一个对象不传递参数了(看例子)
// >> TODO 所以我们一直都在使用构造方法,这也是为什么创建对象的时候类名后面要有一个括号的原因。
// >> TODO 构造方法无法被点操作符调用或者在普通方法里调用,只能通过 new 语句在创建对象的时候,间接调用。
// TODO 理解一下为什么构造方法不能有返回值,因为有返回值也没有意义,new 语句永远返回的是创建出来的对象的引用
public MerchandiseV2WithConstructor(String name, String id, int count,
double soldPrice, double purchasePrice) {
this.name = name;
this.id = id;
this.count = count;
this.soldPrice = soldPrice;
this.purchasePrice = purchasePrice;
}
public void describe() {
System.out.println("商品名字叫做" + name + ",id是" + id + "。 商品售价是" + soldPrice
+ "。商品进价是" + purchasePrice + "。商品库存量是" + count +
"。销售一个的毛利润是" + (soldPrice - purchasePrice));
}
public double calculateProfit() {
double profit = soldPrice - purchasePrice;
// if(profit <= 0){
// return 0;
// }
return profit;
}
public double buy(int count) {
if (this.count < count) {
return -1;
}
return this.count -= count;
}
}
如果我们自己添加类构造方法,Java就不会再添加无参数的构造方法。
这时候,就不能直接 new 一个对象不传递参数了
构造方法(constructor)的方法名必须与类名一样,而且构造方法没有返回值。这样的方法才是构方法。
构造方法可以有参数,规则和语法于普通方法一样。使用时,参数传递给 new 语句后类名的括号面。
如果没有显示的添加一个构造方法,Java会给每个类都会默认自带一个无参数的构造方法。
如果我们自己添加类构造方法,Java就不会再添加无参数的构造方法。这时候,就不能直接 new 一对象不传递参数了
所以我们一直都在使用构造方法,这也是为什么创建对象的时候类名后面要有一个括号的原因。
构造方法无法被点操作符调用或者在普通方法里调用,只能通过 new 语句在创建对象的时候,间接调用。
理解一下为什么构造方法不能有返回值,因为有返回值也没有意义,new 语句永远返回的是创建出来的对象的引用