类的多态
多态
多态:⼀个对象具有多种状态的⾏为
1、要有继承
2、要有重写
3、父类的引用指向子类的对象
Animal实例
定义以下类:
(1) Dog类:变量(昵称、健康值、亲密度、品种),输出宠物信息的方法。
(2)Penguin企鹅类:变量(昵称、健康值、亲密度、性别),输出宠物信息的方法。
(3)抽取Dog和Penguin的父类
(4)定义测试类
要求:
a) 正确编写Dog类,编写类中特有的变量输出信息的方法
b)正确编写Penguin类,编写类中特有的变量和输出信息的方法
c) 多态方式创建对象,调用其方法
public class Animal {
private String name;//昵称
private double num1;//健康值
private double num2;//亲密度
public Animal(String name, double num1, double num2) {
this.name = name;
this.num1 = num1;
this.num2 = num2;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getNum1() {
return num1;
}
public void setNum1(double num1) {
this.num1 = num1;
}
public double getNum2() {
return num2;
}
public void setNum2(double num2) {
this.num2 = num2;
}
}
public class Dog extends Animal{
private String kind;//品种
public Dog(String name, double num1, double num2, String kind) {
super(name, num1, num2);
this.kind = kind;
}
public String toString() {
return "昵称-"+super.getName()+"\t健康值-"+super.getNum1()+"\t亲密度-"+super.getNum2()+"\t品种-"+this.getKind();
}
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
}
public class Penguin extends Animal{
private String sex;//性别
public Penguin(String name, double num1, double num2, String sex) {
super(name, num1, num2);
this.sex = sex;
}
public String toString() {
return "昵称-"+super.getName()+"\t健康值-"+super.getNum1()+"\t亲密度-"+super.getNum2()+"\t性别-"+this.getSex();
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
public class TestAnimal {
public static void main(String[] args) {
Animal animal;
animal = new Dog("二哈", 2.5, 1.5,"哈士奇");
System.out.println(animal.toString());
animal = new Penguin("企鹅", 1.0, 2.3,"男");
System.out.println(animal.toString());
}
}
运行结果:
final关键字
**final修饰类:**不能被继承,即final类不能有子类
final修饰方法: 该方法就不能被子类重写,final方法的行为不允许子类进行篡改
final修饰成员变量: 可以操作使用它,但是不能对它进行更改操作
person实例
Person类:可以吃饭、睡觉、走路
Banker类:可以投资、出行方式:坐飞机
Coder类:可以敲代码、出行方式:坐火箭
public class Person {
public final void Eat() {//吃饭
System.out.println("吃饭");
}
public void Sleep() {//睡觉
System.out.println("睡觉");
}
public void Walk() {//走路
System.out.println("走路");
}
}
public class Banker extends Person{
public void invest() {//投资
System.out.println("投资");
}
@Override
public void Walk() {
System.out.println("坐飞机");
}
}
public class Coder extends Person{
public void Coding() {//写代码
System.out.println("写代码");
}
@Override
public void Walk() {
System.out.println("坐火箭");
}
}
public class TestPerson {
public static void main(String[] args) {
Person person;
person = new Banker();
person.Eat();
person.Sleep();
person.Walk();
((Banker) person).invest();
System.out.println("*******");
person = new Coder();
person.Eat();
person.Sleep();
person.Walk();
((Coder) person).Coding();
}
}
向上、向下转型
向上转型: 父类的引用指向子类的对象
向下转型: 子类的引用指向父类的对象,需要强制类型转换符进行转换
只能先向上再向下进行转型,向下转型后不能再向上转型
花木兰实例
public class Father {
public void fight() {
System.out.println("我是花湖,我要去打仗");
}
}
public class Daughter extends Father{
@Override
public void fight() {
System.out.println("我是女儿花木兰,我替父从军");
}
public void dressup() {
System.out.println("我是女儿花木兰,画一个妆");
}
}
public class Test {
public static void main(String[] args) {
//女扮男装,由女儿的身份变成父亲的身份去打仗
//向上转型
Daughter daughter = new Daughter();
Father father = daughter;
daughter = null;
father.fight();
System.out.println("***********");
//打仗归来,恢复女儿身份
//向下转型
daughter = (Daughter)father;
father = null;
daughter.dressup();
}
}
instanceOf :判断对象是否是特定类的一个实例
实例
北方人吃馒头,南方人吃米饭,厨师根据人进行做饭
package person1;
public class Person {
public String name;//姓名
public void Eat() {
System.out.println("吃饭");
}
}
package person1;
public class NorthPerson extends Person{
@Override
public void Eat() {
System.out.println("馒头");
}
}
package person1;
public class SouthPerson extends Person{
@Override
public void Eat() {
System.out.println("米饭");
}
}
package person1;
public class Cook extends Person{
public void Cook(Person p) {
if(p instanceof SouthPerson) {//判断进店的是南方人
//向下转型 强制类型转换
SouthPerson sp = (SouthPerson)p;
sp.Eat();
}
else {
NorthPerson np = (NorthPerson)p;
np.Eat();
}
}
}
package person1;
public class TestPerson {
public static void main(String[] args) {
SouthPerson zhangsan = new SouthPerson();
zhangsan.name = "南方";
NorthPerson lisi = new NorthPerson();
lisi.name = "北方";
Cook xinDongFang = new Cook();
xinDongFang.Cook(lisi);
xinDongFang.Cook(zhangsan);
}
}
运行结果:
店铺类Store实例
设计店铺类Store ,属性包括店铺名称、店铺地址、员工人数、注册资金;方法包括开店、闭店、购物。
设计店铺类子类:
直营店DirectSale,继承店铺类,属性包含店长姓名、送货工人人数、商品起送金额、特殊商品折扣比率;方法包括送货。
加盟店Franchise,继承店铺类,属性包含加盟商姓名、加盟金额、加盟时间(字符串);方法包括解约。
折扣店Discount,继承店铺类,属性包括最大折扣、最小折扣、是否打折;方法包括打折。
要求:
1、所有父子类均完成get和set封装,以及构造方法(无参构造方法和有参构造方法)封装。
2、子类继承父类时,重写父类开店、闭店、购物方法,方法体为输出开店闭店时间。
3、参观某店,输出这个店铺的全部信息(例如参观直营店,输出店铺信息及店长名称、起送金额等)。
4、手动输入(直营店、加盟店、折扣店),判断输入内容,实例化不同子类对象,初始化店铺信息。
5、去某店购物,直营店要求输入购买金额判断是否能够配送,判断送货工人人数是否能够配送;加盟店要求输入加盟金额,判断是否可以加盟;折扣店输入购买金额,判断是否开始打折,如果未开始打折,输出对不起,未开始打折活动,如果以开始打折,输出最大折扣和最小折扣。使用instanceof关键字判断
6、编写测试类完成相关功能输入和输出
package store;
/**
* 店铺类
* */
public class Store {
private String name;//店铺名称
private String address;//店铺地址
private int num;//员工人数
private double money;//注册资金
public void openStore() {//开店
}
public void closeStore() {//闭店
}
public void shopping() {//购物
}
public Store() {
}
public Store(String name, String address, int num, double money) {
this.name = name;
this.address = address;
this.num = num;
this.money = money;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
}
package store;
/**
* 直营店类
* */
public class DirectSale extends Store{
private String directSaleName;//店长姓名
private int number;//送货工人人数
private double amount;//商品起送金额
private double rates;//特殊商品折扣比率
public void deliverGoods() {//送货
}
public DirectSale() {
}
public DirectSale(String name, String address, int num, double money, String directSaleName, int number,
double amount, double rates) {
super(name, address, num, money);
this.directSaleName = directSaleName;
this.number = number;
this.amount = amount;
this.rates = rates;
}
@Override
public void openStore() {
System.out.println("早上6:00");
}
@Override
public void closeStore() {
System.out.println("晚上7:00");
}
@Override
public void shopping() {
}
public String toString() {
return "直营店的店长姓名为:"+this.getDirectSaleName()+"\t送货工人人数为:"+this.getNumber()+"\t送货工人人数为:"+this.getAmount()+"\t特殊商品折扣比率为:"+this.getRates();
}
public String getDirectSaleName() {
return directSaleName;
}
public void setDirectSaleName(String directSaleName) {
this.directSaleName = directSaleName;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public double getRates() {
return rates;
}
public void setRates(double rates) {
this.rates = rates;
}
}
package store;
/**
* 加盟店类
* */
public class Franchise extends Store{
private String franchiseName;//加盟商姓名
private double famount;//加盟金额
private String time;//加盟时间
public void termination() {//解约
}
public Franchise() {
}
public Franchise(String name, String address, int num, double money, String franchiseName, double famount,
String time) {
super(name, address, num, money);
this.franchiseName = franchiseName;
this.famount = famount;
this.time = time;
}
@Override
public void openStore() {
System.out.println("早上5:30");
}
@Override
public void closeStore() {
System.out.println("晚上6:00");
}
@Override
public void shopping() {
}
public String toString() {
return "加盟商姓名为:"+this.getFranchiseName()+"\t加盟金额为:"+this.getFamount()+"\t加盟时间为:"+this.getTime();
}
public String getFranchiseName() {
return franchiseName;
}
public void setFranchiseName(String franchiseName) {
this.franchiseName = franchiseName;
}
public double getFamount() {
return famount;
}
public void setFamount(double famount) {
this.famount = famount;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
package store;
/**
* 打折店类
* */
public class Discount extends Store{
private double max;//最大折扣
private double min;//最小折扣
private String discount;//是否打折
public void discount() {//打折
}
public Discount() {
}
public Discount(String name, String address, int num, double money, double max, double min, String discount) {
super(name, address, num, money);
this.max = max;
this.min = min;
this.discount = discount;
}
public String toString() {
return "打折店的最大折扣为:"+this.getMax()+"\t最小折扣为:"+this.getMin();
}
@Override
public void openStore() {
System.out.println("早上5:00");
}
@Override
public void closeStore() {
System.out.println("晚上8:00");
}
@Override
public void shopping() {
}
public double getMax() {
return max;
}
public void setMax(double max) {
this.max = max;
}
public double getMin() {
return min;
}
public void setMin(double min) {
this.min = min;
}
public String getDiscount() {
return discount;
}
public void setDiscount(String discount) {
this.discount = discount;
}
}
package store;
/**
* 购物者
* */
import java.util.Scanner;
import financial.Fund;
public class Person {
public void buy(Store s) {
Scanner scanner = new Scanner(System.in);
//直营店
if(s instanceof DirectSale) {
DirectSale directSale = (DirectSale)s;//向下转型
System.out.println(directSale.toString());
System.out.println("请输入购买的金额及需要配送的人数:");
double n1 = scanner.nextDouble();//购买的金额
int n2 = scanner.nextInt();//需要配送的人数
double n0 = 1000;//配送最低金额
//判断金额是否达到最低配送金额
if( n1 >= n0 ) {
//判断配送人数
if(n2 <= 3) {
System.out.println("人员足够,可以进行配送");
}
else {
System.out.println("人员缺少,不可以进行配送");
}
}
else {
System.out.println("消费金额过少,不进行配送");
}
}
//加盟店
else if(s instanceof Franchise) {
Franchise franchise = (Franchise)s;
System.out.println(franchise.toString());
double num0 = 10000;//最低加盟金额
System.out.println("请输入加盟金额:");
double num1 = scanner.nextDouble();
//判断是否可以进行加盟
if(num1 >= num0) {
System.out.println("可以进行加盟");
}
else {
System.out.println("金额过少,不可以进行加盟");
}
}
//折扣店
else if(s instanceof Discount) {
Discount discount = (Discount)s;
System.out.println(discount.toString());
double number0 = 1000;//最低购买金额
System.out.println("请输入购买金额:");
double number1 = scanner.nextDouble();
//判断是否达到最低购买金额
if(number1 >= number0) {
System.out.println("可以进行打折");
System.out.println("最大折扣为:"+discount.getMax()*number1);
System.out.println("最小折扣为:"+discount.getMin()*number1);
}
else {
System.out.println("购买金额过少,不可以进行打折");
}
}
}
}
package store;
import java.util.Scanner;
public class TestStore {
public static void main(String[] args) {
DirectSale directSale = new DirectSale("直营店", "北京", 3, 50000, "张三", 1, 300, 0.5);
Franchise franchise = new Franchise("加盟店", "上海", 3, 30000, "李四", 20000, "2021年");
Discount discount =new Discount("打折店", "广州", 3, 30000, 0.8, 0.95, "是");
System.out.println("请输入店铺名称:");
String s = new Scanner(System.in).next();
Person person = new Person();
if(s.equals(directSale.getName())) {
person.buy(directSale);
}
else if(s.equals(discount.getName())) {
person.buy(discount);
}
else if(s.equals(franchise.getName())) {
person.buy(franchise);
}
else {
System.out.println("输入错误");
}
}
}
运行结果:
-
直营店
- 金额达到最低标准,人数足够
- 金额未达到最低标准,人数足够
- 金额达到最低标准,人数不够
- 金额达到最低标准,人数足够
-
加盟店
-
加盟金额达到最低标准
-
加盟金额未达到最低标准
-
-
打折店
- 购买金额达到最低标准,可以进行打折
- 购买金额未达到最低标准,不可以进行打折
金融产品类Financial实例
设计金融产品类Financial,属性包括产品名称、产品介绍、起投金额、产品期限(int)、年化收益(百分数);方法包括发布、截止、投资。
设计金融产品类子类:
基金产品Fund,继承金融产品类,属性包括投资地区、投资人、投资金额
股票产品Shares,继承金融产品类,属性包括股票价值、购买价格、购买 笔数
要求:
1、所有父子类均完成get和set封装,以及构造方法(无参构造方法和有参构造方法)封装。
2、子类继承父类时,重写父类发布、截止、投资方法,方法体为输出发布、截止时间。
3、输入购买产品,判断实例化哪个子类对象,初始化父子类属性
4、投资,投资基金产品时,判断投资金额是否超过起投金额,如果未超过,提示投资失败,如果超过,输出基金产品完整信息并计算年化收益。(年化收益=投资金额期限年化收益)
5、投资,投资股票产品时,判断投资金额是否超过起投金额,如果未超过,提示投资失败,如果超过,输出股票产品完整信息并计算年化收益。年化收益=投资金额笔数期限*年化收益)
6、投资时使用instanceof关键字判断
package financial;
/**
* 金融产品
* */
public class Financial {
private String name;//产品名称
private String introduce;//产品介绍
private double money;//起投金额
private int time;//产品期限
private double earn;//年化收益
public void announced() {//发布
}
public void stop() {//截止
}
public void invest() {//投资
}
public Financial(String name, String introduce, double money, int time, double earn) {
super();
this.name = name;
this.introduce = introduce;
this.money = money;
this.time = time;
this.earn = earn;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIntroduce() {
return introduce;
}
public void setIntroduce(String introduce) {
this.introduce = introduce;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public int getTime() {
return time;
}
public void setTime(int time) {
this.time = time;
}
public double getEarn() {
return earn;
}
public void setEarn(double earn) {
this.earn = earn;
}
}
package financial;
/**
* 基金产品
* */
public class Fund extends Financial{
private String address;//投资地区
private String fundName;//投资人
private double cost;//投资金额
public Fund(String name, String introduce, double money, int time, double earn, String address, String fundName,
double cost) {
super(name, introduce, money, time, earn);
this.address = address;
this.fundName = fundName;
this.cost = cost;
}
public String toString() {
return "投资地区:"+this.getAddress()+"\t投资人:"+this.getFundName()+"\t投资金额:"+this.getCost();
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getFundName() {
return fundName;
}
public void setFundName(String fundName) {
this.fundName = fundName;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
}
package financial;
/**
* 股票产品
* */
public class Shares extends Financial{
private String value;//股票价值
private double price;//购买价格
private int num;//购买笔数
public Shares(String name, String introduce, double money, int time, double earn, String value, double price,
int num) {
super(name, introduce, money, time, earn);
this.value = value;
this.price = price;
this.num = num;
}
public String toString() {
return "股票价值:"+this.getValue()+"\t购买价格:"+this.getPrice()+"\t购买笔数:"+this.getNum();
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
package financial;
import java.util.Scanner;
public class Person {
public void buyFinancial(Financial f) {
Scanner scanner = new Scanner(System.in);
//基金
if(f instanceof Fund) {
Fund fund = (Fund)f;
double n0 = 1000;//起投金额
System.out.println("请输入投资的金额:");
double n1 = scanner.nextDouble();//投资的金额
//判断投资金额是否超过起投金额
if(n1 >= n0) {
System.out.println(fund.toString());
double m = n1*fund.getTime()*fund.getEarn();//计算年化收益
System.out.println("年化收益为:"+m);
}
else {
System.out.println("投资失败");
}
}
//股票
else if(f instanceof Shares) {
Shares shares = (Shares)f;
System.out.println(shares.toString());
double num0 = 1000;//起投金额
System.out.println("请输入投资的金额:");
double num1 = scanner.nextDouble();//投资的金额
//判断投资金额是否超过起投金额
if(num1 >= num0) {
System.out.println(shares.toString());
double m = num1*shares.getTime()*shares.getEarn();//计算年化收益
System.out.println("年化收益为:"+m);
}
else {
System.out.println("投资失败");
}
}
}
}
package financial;
import java.util.Scanner;
public class TestFinancial {
public static void main(String[] args) {
Fund fund = new Fund("基金", "基金产品",500, 30, 0.05, "北京", "张三", 600);
Shares shares = new Shares("股票", "股票产品", 500, 30, 5, "有价值", 60, 3);
System.out.println("请输入金融产品:");
String s = new Scanner(System.in).next();
Person p = new Person();
if(s.equals(fund.getName())) {
p.buyFinancial(fund);;
}
else if(s.equals(shares.getName())) {
p.buyFinancial(shares);
}
else {
System.out.println("输入错误");
}
}
}
运行结果:
-
基金
-
投资成功
-
投资失败
-
-
股票
-
投资成功
-
投资失败
-