本文主要是介绍java用继承简单模拟实现群主发红包,成员收红包,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
先创建父类用户
import java.util.Scanner;class User{String name;int mymoney;int sendmoney;int number;public User() {}public User(String name, int mymoney, int sendmoney) {this.name = name;this.mymoney = mymoney;this.sendmoney = sendmoney;this.number = number;}//定义成员方法,获取成员数量public int numberExact(){System.out.println("请输入红包的数量:");Scanner nu = new Scanner(System.in);number = nu.nextInt();return number;}//定义成员方法,发红包public void sendmoney(){System.out.println("发红包,请输入红包总金额:");Scanner mo = new Scanner(System.in);sendmoney = mo.nextInt();};//定义成员方法,收钱public void recivemoney(){System.out.println("我收到红包啦!");mymoney +=sendmoney;}
}
创建群主类
class Leadman extends User{//初始化public Leadman() {}public Leadman(String name, int mymoney, int sendmoney) {super(name, mymoney, sendmoney);}//定义成员方法,实现发红包public void leadsendmoney(){int mymoney=100;super.sendmoney();super.numberExact();mymoney -= sendmoney;sendmoney = super.sendmoney/number;System.out.println("每个群成员可获取红包:"+sendmoney);System.out.println("我的余额:"+mymoney);};public static void main(String[] args) {System.out.println("我是群主我要发红包啦!");Leadman leads = new Leadman();leads.leadsendmoney();}
}
更好的思路应该这样分析
这篇关于java用继承简单模拟实现群主发红包,成员收红包的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!