父类和子类的引用赋值关系
看例程:学习子类引用和父类引用的关系
父类引用可以指向子类对象,子类引用不可以指向父类的对象
可以进行强制类型转换,如果类型不对,会出错
可以调用的方法,是受引用类型决定的
public class LittleSuperMarketAppMain {
public static void main(String[] args) {
LittleSuperMarket superMarket = new LittleSuperMarket("大卖场",
"世纪大道1号", 500, 600, 100);
MerchandiseV2 m0 = superMarket.getMerchandiseOf(0);
m0.describe();
System.out.println();
MerchandiseV2 m100 = superMarket.getMerchandiseOf(100);
m100.describe();
System.out.println();
MerchandiseV2 m10 = superMarket.getMerchandiseOf(10);
m10.describe();
}
}
public class ReferenceAssign {
public static void main(String[] args) {
Phone ph = new Phone(
"手机001", "Phone001", 100, 1999, 999,
4.5, 3.5, 4, 128, "索尼", "安卓"
);
// >> TODO 可以用子类的引用给父类的引用赋值,也就是说,父类的引用可以指向子类的对象
MerchandiseV2 m = ph;
MerchandiseV2 m2 = new Phone(
"手机002", "Phone002", 100, 1999, 999,
4.5, 3.5, 4, 128, "索尼", "安卓"
);
// >> TODO 但是反之则不行,不能让子类的引用指向父类的对象。因为父类并没有子类的属性和方法呀
// Phone notDoable = new MerchandiseV2();
// >> TODO 重点
// >> TODO 因为子类继承了父类的方法和属性,所以父类的对象能做到的,子类的对象肯定能做到
// TODO 换句话说,我们可以在子类的对象上,执行父类的方法
// >> TODO 当父类的引用指向子类的实例(或者父类的实例),只能通过父类的引用,像父类一样操作子类的对象
// TODO 也就是说"名"的类型,决定了能执行哪些操作
// >> TODO ph和m都指向同一个对象,通过ph可以调用getBrand方法
// TODO 因为ph的类型是Phone,Phone里定义了getBrand方法
ph.getBrand();
// >> TODO ph和m都指向同一个对象,但是通过m就不可以调用getBrand方法
// TODO 因为m的类型是MerchandiseV2,MerchandiseV2里没有你定义getBrand方法
// m.getBrand();
// TODO 如果确定一个父类的引用指向的对象,实际上就是一个子类的对象(或者子类的子类的对象),可以强制类型转换
Phone aPhone = (Phone) m2;
// MerchandiseV2是Phone的父类,Phone是shellColorChangePhone的父类
ShellColorChangePhone shellColorChangePhone = new ShellColorChangePhone(
"手机002", "Phone002", 100, 1999, 999,
4.5, 3.5, 4, 128, "索尼", "安卓"
);
// TODO 父类的引用,可以指向子类的对象,即可以用子类(以及子类的子类)的引用给父类的引用赋值
MerchandiseV2 ccm = shellColorChangePhone;
// TODO 父类的引用,可以指向子类的对象。
// TODO 确定MerchandiseV2的引用ccm是指向的是Phone或者Phone的子类对象,那么可以强制类型转换
Phone ccp = (Phone) ccm;
// TODO 确定MerchandiseV2的引用ccm是指向的是ShellColorChangePhone或者ShellColorChangePhone的子类对象
// TODO 那么可以强制类型转换
ShellColorChangePhone scp = (ShellColorChangePhone) ccm;
// TODO 会出错,因为m2指向的是一个Phone类型的对象,不是ShellColorChangePhone的对象
ShellColorChangePhone notCCP = (ShellColorChangePhone) m2;
}
}
public class LittleSuperMarket {
public String superMarketName;
public String address;
public int parkingCount;
public double incomingSum;
public MerchandiseV2[] 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 MerchandiseV2[merchandiseCount];
for (int i = 0; i < merchandises.length; i++) {
// 创建手机,手机壳变色手机,和普通商品,都放在商品数组里
MerchandiseV2 m = null;
if (i > 0 & i % 100 == 0) {
m = new ShellColorChangePhone(
"商品" + i,
"ID" + i,
count,
1999,
999,
4.5,
3.5,
4,
128,
"三星",
"Android"
);
} else if (i > 0 & i % 10 == 0) {
m = new Phone(
"商品" + i,
"ID" + i,
count,
1999,
999,
4.5,
3.5,
4,
128,
"索尼",
"Android"
);
} else {
double purchasePrice = Math.random() * 200;
m = new MerchandiseV2(
"商品" + 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(MerchandiseV2[] merchandises) {
this.merchandises = merchandises;
}
public void setMerchandiseSold(int[] merchandiseSold) {
this.merchandiseSold = merchandiseSold;
}
// 一些特殊的逻辑
/**
* 得到利润最高的商品
*
* @return
*/
public MerchandiseV2 getBiggestProfitMerchandise() {
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;
}
/**
* 根据索引获取商品
*
* @param merchandiseIndex
* @return
*/
public MerchandiseV2 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 MerchandiseV2(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 MerchandiseV2(String name, String id, int count, double soldPrice) {
// double purPrice = soldPrice * 0.8;
// this(name, id, count, soldPrice, purchasePrice);
this(name, id, count, soldPrice, soldPrice * 0.8);
// double purPrice = soldPrice * 0.8;
}
public MerchandiseV2() {
this("无名", "000", 0, 1, 1.1);
}
public void describe() {
System.out.println("商品名字叫做" + name + ",id是" + id + "。 商品售价是" + soldPrice
+ "。商品进价是" + purchasePrice + "。商品库存量是" + count +
"。销售一个的毛利润是" + calculateProfit());
}
public double calculateProfit() {
double profit = soldPrice - purchasePrice;
// if(profit <= 0){
// return 0;
// }
return profit;
}
public double buy(int count) {
if (this.count < count) {
System.out.println("购买失败,库存不够");
return -1;
}
this.count -= count;
double cost = count * soldPrice;
System.out.println("购买成功,花费为" + cost);
return cost;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public double getSoldPrice() {
return soldPrice;
}
public void setSoldPrice(double soldPrice) {
this.soldPrice = soldPrice;
}
public double getPurchasePrice() {
return purchasePrice;
}
public void setPurchasePrice(double purchasePrice) {
this.purchasePrice = purchasePrice;
}
}
public class Phone extends MerchandiseV2 {
// 给Phone增加新的属性和方法
private double screenSize;
private double cpuHZ;
private int memoryG;
private int storageG;
private String brand;
private String os;
private static int MAX_BUY_ONE_ORDER = 5;
public Phone(
String name, String id, int count, double soldPrice, double purchasePrice,
double screenSize, double cpuHZ, int memoryG, int storageG, String brand, String os
) {
super(name, id, count, soldPrice * 1.2, purchasePrice);
this.screenSize = screenSize;
this.cpuHZ = cpuHZ;
this.memoryG = memoryG;
this.storageG = storageG;
this.brand = brand;
this.os = os;
}
public double buy(int count) {
if (count > MAX_BUY_ONE_ORDER) {
System.out.println("购买失败,手机一次最多只能买" + MAX_BUY_ONE_ORDER + "个");
return -2;
}
return super.buy(count);
}
public String getName() {
return this.brand + ":" + this.os + ":" + super.getName();
}
public void describe() {
System.out.println("此手机商品属性如下");
super.describe();
System.out.println("手机厂商为" + brand + ";系统为" + os + ";硬件配置如下:\n" +
"屏幕:" + screenSize + "寸\n" +
"cpu主频" + cpuHZ + " GHz\n" +
"内存" + memoryG + "Gb\n" +
"存储空间" + storageG + "Gb");
}
public boolean meetCondition() {
return true;
}
public double getScreenSize() {
return screenSize;
}
public void setScreenSize(double screenSize) {
this.screenSize = screenSize;
}
public double getCpuHZ() {
return cpuHZ;
}
public void setCpuHZ(double cpuHZ) {
this.cpuHZ = cpuHZ;
}
public int getMemoryG() {
return memoryG;
}
public void setMemoryG(int memoryG) {
this.memoryG = memoryG;
}
public int getStorageG() {
return storageG;
}
public void setStorageG(int storageG) {
this.storageG = storageG;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getOs() {
return os;
}
public void setOs(String os) {
this.os = os;
}
}
public class ShellColorChangePhone extends Phone {
private boolean enableShellColorChange;
public ShellColorChangePhone(String name, String id, int count, double soldPrice, double purchasePrice,
double screenSize, double cpuHZ, int memoryG, int storageG, String brand, String os) {
super(name, id, count, soldPrice, purchasePrice, screenSize, cpuHZ, memoryG, storageG, brand, os);
enableShellColorChange = false;
}
public boolean isEnableShellColorChange() {
return enableShellColorChange;
}
public void setEnableShellColorChange(boolean enableShellColorChange) {
this.enableShellColorChange = enableShellColorChange;
}
@Override
public void describe() {
super.describe();
System.out.println("壳色随着屏幕色变的功能开启状态:" + enableShellColorChange);
}
@Override
public double calculateProfit() {
// TODO 厂家提供10个点的返点
return super.calculateProfit() + super.getPurchasePrice() * 0.1;
}
}
可以用子类的引用给父类的引用赋值,也就是说,父类的引用可以指向子类的对象
但是反之则不行,不能让子类的引用指向父类的对象。因为父类并没有子类的属性和方法呀
重点
因为子类继承了父类的方法和属性,所以父类的对象能做到的,子类的对象肯定能做到
换句话说,我们可以在子类的对象上,执行父类的方法
当父类的引用指向子类的实例(或者父类的实例),只能通过父类的引用,像父类一样操作子类的对象
也就是说"名"的类型,决定了能执行哪些操作
如果确定一个父类的引用指向的对象,实际上就是一个子类的对象(或者子类的子类的对象),可以强制类型转换
父类的引用,可以指向子类的对象,即可以用子类(以及子类的子类)的引用给父类的引用赋值