2.18.理解方法的调用:代码的一种特 殊跳转
作者:互联网
理解方法的调用:代码的一种特殊跳转
看例程:模拟this自引用传递
看例程:对象可以在自己的方法里调用自己类的别的的方法,甚至可以调用自己的方法。
方法在类中定义的先后,没有任何影响。
方法是一种根据名字做跳转的代码流程控制方式。它的特殊之处在于有this自引用,有参数,
有返回值。
public class RunLittleSupperMarketAppMain {
public static void main(String[] args) {
// 创建一个小超市类
LittleSuperMarket littleSuperMarket = new LittleSuperMarket();
// 依次给超市的名字,地址,停车位赋值
littleSuperMarket.superMarketName = "有家小超市";
littleSuperMarket.address = "浦东新区世纪大道666号";
littleSuperMarket.parkingCount = 100;
// 给超市200种商品
littleSuperMarket.merchandises = new MerchandiseV2[200];
// 统计用的数组
littleSuperMarket.merchandiseSold = new int[littleSuperMarket.merchandises.length];
// 为了使用方便,创建一个商品数组引用,和littleSuperMarket.merchandises指向同一个数组对象
MerchandiseV2[] all = littleSuperMarket.merchandises;
// 遍历并给200种商品赋值
for (int i = 0; i < all.length; i++) {
// 创建并给商品的属性赋值
MerchandiseV2 m = new MerchandiseV2();
m.name = "商品" + i;
m.count = 200;
m.purchasePrice = Math.random() * 200;
m.soldPrice = m.purchasePrice * (1 + Math.random());
m.id = "ID" + i;
// 用创建的商品,给商品数组的第i个引用赋值,all和小超市的商品数组引用指向的是同一个数组对象
all[i] = m;
}
System.out.println("下面请利润最高的商品自我介绍:");
MerchandiseV2 m = littleSuperMarket.getBiggestProfitMerchandise();
m.describe();
System.out.println("调用makeEnoughFor");
m.makeEnoughFor(900);
m.describe();
}
}
public class RunLittleSupperMarketAppMainOneByOne {
public static void main(String[] args) {
// 创建一个小超市类
LittleSuperMarket littleSuperMarket = new LittleSuperMarket();
// 依次给超市的名字,地址,停车位赋值
littleSuperMarket.superMarketName = "有家小超市";
littleSuperMarket.address = "浦东新区世纪大道666号";
littleSuperMarket.parkingCount = 100;
// 给超市200种商品
littleSuperMarket.merchandises = new MerchandiseV2[200];
// 统计用的数组
littleSuperMarket.merchandiseSold = new int[littleSuperMarket.merchandises.length];
// 为了使用方便,创建一个商品数组引用,和littleSuperMarket.merchandises指向同一个数组对象
MerchandiseV2[] all = littleSuperMarket.merchandises;
// 遍历并给200种商品赋值
for (int i = 0; i < all.length; i++) {
// 创建并给商品的属性赋值
MerchandiseV2 m = new MerchandiseV2();
m.name = "商品" + i;
m.count = 200;
m.purchasePrice = Math.random() * 200;
m.soldPrice = m.purchasePrice * (1 + Math.random());
m.id = "ID" + i;
// 用创建的商品,给商品数组的第i个引用赋值,all和小超市的商品数组引用指向的是同一个数组对象
all[i] = m;
}
System.out.println("下面请利润最高的商品自我介绍:");
MerchandiseV2 m = littleSuperMarket.getBiggestProfitMerchandise();
m.describe();
System.out.println("调用makeEnoughForOneByOne");
// m.makeEnoughFor(900);
m.makeEnoughForOneByOne(900);
m.describe();
}
}
public class LittleSuperMarket {
public String superMarketName;
public String address;
public int parkingCount;
public double incomingSum;
public MerchandiseV2[] merchandises;
public int[] merchandiseSold;
public MerchandiseV2 getBiggestProfitMerchandise() {
System.out.println("LittleSuperMarket的getBiggestProfitMerchandise方法使用的对象是:" + this);
MerchandiseV2 curr = null;
for (int i = 0; i < merchandises.length; i++) {
MerchandiseV2 m = merchandises[i];
if (curr == null || curr.calculateProfit() < m.calculateProfit()) {
curr = m;
}
}
return curr;
}
}
public class MerchandiseV2 {
public String name;
public String id;
public int count;
public double soldPrice;
public double purchasePrice;
public void describe() {
System.out.println("商品名字叫做" + name + ",id是" + id + "。 商品售价是" + soldPrice
+ "。商品进价是" + purchasePrice + "。商品库存量是" + count +
"。销售一个的毛利润是" + (soldPrice - purchasePrice));
}
public double calculateProfit() {
double profit = soldPrice - purchasePrice;
return profit;
}
public int getCount() {
return count;
}
public void addCount(int count) {
this.count += count;
}
public boolean hasEnoughCountFor(int count) {
return this.count >= count;
}
public void makeEnoughFor(int count) {
boolean hasEnough = hasEnoughCountFor(count);
if (!hasEnough) {
int toBeAdd = count - this.count;
addCount(toBeAdd);
}
}
public void makeEnoughForOneByOne(int count) {
boolean hasEnough = this.hasEnoughCountFor(count);
if (!hasEnough) {
this.addCount(1);
makeEnoughForOneByOne(count);
}
}
}
public class LittleSuperMarketWithMerchandiseMock {
public String superMarketName;
public String address;
public int parkingCount;
public double incomingSum;
public MerchandiseV2MockThis[] merchandises;
public int[] merchandiseSold;
public MerchandiseV2MockThis getBiggestProfitMerchandise(LittleSuperMarketWithMerchandiseMock This) {
System.out.println("LittleSuperMarket的getBiggestProfitMerchandise方法使用的对象是:" + This);
MerchandiseV2MockThis curr = null;
for (int i = 0; i < This.merchandises.length; i++) {
MerchandiseV2MockThis m = This.merchandises[i];
if (curr == null || curr.calculateProfit() < m.calculateProfit()) {
curr = m;
}
}
return curr;
}
}
public class MerchandiseV2MockThis {
public String name;
public String id;
public int count;
public double soldPrice;
public double purchasePrice;
public void describe(MerchandiseV2MockThis This) {
System.out.println("商品名字叫做" + This.name + ",id是" + This.id + "。 商品售价是" + This.soldPrice
+ "。商品进价是" + This.purchasePrice + "。商品库存量是" + This.count +
"。销售一个的毛利润是" + (This.soldPrice - This.purchasePrice));
}
public double calculateProfit() {
double profit = soldPrice - purchasePrice;
return profit;
}
public void duplicateName(int count) {
}
public int getCount(MerchandiseV2MockThis This) {
return This.count;
}
public void addCount(MerchandiseV2MockThis This, int count) {
This.count += count;
}
public boolean hasEnoughCountFor(MerchandiseV2MockThis This, int count) {
return This.count >= count;
}
public void makeEnoughFor(MerchandiseV2MockThis This, int count) {
boolean hasEnough = This.hasEnoughCountFor(This, count);
if (!hasEnough) {
int toBeAdd = count - This.count;
This.addCount(This, toBeAdd);
}
}
}
public class RunLittleSupperMarketWithMockThisMerchandiseAppMain {
public static void main(String[] args) {
// 创建一个小超市类
LittleSuperMarketWithMerchandiseMock littleSuperMarket = new LittleSuperMarketWithMerchandiseMock();
// 依次给超市的名字,地址,停车位赋值
littleSuperMarket.superMarketName = "有家小超市";
littleSuperMarket.address = "浦东新区世纪大道666号";
littleSuperMarket.parkingCount = 100;
// 给超市200种商品
littleSuperMarket.merchandises = new MerchandiseV2MockThis[200];
// 统计用的数组
littleSuperMarket.merchandiseSold = new int[littleSuperMarket.merchandises.length];
// 为了使用方便,创建一个商品数组引用,和littleSuperMarket.merchandises指向同一个数组对象
MerchandiseV2MockThis[] all = littleSuperMarket.merchandises;
// 遍历并给200种商品赋值
for (int i = 0; i < all.length; i++) {
// 创建并给商品的属性赋值
MerchandiseV2MockThis m = new MerchandiseV2MockThis();
m.name = "商品" + i;
m.count = 200;
m.purchasePrice = Math.random() * 200;
m.soldPrice = m.purchasePrice * (1 + Math.random());
m.id = "ID" + i;
// 用创建的商品,给商品数组的第i个引用赋值,all和小超市的商品数组引用指向的是同一个数组对象
all[i] = m;
}
System.out.println("下面请利润最高的商品自我介绍:");
MerchandiseV2MockThis m = littleSuperMarket.getBiggestProfitMerchandise(littleSuperMarket);
m.describe(m);
System.out.println("调用makeEnoughFor");
m.makeEnoughFor(m, 900);
m.describe(m);
}
}
标签:count,merchandises,调用,int,跳转,商品,2.18,littleSuperMarket,public 来源: https://blog.csdn.net/weixin_45471415/article/details/104795614